zoukankan      html  css  js  c++  java
  • Spring IOC/DI- 3 different types

    理论:

    IOC(Inversion of Control控制反转)

    DI(依赖注入) Dependency Injection

     

     它不是一种技术而是一种思想。当初IOC理论的提出就是为了解决对象之间的“解耦”。在传统的创建对象的方式中我们直接在对象内部通过new进行创建对象,是程序主动去创建依赖对象;而现在IOC是有专门一个容器来创建这些对象,即由IOC容器来控制对象的创建,并将依赖对象注入给调用者,这样就产生为了“依赖注入”的概念,也就是"DI"。

     

    1. Injection based on Annotation
      <?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:context="http://www.springframework.org/schema/context"
          xmlns:mvc="http://www.springframework.org/schema/mvc"
          xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
              http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
              http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">                    
      <!-- 以上是全部Spring都一样的配置文件 、beans是根元素-->
          <bean id="stoneAxe" class="com.StoneAxe"></bean>
      <!-- 下面是根据注解方式进行注入 -->
         <context:annotation-config/>
          <bean id="chinese" class="com.Chinese"/> 
      </beans>
      public class Chinese implements Person {
          @Autowired 
          private Axe axe;
      }
    2. Injection base on set method
      <?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:context="http://www.springframework.org/schema/context"
          xmlns:mvc="http://www.springframework.org/schema/mvc"
          xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
              http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
              http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">                    
      
      <!-- 以上是全部Spring都一样的配置文件 、beans是根元素-->
          <bean id="stoneAxe" class="com.StoneAxe"></bean>
      
      <!-- 下面是根据属性注入,也就是set方法注入 -->
          <bean id="chinese" class="com.Chinese">
             <property name="axe" ref="stoneAxe"/>
          </bean>
          
      </beans>
    3. 3. Injection base on constructor.
    <?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:context="http://www.springframework.org/schema/context"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">                    
    
    <!-- 以上是全部Spring都一样的配置文件 、beans是根元素-->
        <bean id="stoneAxe" class="com.StoneAxe"></bean>
    
    
     <!-- 下面是根据构造器注入   -->
    
       <bean id="chinese" class="com.Chinese">
          <constructor-arg ref="stoneAxe"/>
       </bean>    
     
    </beans>
  • 相关阅读:
    《Head First 设计模式》 第八、九章 模板方法模式、迭代器模式、组合模式
    《Head First 设计模式》 第十、十一章 状态模式、代理模式
    《Head First 设计模式》 第六、七章 命令模式、适配器模式、外观模式
    《Head First 设计模式》 第四、五章 工厂模式、单例模式
    敏捷软件开发 第十章、第十一章、第十二章
    《Head First 设计模式》 第二、三章 观察者模式、装饰者模式
    二叉树-面试题27-二叉树的镜像
    堆栈-155-最小栈
    堆栈-20-有效括号
    java编程基础(六)----泛型
  • 原文地址:https://www.cnblogs.com/kakaisgood/p/6136532.html
Copyright © 2011-2022 走看看