ioc控制反转:控制权转移,应用程序本身不负责依赖对象的创建和维护,而是由外部容器负责和维护。ioc的目的是创建对象并且组装对象之间的关系。
举个例子,构建注入
dao层
public interface InjectionDAO {
public void save(String arg);
}
public class InjectionDAOImpl implements InjectionDAO {
public void save(String arg) {
//模拟数据库保存操作
System.out.println("保存数据:" + arg);
}
}
service 层接口:
public interface InjectionService {
public void save(String arg);
}
service 层实现:
public class InjectionServiceImpl implements InjectionService {
private InjectionDAO injectionDAO;
//构造器注入
public InjectionServiceImpl(InjectionDAO injectionDAO1) {
this.injectionDAO = injectionDAO1;
}
//设值注入
public void setInjectionDAO(InjectionDAO injectionDAO) {
this.injectionDAO = injectionDAO;
}
public void save(String arg) {
//模拟业务操作
System.out.println("Service接收参数:" + arg);
arg = arg + ":" + this.hashCode();
injectionDAO.save(arg);
}
在spring xml中的配置
<?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.forezp.ioc.injection.service.InjectionServiceImpl">
<constructor-arg name="injectionDAO1" ref="injectionDAO"></constructor-arg>
</bean>
<bean id="injectionDAO" class="com.forezp.ioc.injection.dao.InjectionDAOImpl"></bean>
</beans>
单元测试
本系列文章单元测试基类
public class UnitTestBase {
private ClassPathXmlApplicationContext context;
private String springXmlpath;
public UnitTestBase() {}
public UnitTestBase(String springXmlpath) {
this.springXmlpath = springXmlpath;
}
@Before
public void before() {
if (StringUtils.isEmpty(springXmlpath)) {
springXmlpath = "classpath*:spring-*.xml";
}
try {
context = new ClassPathXmlApplicationContext(springXmlpath.split("[,\\s]+"));
context.start();
} catch (BeansException e) {
e.printStackTrace();
}
}
@After
public void after() {
context.destroy();
}
@SuppressWarnings("unchecked")
protected <T extends Object> T getBean(String beanId) {
try {
return (T)context.getBean(beanId);
} catch (BeansException e) {
e.printStackTrace();
return null;
}
}
protected <T extends Object> T getBean(Class<T> clazz) {
try {
return context.getBean(clazz);
} catch (BeansException e) {
e.printStackTrace();
return null;
}
}
}
单元测试:
@RunWith(BlockJUnit4ClassRunner.class)
public class TestInjection extends UnitTestBase {
public TestInjection() {
super("classpath:spring-injection.xml");
}
@Test
public void testSetter() {
InjectionService service = super.getBean("injectionService");
service.save("这是要保存的数据");
}
@Test
public void testCons() {
InjectionService service = super.getBean("injectionService");
service.save("这是要保存的数据");
}
}
运行打印:
>
Service接收参数:这是要保存的数据 保存数据:这是要保存的数据:1247298779
这个例子说明,我们可以通过ClassPathXmlApplicationContext.getBean()获取到了service,这个service 是通过xml配置注入到容器中,并且注入的时候通过构造函数的设置了成员变量dao。
举个例子: 测试sinleton和prototype
创建bean实例
public class BeanScope {
public void say() {
System.out.println("BeanScope say : " + this.hashCode());
}
}
在xml中配置,作用域为singleton
<?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="beanScope" class="com.imooc.bean.BeanScope" scope="singleton"></bean>
</beans>
单元测试:
@RunWith(BlockJUnit4ClassRunner.class)
public class TestBeanScope extends UnitTestBase {
public TestBeanScope() {
super("classpath*:spring-beanscope.xml");
}
@Test
public void testSay() {
BeanScope beanScope = super.getBean("beanScope");
beanScope.say();
BeanScope beanScope2 = super.getBean("beanScope");
beanScope2.say();
}
}
运行单元测试:
>
BeanScope say : 1113008012 BeanScope say : 1113008012
在xml中配置,作用域为prototype
<?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="beanScope" class="com.imooc.bean.BeanScope" scope="prototype"></bean>
</beans>
运行单元测试
>
BeanScope say : 144724468 BeanScope say : 1432645272
由此可发现sington在bean容器是一个实例,而prototype创建了二个实例。
包括以下几个方面:
有两种方式
也有两种方式:
举个例子:
创建bean实例:
public class BeanLifeCycle implements InitializingBean, DisposableBean {
public void defautInit() {
System.out.println("Bean defautInit.");
}
public void defaultDestroy() {
System.out.println("Bean defaultDestroy.");
}
@Override
public void destroy() throws Exception {
System.out.println("Bean destroy.");
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("Bean afterPropertiesSet.");
}
public void start() {
System.out.println("Bean start .");
}
public void stop() {
System.out.println("Bean stop.");
}
}
bean的配置:
<?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"
default-init-method="defautInit" default-destroy-method="defaultDestroy">
<bean id="beanLifeCycle" class="com.imooc.lifecycle.BeanLifeCycle" init-method="start" destroy-method="stop"></bean>
</beans>
单元测试:
@RunWith(BlockJUnit4ClassRunner.class)
public class TestBeanLifecycle extends UnitTestBase {
public TestBeanLifecycle() {
super("classpath:spring-lifecycle.xml");
}
@Test
public void test1() {
super.getBean("beanLifeCycle");
}
}
运行:
Bean afterPropertiesSet. Bean start . Bean destroy. Bean stop.
同时实现两种方式的初始化方法的执行顺序: 接口实现优先于xml中的配置。
举例子: 1.byName方式:
创建一个dao:
public class AutoWiringDAO {
public void say(String word) {
System.out.println("AutoWiringDAO : " + word);
}
}
创建一个service
public class AutoWiringService {
private AutoWiringDAO autoWiringDAO;
public void setAutoWiringDAO(AutoWiringDAO autoWiringDAO) {
System.out.println("setAutoWiringDAO");
this.autoWiringDAO = autoWiringDAO;
}
public void say(String word) {
this.autoWiringDAO.say(word);
}
}
在xml中配置:
<?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"
default-autowire="byName">
<bean id="autoWiringService" class="com.imooc.autowiring.AutoWiringService" ></bean>
<bean id="autoWiringDAO" class="com.imooc.autowiring.AutoWiringDAO" ></bean>
</beans>
单元测试:
@RunWith(BlockJUnit4ClassRunner.class)
public class TestAutoWiring extends UnitTestBase {
public TestAutoWiring() {
super("classpath:spring-autowiring.xml");
}
@Test
public void testSay() {
AutoWiringService service = super.getBean("autoWiringService");
service.say(" this is a test");
}
}
运行:
setAutoWiringDAO AutoWiringDAO : this is a test
通过default-autowire=”byName”; AutoWiringService 自动获取了autoWiringDAO的实例。
2.byTYpe
将在xml中配置改为byType :
<?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"
default-autowire="byName">
<bean id="autoWiringService" class="com.imooc.autowiring.AutoWiringService" ></bean>
<bean id="autoWiringDAO" class="com.imooc.autowiring.AutoWiringDAO" ></bean>
</beans>
其他不变,运行和byName 一样。
3.constructor
<?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"
default-autowire="constructor">
<bean id="autoWiringService" class="com.imooc.autowiring.AutoWiringService" ></bean>
<bean id="autoWiringDAO" class="com.imooc.autowiring.AutoWiringDAO" ></bean>
</beans>
AutoWiringService 中增加构造器
public class AutoWiringService {
private AutoWiringDAO autoWiringDAO;
public AutoWiringService(AutoWiringDAO autoWiringDAO) {
System.out.println("AutoWiringService");
this.autoWiringDAO = autoWiringDAO;
}
public void setAutoWiringDAO(AutoWiringDAO autoWiringDAO) {
System.out.println("setAutoWiringDAO");
this.autoWiringDAO = autoWiringDAO;
}
public void say(String word) {
this.autoWiringDAO.say(word);
}
}
允行:
AutoWiringDAO : this is a test
从spring 3.0开始,spring javaConfig 项目提供了许多特性,包括使用java而不是xml
1.比如注解
@Configuration @Bean @Import @DependsOn @Component 是一个通用注解,应用于任何bean @Reposity注解DAO @Service注解service @Controller注解controller
2.spring可以自动检测类并注册bean到applicationContext中。比如 @Service @Reposity等
3.< context:annoation-config />会查找applicationContext中bean的注解。
扫描 :< context:component-scan> 包含< context:annoation-config/>,通常只需要使用前者。
<context:component-scan base-package="com.forezp" >
举个例子: 通过扫描获取bean,在xml中的配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd" >
<context:component-scan base-package="com.forezp.beanannotation"></context:component-scan>
</beans>
定义一个bean类: 其中scope 注解表示bean的作用域,默认singleton。Component默认类名并将第一个字母小写。
@Scope
@Component
public class BeanAnnotation {
public void say(String arg) {
System.out.println("BeanAnnotation : " + arg);
}
public void myHashCode() {
System.out.println("BeanAnnotation : " + this.hashCode());
}
}
单元测试:
@RunWith(BlockJUnit4ClassRunner.class)
public class TestBeanAnnotation extends UnitTestBase {
public TestBeanAnnotation() {
super("classpath*:spring-beanannotation.xml");
}
@Test
public void testSay() {
BeanAnnotation bean = super.getBean("beanAnnotation");
bean.say("This is test.");
//bean = super.getBean("bean");
//bean.say("This is test.");
}
}
运行:
BeanAnnotation : This is test.
举个例子:
采用包扫描:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd" >
<context:component-scan base-package="com.forezp.beanannotation"></context:component-scan>
</beans>
采用注解:DAO层
@Repository
public class InjectionDAOImpl implements InjectionDAO {
public void save(String arg) {
//模拟数据库保存操作
System.out.println("保存数据:" + arg);
}
}
service层:
@Service
public class InjectionServiceImpl implements InjectionService {
// @Autowired
private InjectionDAO injectionDAO;
@Autowired
public InjectionServiceImpl(InjectionDAO injectionDAO) {
this.injectionDAO = injectionDAO;
}
// @Autowired
public void setInjectionDAO(InjectionDAO injectionDAO) {
this.injectionDAO = injectionDAO;
}
public void save(String arg) {
//模拟业务操作
System.out.println("Service接收参数:" + arg);
arg = arg + ":" + this.hashCode();
injectionDAO.save(arg);
}
}
单元测试:
“` @RunWith(BlockJUnit4ClassRunner.class) public class TestInjection extends UnitTestBase {
public TestInjection() {
super("classpath:spring-beanannotation.xml");
}
@Test
public void testAutowired() {
InjectionService service = super.getBean("injectionServiceImpl");
service.save("This is autowired.");
}
}
“`
运行:
Service接收参数:This is autowired. 保存数据:This is autowired.:1641742937
举个例子: 用注解去代替xml文件
@Configuration
public class StoreConfig {
@Bean(name = "stringStore", initMethod="init", destroyMethod="destroy")
public Store stringStore() {
return new StringStore();
}
javabean StringStore类
public class StringStore implements Store<String> {
public void init() {
System.out.println("This is init.");
}
public void destroy() {
System.out.println("This is destroy.");
}
}
单元测试:
@RunWith(BlockJUnit4ClassRunner.class)
public class TestJavabased extends UnitTestBase {
public TestJavabased() {
super("classpath*:spring-beanannotation.xml");
}
@Test
public void test() {
Store store = super.getBean("stringStore");
System.out.println(store.getClass().getName());
}
}
另外可以用ImportResource注解类获取资源文件信息:
@Configuration
@ImportResource("classpath:config.xml")
public class StoreConfig {
@Value("${url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${password}")
private String password;
举个例子:
Dao层:
@Repository
public class JsrDAO {
public void save() {
System.out.println("JsrDAO invoked.");
}
}
service层:
@Service
public class JsrServie {
@Resource
private JsrDAO jsrDAO;
// @Resource
// public void setJsrDAO(@Named("jsrDAO") JsrDAO jsrDAO) {
// this.jsrDAO = jsrDAO;
//}
@PostConstruct
public void init() {
System.out.println("JsrServie init.");
}
@PreDestroy
public void destroy() {
System.out.println("JsrServie destroy.");
}
public void save() {
jsrDAO.save();
}
}