Android Spring依賴注入(1)

來源:互聯網
上載者:User

使用構造器注入

使用屬性setter方法注入

使用Field注入用於註解方式)

注入依賴對象可以採用手工裝配或自動裝配,在實際應用中建議使用手工裝配,因為自動裝配會產生未知情況,開發人員無法預見最終的裝配結果。

1.手工裝配依賴對象

手工裝配依賴對象,在這種方式中又有兩種編程方式

在xml設定檔中,通過在bean節點下配置

在java代碼中使用@Autowired或@Resource註解方式進行裝配

依賴注入--手工裝配--XML方式

通過setter方法注入依賴

<bean>元素的< property >子項目指明了使用它們的set方法來注入。可以注入任何東西,從基本類型到集合類,甚至是應用系統的bean。

通過setter方法注入依賴

簡單bean配置

配置bean的簡單屬性,基礎資料型別 (Elementary Data Type)和String。

  1. <beanidbeanid="personService" class="com.test.bean.impl.PersonServiceImpl"> 
  2. <!-- 基本類型,string類型 --> 
  3. <propertynamepropertyname="age"value="20"></property> 
  4. <propertynamepropertyname="name" value="張無忌"></property>                         
  5. </bean> 

通過setter方法注入依賴

引用其它bean

  1. <beanidbeanid="person"class="com.test.bean.Person" /> 
  2. <beanidbeanid="personService"   
  3. class="com.test.bean.impl.PersonServiceImpl"> 
  4. <!-- 參考型別 --> 
  5. <propertynamepropertyname="person" ref="person" /> 
  6. </bean> 

內部bean

  1. <beanidbeanid="personService"class="com.test.bean.impl.PersonServiceImpl"> 
  2. <!-- 內部bean注入 --> 
  3. <propertynamepropertyname="personClass"> 
  4. <beanclassbeanclass="com.test.bean.PersonClass" /> 
  5. </propert> 
  6. </bean> 

這種方式的缺點是你無法在其它地方重用這個personClass執行個體,原因是它是專門為personService而用。

裝配集合

若bean的屬性是集合類型,按如下處理:

A、裝配List和數組:

  1. <!-- 裝配list --> 
  2. <propertynamepropertyname="lists"> 
  3.   <list> 
  4.     <value>list1</value> 
  5.     <value>list2</value> 
  6.     <refbeanrefbean="person"/> 
  7.   </list> 
  8. </property> 
  9. <!--裝配數組 --> 
  10. <property name="obj"> 
  11.   <list> 
  12.     <value>obj1</value> 
  13.     <value>obj2</value> 
  14.     <refbeanrefbean="person"/> 
  15.   </list> 
  16. </property> 

B、 裝配set:

  1. <!--裝配set --> 
  2. <property name="sets"> 
  3.    <set> 
  4.     <value>set1</value> 
  5.     <value>set2</value> 
  6.     <refbeanrefbean="person"/> 
  7.   </set> 
  8. </property> 

set使用方法和list一樣,不同的是對象被裝配到set中,而list是裝配到List或數組中裝配。

裝配集合

C、裝配map:

  1. <!-- 裝配map--> 
  2. <propertynamepropertyname="maps"> 
  3. <map> 
  4. <entrykeyentrykey="01"> 
  5. <value>map01</value> 
  6. </entry> 
  7. <entrykeyentrykey="02"> 
  8. <value>map02</value> 
  9. </entry> 
  10. </map> 
  11. </property> 

map中的<entry>的數值和<list>以及<set>的一樣,可以使任何有效屬性元素,需要注意的是key值必須是String的。

D、裝配Properties:

  1. <!--裝配Properties  --> 
  2. <property name="props"> 
  3. <props> 
  4. <prop key="01">prop1</prop> 
  5. <prop key="02">prop2</prop> 
  6. </props> 
  7. </property> 

E、設定null:

  1. <!--裝配null --> 
  2. <property name="listnull"> 
  3. <null/> 
  4. </property> 

通過參數的順序:

  1. <constructor-argindexconstructor-argindex="0"> 
  2. <value>張三</value> 
  3. </constructor-arg> 
  4. <constructor-argindexconstructor-argindex="1"> 
  5. <value>56</value> 
  6. </constructor-arg> 


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.