Spring IOC/DI- 3 different types

理論:spring

IOC(Inversion of Control控制反轉)spring-mvc

DI(依賴注入) Dependency Injectionmvc

 

 它不是一種技術而是一種思想。當初IOC理論的提出就是爲了解決對象之間的「解耦」。在傳統的建立對象的方式中咱們直接在對象內部經過new進行建立對象,是程序主動去建立依賴對象;而如今IOC是有專門一個容器來建立這些對象,即由IOC容器來控制對象的建立,並將依賴對象注入給調用者,這樣就產生爲了「依賴注入」的概念,也就是"DI"。spa

 

 

  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>
相關文章
相關標籤/搜索