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>
  • 相关阅读:
    onmouseover事件
    ref:ThinkPHP Builder.php SQL注入漏洞(<= 3.2.3)
    ref:mysql命令大全
    ref:mysql丢失密码,如何修改?
    ref:学习笔记 UpdateXml() MYSQL显错注入
    转:[译]CSV 注入:被人低估的巨大风险
    转:深入剖析 JavaScriptCore
    转:Exploiting Electron RCE in Exodus wallet
    转:LNMP虚拟主机PHP沙盒绕过/命令执行(php exec命令被禁之后)
    转:Ubuntu16.04下配置php+vscode+xdebug开发环境
  • 原文地址:https://www.cnblogs.com/kakaisgood/p/6136532.html
Copyright © 2011-2022 走看看