一、引言
学习spring不可避免的会涉及到IOC,IOC目的就是为了解耦,用一个中介容器控制不同模块之间的联系。当时为了解决对象之间的耦合度过高的问题,软件专家Michael Mattson提出了IOC理论,用来实现对象之间的“解耦”。
简单来讲可以用一下的图说明,以前的面向对象程序相互依赖,深度结合,可能在里面有各种new操作:
在使用IOC改变后的程序如下,模块之间没有了特别直接的联系,耦合都降低了:
二、IOC(DI)的三种使用
这三种使用方法各自独立,可以单独使用,注解方法都是后来兴起的,但是大家一般都习惯使用混合式的,其他的介绍只是为了让大家了解。
1、xml配置方法
- 函数调用bean配置文件
public String springtest() {
ApplicationContext context=new ClassPathXmlApplicationContext("classpath:spring/bean1.xml");
TestService testService= (TestService) context.getBean("testService");
testService.add();
return "success";
}
- bean配置文件(将dao当作属性注入到service中,当然service中也有dao的对象)
<?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-3.0.xsd">
<bean id="testDao" class="com.xcy.dao.TestDao"></bean>
<bean id="testService" class="com.xcy.service.TestService">
<property name="testDao" ref="testDao"></property>
</bean>
</beans>
- serviceydao文件
public class TestService {
private TestDao testDao;
public TestDao getTestDao() {
return testDao;
}
public void setTestDao(TestDao testDao) {
this.testDao = testDao;
}
public void add() {
System.out.print("==service==");
testDao.add();
}
}
public class TestDao {
public void add() {
System.out.println("==dao==");
}
}
2、注解方法
- 调用函数
@Controller
public class Hellowmvn {
@Autowired
Test2Service test2Service;
@RequestMapping(value="/Hellowmvn2")
@ResponseBody
public String test2() {
return test2Service.test();
}
}
- bean配置(扫描注解配置,针对@repository@component@service@controller)
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!--开启注解扫描 -->
<context:component-scan base-package="com.xcy.service"></context:component-scan>
<!-- <bean id="testService" class="com.xcy.service.TestService"></bean>
--></beans>
- 添加注解的service类
@Service(value="test2Service")
public class Test2Service {
public String test() {
return "xcyservice";
}
}
3、xml配置与注解混合使用
混合方法使用核心:通过xml配置进行对象创建,使用注解进行属性注入,将属性注入过程从xml配置中分解出来==
- 调用函数(属性注入)
@Controller
public class Hellowmvn {
@Autowired
TestService testService;
@RequestMapping(value="/Hellowmvn")
@ResponseBody
public String test() {
return testService.test();
}
}
- service类
public class TestService {
public String test() {
return "xcyservice";
}
}
- 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" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!--开启注解扫描 -->
< <bean id="testService" class="com.xcy.service.TestService"></bean>
</beans>
三、总结
- IOC的原理与作用;
- IOC xml配置方法使用;
- IOC注解方法使用;
- IOC混合方法使用;