实例理解IoC
2013-03-07 16:20
99 查看
控制反转(IoC:Inversion of Control)是Spring框架的核心,主要用来减少对象间的耦合问题。Martin Fowler曾提问:哪方面的控制被反转了?他总结是依赖对象的获得被反转。因此,他给控制反转提出一个更贴切的名字:依赖注入。Spring的优势是通过一个IoC容器,管理所有的JavaBean,自动注入依赖对象间的引用,减轻程序员的负担。下面通过一个简单的实例,理解Spring中IoC的优势和原理。
一个parent有两个children,一个son和一个daughter。parent每次出去玩的时候,只能带上一个小孩,因此只能轮流地和他们玩。他们的类的实现如下:
抽象类Child:
Child的子类Son:
Child的子类Daughter:
类parent:
有如下两种应用场景需求:
情况一:parent带son出去玩
情况二:parent带daughter出去玩
下面比较两种实现方法:
实现方法一:常规实现
实现方法二:IoC实现
beans.xml配置文件:
JUnit测试程序:
两种实现方式的比较:
1. 常规方法中,parent依赖的child引用,通过调用属性方法来传递;IoC实现中,通过byType由IoC容器自动注入parent所依赖的引用。
2. 常规方法除了要创建实例,还要手动传递parent依赖的对象的引用;IoC只需要修改xml配置文件
3. 测试代码中红色标记部分,表示针对两种情况需要改动的代码。IoC明显要简单、灵活、方便。
两种实现方式的不同,可以用下图说明:
![](http://img.my.csdn.net/uploads/201303/07/1362643436_6163.jpg)
一个parent有两个children,一个son和一个daughter。parent每次出去玩的时候,只能带上一个小孩,因此只能轮流地和他们玩。他们的类的实现如下:
抽象类Child:
public abstract class Child { public abstract void play(); }
Child的子类Son:
public class Son extends Child { @Override public void play() { // TODO Auto-generated method stub System.out.println("Parent is playing with the son!"); } }
Child的子类Daughter:
public class Daughter extends Child { @Override public void play() { // TODO Auto-generated method stub System.out.println("Parent is playing with the daughter!"); } }
类parent:
public class Parent { private Child child; public Child getChild() { return child; } public void setChild(Child child) { this.child = child; } public void playWithChild() { child.play(); } }
有如下两种应用场景需求:
情况一:parent带son出去玩
情况二:parent带daughter出去玩
下面比较两种实现方法:
实现方法一:常规实现
import org.junit.Test; import junit.framework.TestCase; public class ParentTest extends TestCase { @Test public void testPlayWithChild() { Parent parent = new Parent(); // 情况一: // 如果Parent和Son玩,则创建Son实例,并调用属性方法传递引用 Son son = new Son(); parent.setChild(son); // 情况二: // 如果Parent和Daughter玩,则创建Daughter实例,并调用属性方法传递引用 // Daughter daughter = new Daughter(); // parent.setChild(daughter); parent.playWithChild(); } }
实现方法二:IoC实现
beans.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-2.5.xsd"> <!-- 情况一:Parent带Son出去玩 --> <bean id="child" class="Son"> </bean> <!-- 情况二:Parent带Daughter出去玩 <bean id="child" class="Daughter"> </bean> --> <bean id="parent" class="Parent" autowire="byType"> </bean> </beans>
JUnit测试程序:
import org.junit.Test; import junit.framework.TestCase; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class ParentTest extends TestCase { @Test public void testPlayWithChild() { ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); Parent parent = (Parent) ctx.getBean("parent"); parent.playWithChild(); } }
两种实现方式的比较:
1. 常规方法中,parent依赖的child引用,通过调用属性方法来传递;IoC实现中,通过byType由IoC容器自动注入parent所依赖的引用。
2. 常规方法除了要创建实例,还要手动传递parent依赖的对象的引用;IoC只需要修改xml配置文件
3. 测试代码中红色标记部分,表示针对两种情况需要改动的代码。IoC明显要简单、灵活、方便。
两种实现方式的不同,可以用下图说明:
![](http://img.my.csdn.net/uploads/201303/07/1362643436_6163.jpg)
相关文章推荐
- Spring学习总结(二)---谈谈对Spring IOC的理解(二:代码实现实例)
- Laravel 服务容器实例教程 —— 深入理解控制反转(IoC)和依赖注入(DI)
- Laravel 服务容器实例教程 —— 深入理解控制反转(IoC)和依赖注入(DI)
- 通过代码实例来理解反转控制IOC
- 深入理解IOC控制反转及应用实例
- Laravel 服务容器实例教程 —— 深入理解控制反转(IoC)和依赖注入(DI)
- 【StrangeIOC框架】 通过一个实例来理解MVCS结构
- Laravel 服务容器实例教程 —— 深入理解控制反转(IoC)和依赖注入(DI)
- Laravel 服务容器实例教程 —— 深入理解控制反转(IoC)和依赖注入(DI)
- laravel 服务容器实例——深入理解IoC模式
- [置顶] Spring IoC容器之---通过实例理解什么是IoC
- Laravel 服务容器实例教程 —— 深入理解控制反转(IoC)和依赖注入(DI)
- 深入理解IOC控制反转及应用实例
- 理解Hibernate的三种实例状态
- 谈谈对Spring IOC的理解
- Spring核心IoC和AOP的理解
- 我理解的IOC技术在Java和C#中比较分析
- IOC的深刻理解 和《两个个很形象的依赖注入的比喻》
- 谈谈对Spring IOC的理解
- 简单的实例来理解WCF 数据服务(WCF DS)