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>
  • 相关阅读:
    Samba配置文件常用参数详解-OK
    SVN Server配置详解 及备份
    secure crt 基本设置***
    ultraedit15.00.0.1046注册码
    makefile中的shell语法 ***
    Spring (一) IOC ( Inversion Of Control )
    软件开发过程中会出来的几个版本
    poj3070
    Python基础 3----文件和网络
    HDU 4720 Naive and Silly Muggles (外切圆心)
  • 原文地址:https://www.cnblogs.com/kakaisgood/p/6136532.html
Copyright © 2011-2022 走看看