Spring-IOC

控制反转(Inversion ofControl,英文缩写为IoC)是一种可以解耦的方法,不是什么技术,是一种思想,也是轻量级的Spring框架的核心。控制反转一般分为两种类型,依赖注入(DependencyInjection,简称DI)和依赖查找。控制反转是,关于一个对象如何获取他所依赖的对象的引用,这个责任的反转。

 

我们通过一个例子体会IoC的好处:

数据模型如下:

Human接口:

  1. package sping.tgb.ioc;  
  2.   
  3. public interface Human {  
  4.   
  5.     public void eat();  
  6.     public void sleep();  
  7.       
  8. }  

Student类:


  1. package sping.tgb.ioc;  
  2.   
  3. public class Student implements Human {  
  4.   
  5.   
  6.     public void eat() {  
  7.         System.out.println("学生吃饭");  
  8.           
  9.     }  
  10.   
  11.   
  12.     public void sleep() {  
  13.         System.out.println("学生睡觉");  
  14.           
  15.     }  
  16.   
  17. }  

Teacher类:


  1. package sping.tgb.ioc;  
  2.   
  3. public class Teacher implements Human {  
  4.   
  5.   
  6.     public void eat() {  
  7.         System.out.println("老师吃饭");  
  8.           
  9.     }  
  10.   
  11.   
  12.     public void sleep() {  
  13.         System.out.println("老师睡觉");  
  14.           
  15.     }  
  16.   
  17. }  

 

用工厂模式:

   

   我们如果要在客户端调用这两个类,不用控制反转的话,可以用工厂实现,如下:

工厂类:

  1. package sping.tgb.ioc;  
  2.   
  3. public class Factory {  
  4.   
  5.     public final static String TEACHER = "teacher";  
  6.     public final static String STUDENT = "student";  
  7.   
  8.     public Human getHuman(String ethnic) {  
  9.         if (ethnic.equals(TEACHER))  
  10.             return new Teacher();  
  11.         else if (ethnic.equals(STUDENT))  
  12.             return new Student();  
  13.         else  
  14.             throw new IllegalArgumentException("参数(职业)错误");  
  15.     }  
  16. }  
客户端:
  1. public static void main(String[] args) {  
  2.       
  3.         Human human =null;  
  4.           
  5.         human=new Factory().getHuman(Factory.STUDENT);  
  6.         human.eat();  
  7.         human.sleep();  
  8.           
  9.         human=new Factory().getHuman(Factory.TEACHER);  
  10.         human.eat();  
  11.         human.sleep();  
  12.   
  13.     }  

输出结果:



 Spring

      用工厂我们做到了解耦和,客户端不用知道具体的调用类,由工厂判断。下面我们看一下用控制反转我们如何做:

       使用spring,我们就不用写工厂类,直接写配置文件,如下:

客户端:

  1. public static void main(String[] args) {  
  2.       
  3.         BeanFactory beanFactory=null;  
  4.         beanFactory=new ClassPathXmlApplicationContext("applicationContext.xml");  
  5.           
  6.         Human human = null;  
  7.         human = (Human) beanFactory.getBean("student");  
  8.         human.eat();  
  9.         human.sleep();  
  10.         human = (Human) beanFactory.getBean("teacher");  
  11.         human.eat();  
  12.         human.sleep();  
  13.   
  14.     }  

输出结果:


对比:

      大家可以看到用IoC和用工厂的作用基本是一致的,那为什么还要用IoC呢?优势在哪里?

 

     Spring做到了解耦,上一层不依赖下一层的具体类,同样,工厂也做到了。但是用Spring当需求发生变化时只要修改配置文件,不用重新编译系统;而用工厂当需求发生变化需要增加或删除、修改类时,需要重新编译。这样可以说Spring做到了热插拔。

 

总结:

 

      IoC的灵活性是有代价的:设置步骤麻烦、生成对象的方式不直观、反射比正常生成对象在效率上慢一点。因此并不是任何地方都适合用IoC的,用到工厂模式的地方都可以考虑用IoC模式。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值