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>
本文出自:藝意