Spring bean的依赖注入--Spring学习之路2
此教程先讲如何创建一个bean和实验效果,解说在后面再补回来,或者可以看看慕课网的《Spring入门篇》。此教程主要是针对该视频,该视频的教材比较久,而且本人比较喜欢用idea,为了实现与视频对应的效果花了挺多时间的,此时本人也是刚刚学习Spring一周不到。
首先,建立Spring-injection.xml,在src文件目录下,并在Project Structure的Modules添加到项目里面
1 2然后在mian中建立四个类。在新建类用逗号分隔来表示在哪个包下,非常方便
- com.study.injection.service.InjectionService.class
- com.study.injection.service.InjectionServiceImpl.class
- com.study.injection.dao.InjectionDao.class
- com.study.injection.dao.InjectionDaoImpl.class
并把其中InjectionService.class和InjectionDao.class改成接口
InjectionDao:
package com.study.injection.dao;
public interface InjectionDao {
public void save(String arg);
}
InjectionService:
package com.study.injection.service;
public interface InjectionService {
public void save(String arg);
}
填写InjectionServiceImpl和InjectionDaoImpl
InjectionServiceImpl:
package com.study.injection.service;
import com.study.injection.dao.InjectionDao;
public class InjectionServiceImpl implements InjectionService{
private InjectionDao injectionDao;
//设值注入
public void setInjectionDao(InjectionDao injectionDao) {
this.injectionDao = injectionDao;
}
//构造器注入
public InjectionServiceImpl(InjectionDao injectionDao){
this.injectionDao = injectionDao;
}
@Override
public void save(String arg) {
System.out.println("InjectionService:" + arg);
arg = arg + this.hashCode();
injectionDao.save(arg);
}
}
InjectionDaoImpl:
package com.study.injection.dao;
public class InjectionDaoImpl implements InjectionDao{
@Override
public void save(String arg) {
System.out.println("InjectionDao:" + arg);
}
}
需要用到的测试的类已经建立完和填写完后,在test下建立测试类injection.TestInejction.class
<pre><code>
package injection;
import com.study.injection.service.InjectionService;
import org.junit.*;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
@RunWith(BlockJUnit4ClassRunner.class)
public class TestInjection {
//设值注入
@Test
public void testSet(){
ApplicationContext context = new FileSystemXmlApplicationContext("src/Spring-injection.xml");
InjectionService service =(InjectionService) context.getBean("injectionService");
service.save("test1:这是要保存的数据");
}
//构造注入
@Test
public void testCon(){
ApplicationContext context = new FileSystemXmlApplicationContext("src/Spring-injection.xml");
InjectionService service = (InjectionService) context.getBean("injectionService");
service.save("这是要保存的数据");
}
}
</pre></code>
说明,使用设值注入的时候把构造注入的代码注释掉,同理使用构造注入把设值注入注释
修改Spring-injection.xml的内容:
<pre><code>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="injectionService" class="com.study.injection.service.InjectionServiceImpl" >
<!--设值注入-->
<!--<property name="injectionDao" ref="injectionDao"></property>-->
<!--构造注入-->
<constructor-arg name="injectionDao" ref="injectionDao"></constructor-arg>
</bean>
<bean id="injectionDao" class="com.study.injection.dao.InjectionDaoImpl"/>
</beans>
</pre></code>
输出单元测试结果为结果为:
4