zoukankan      html  css  js  c++  java
  • Spring中的IoC(控制反转)具体是什么东西

      IOC:inverse of Control: 控制反转。

      意思是程序中的之间的关系,不用代码控制,而完全是由容器来控制。在运行阶段,容器会根据配置信息直接把他们的关系注入到组件中。同样,这也是 依赖注入的含义。依赖注入和控制反转其实是一个概念。只不过强调的不同而已,依赖注入强调关系的注入是由容器在运行时完成,而控制反转强调关系是由容器控制。其实本质是一样的。

    贴一段代码

        /** 
         * 学校类 
         */  
        public class School {  
           private String name;  
           public School(String name) {  
               this.name=name;  
           }  
           public void printInfo() {  
               System.out.println("该学校的名称是:"+name);  
           }  
        }  
    /** 
     * 学生类 
     */  
    public class Student {  
      public int id;  
      public String name;  
      private School school;  
    public int getId() {  
        return id;  
    }  
    public void setId(int id) {  
        this.id = id;  
    }  
    public String getName() {  
        return name;  
    }  
    public void setName(String name) {  
        this.name = name;  
    }  
    public School getSchool() {  
        return school;  
    }  
    public void setSchool(School school) {  
        this.school = school;  
    }    
    }
        <?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: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-2.0.xsd  
                   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd  
                   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">  
         <bean id="school" class="ioc.iocsample.School">  
               <constructor-arg index="0">  
                 <value>廊坊师院</value>  
               </constructor-arg>  
         </bean>  
         <bean id="student" class="ioc.iocsample.Student">  
             <property name="id"      value="001"/>  
             <property name="name" value="张三"/>  
             <property name="school"  ref ="school"/>  
         </bean>  
        </beans>  
    import org.springframework.beans.factory.BeanFactory;  
    import org.springframework.context.support.ClassPathXmlApplicationContext;  
      
    public class Client {  
        public static void main(String[] args) {  
            // TODO Auto-generated method stub  
            BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");  
            Student student=(Student)factory.getBean("student");  
            student.getSchool().printInfo();  
        }  
    }  

       spring中的依赖注入DI(dependence injection)共有三种方式:第一种是接口注入(Interface Injection)第二种是get set注入(set/get Injection)第三种是构造器注入(Constructor Injection)

       三种注入方式的区别:

       1.接口注入:组件需要依赖特定接口的实现,其中的加载接口实现和接口实现的具体对象都是由容器来完成。这样,接口必须依赖容器,这样的组件具有侵入性, 降低了重用性。其中如J2EE开发中常用的Context.lookup(ServletContext.getXXX),都是接口注入的表现形式。(这 种注入方式不是常用的)

      

       2.getter/setter方式注入:对于需要注入的东西比较明确。符合java的设计规则。更适合java开发人员,使用起来更加自然,更加方便。

     

       3.构造器方式注入:在类加载的时候,就已经注入依赖的组件。但是若是参数多的话,使用起来不方便。

     

      但是后两种注入方式是spring常用的,而第一种接口注入方式不常用。

  • 相关阅读:
    STL源代码剖析——STL算法之set集合算法
    iOS + Nodejs SSL/Https双向认证
    C语言将10进制转为2进制
    图的遍历算法
    Web—CSS概述
    苹果新的编程语言 Swift 语言进阶(八)--属性
    UVa 10700
    C++实现KMP模式匹配算法
    软件project
    oralce GROUPING SETS
  • 原文地址:https://www.cnblogs.com/wgbs25673578/p/5179810.html
Copyright © 2011-2022 走看看