spring--基於配置文件的依賴注入

Spring 依賴注入

  • 經過類構造函數被注入到bean中。所以,控制流經過依賴注入(DI)已經「反轉」
  • 第二種方法是經過 Setter 方法

Spring 基於構造函數的依賴注入

   <bean id="exampleBean" class="examples.ExampleBean">
      <constructor-arg type="int" value="2001"/>
      <constructor-arg type="java.lang.String" value="Zara"/>
   </bean>

 

其中constructor-arg標籤有的屬性值
Name

構造函數中的參數名html

Index

構造函數中的參數序號java

Value

參數值spring

Ref 引用bean
Type:bean 類類型

Spring 基於設值函數的依賴注入

   <bean id="john-classic" class="com.example.Person">
      <property name="name" value="John Doe"/>
      <property name="person" ref="jane"/>
   </bean>
   <bean name="jane" class="com.example.Person">
      <property name="name" value="John Doe"/>
   </bean>

使用 p-namespace 實現 XML 配置:

<?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:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

   <bean id="john-classic" class="com.example.Person"
      p:name="John Doe"
      p:spouse-ref="jane"/>
   </bean>
   <bean name="jane" class="com.example.Person"
      p:name="John Doe"/>
   </bean>

</beans>

注入內部 Beans

<bean id="outerBean" class="...">
 <property name="target"> 
<bean id="innerBean" class="..."/> 
</property> </bean> 

Spring 注入集合

如今若是想傳遞多個值,如 Java Collection 類型 List、Set、Map 和 Properties,Spring 提供了四種類型的集合的配置元素,以下所示:函數

<list>spa

它有助於連線,如注入一列值,容許重複。code

<set>xml

它有助於連線一組值,但不能重複。htm

<map>three

它能夠用來注入名稱-值對的集合,其中名稱和值能夠是任何類型。ci

<props>

它能夠用來注入名稱-值對的集合,其中名稱和值都是字符串類型。

集合在xml文件中的注入方式

<bean id="javaCollection" class="com.tutorialspoint.JavaCollection">

      <property name="addressList">
         <list>
            <value>INDIA</value>
            <value>Pakistan</value>
            <value>USA</value>
            <value>USA</value>
         </list>
      </property>

      <property name="addressSet">
         <set>
            <value>INDIA</value>
            <value>Pakistan</value>
            <value>USA</value>
            <value>USA</value>
        </set>
      </property>

      <property name="addressMap">
         <map>
            <entry key="1" value="INDIA"/>
            <entry key="2" value="Pakistan"/>
            <entry key="3" value="USA"/>
            <entry key="4" value="USA"/>
         </map>
      </property>

      <property name="addressProp">
         <props>
            <prop key="one">INDIA</prop>
            <prop key="two">Pakistan</prop>
            <prop key="three">USA</prop>
            <prop key="four">USA</prop>
         </props>
      </property>

   </bean>

集合注入 Bean 引用:

<bean id="..." class="...">

      <property name="addressList">
         <list>
            <ref bean="address1"/>
            <ref bean="address2"/>
            <value>Pakistan</value>
         </list>
      </property>

      <property name="addressSet">
         <set>
            <ref bean="address1"/>
            <ref bean="address2"/>
            <value>Pakistan</value>
         </set>
      </property>

      <property name="addressMap">
         <map>
            <entry key="one" value="INDIA"/>
            <entry key ="two" value-ref="address1"/>
            <entry key ="three" value-ref="address2"/>
         </map>
      </property>

   </bean>

注入 null 和空字符串的值

若是你須要傳遞一個空字符串做爲值,那麼你能夠傳遞它,以下所示:

<bean id="..." class="exampleBean"> <property name="email" value=""/> </bean>

前面的例子至關於 Java 代碼:exampleBean.setEmail("")。

若是你須要傳遞一個 NULL 值,那麼你能夠傳遞它,以下所示:

<bean id="..." class="exampleBean"> <property name="email"><null/></property> </bean>

Spring Beans 自動裝配

可用於指示 Spring 容器爲來使用自動裝配進行依賴注入。

能夠使用<bean>元素的 autowire 屬性爲一個 bean 定義指定自動裝配模式。

no

默認值,不進行自動裝配,顯式的bean引用來連線。

byName

屬性名自動裝配。 bean 的自動裝配的屬性設置爲 byName,嘗試匹配,而且將它的屬性與在配置文件中被定義爲相同名稱的 beans 的屬性進行鏈接。

byType

數據類型自動裝配。 bean 的自動裝配的屬性設置爲 byType。而後若是它的類型匹配配置文件中的有一個確切的 bean ,它將嘗試匹配和鏈接屬性的類型。若是存在不止一個這樣的 bean,則一個致命的異常將會被拋出。

construct

相似於 byType,但該類型適用於構造函數參數類型。若是在容器中沒有一個構造函數參數類型的 bean,則一個致命錯誤將會發生。

autodetect

Spring首先嚐試經過 constructor 使用自動裝配來鏈接,若是它不執行,Spring 嘗試經過 byType 來自動裝配。

Spring 自動裝配 ‘byName’

根據屬性名和其餘的bean id進行匹配

   <bean id="textEditor" class="com.tutorialspoint.TextEditor">
       <property name="spellChecker" ref="spellChecker" />
       <property name="name" value="Generic Text Editor" />
   </bean>

   <bean id="spellChecker" class="com.tutorialspoint.SpellChecker">
   </bean>

可省略爲下圖

   <bean id="textEditor" class="com.tutorialspoint.TextEditor"
      autowire="byName">
      <property name="name" value="Generic Text Editor" />
   </bean>

   <bean id="spellChecker" class="com.tutorialspoint.SpellChecker">
   </bean>

Spring 自動裝配 ‘byType’

根據屬性class匹配與屬性相同的bean

Spring 由構造函數自動裝配

autowire 屬性設置爲 constructor,自動在容器中尋找id與參數名相同的bean(相似於byType)

相關文章
相關標籤/搜索