2004年, Martin Fowler 探讨了同一个问题,既然 IoC 是控制反转,那么究竟是“哪些方面的控制被反转了呢?”,通过详细地分析和论证后,他得出了答案:“得到依赖对象的过程被反转了”。控制被反转以后,得到依赖对象的过程由自身管理对象变为由 IoC 容器主动注入。因而,他给“控制反转”取了一个更合适的名字叫作“依赖注入(Dependency Injection,DI)”。他的这个答案,实际上给出了实现 IoC 的方法:注入。所谓依赖注入,就是由 IoC 容器在运行期间,动态地将某种依赖关系注入到对象之中。html
房屋中介 IoCweb
1. 找中介 ----> 1. 找IoC容器spring
2. 中介介绍房子 ----> 2. 找IoC容器编程
3. 租房、入住 ----> 3. 使用对象app
UnitTestBase 类单元测试
package com.imooc.test.base; import org.junit.After; import org.junit.Before; import org.springframework.beans.BeansException; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.util.StringUtils; public class UnitTestBase { private ClassPathXmlApplicationContext context; private String springXmlpath; public UnitTestBase() {} public UnitTestBase(String springXmlpath) { this.springXmlpath = springXmlpath; } @Before // Test 执行前执行
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 // Test 执行结束后执行
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; } } }
配置 spring-ioc.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="oneInterface" class="com.imooc.ioc.interfaces.OneInterfaceImpl"></bean>
</beans>
测试类this
package com.imooc.test.ioc.interfaces; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.BlockJUnit4ClassRunner; import com.imooc.ioc.interfaces.OneInterface; import com.imooc.test.base.UnitTestBase; @RunWith(BlockJUnit4ClassRunner.class) public class TestOneInterface extends UnitTestBase { public TestOneInterface() { super("classpath*:spring-ioc.xml"); } @Test public void testSay() { OneInterface oneInterface = super.getBean("oneInterface"); oneInterface.say("This is a test."); } }
输出结果spa
文件设计
FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext("D:/workspace/appcontext.xml");
Classpath
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-context.xml");
Web应用
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
本文出自:艺意