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>
  • 相关阅读:
    LeetCode 566 重塑矩阵
    LeetCode 283 移动零
    C++Template(类模板二)
    Qt之简单绘图实现
    QT控件之QSlider
    Redis
    布局总结三: icon图标+标题上下两排排列
    vue中在data中引入图片的路径方法
    布局总结二:宽高比固定比例---移动端
    在vue中使用vue-awesome-swiper插件
  • 原文地址:https://www.cnblogs.com/kakaisgood/p/6136532.html
Copyright © 2011-2022 走看看