最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring的自動裝配Bean的三種方式

 更新時間:2017年02月27日 12:00:01   作者:漏斷人初靜v  
本篇文章主要介紹了 Spring的自動裝配Bean的三種方式,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

spring的自動裝配功能的定義:無須在Spring配置文件中描述javaBean之間的依賴關(guān)系(如配置<property>、<constructor-arg>)。IOC容器會自動建立javabean之間的關(guān)聯(lián)關(guān)系。

如果沒有采用自動裝配的話,手動裝配我們通常在配置文件中進行實現(xiàn):一下代碼就是手動裝配:

<beans xmlns="http://www.springframework.org/schema/beans" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation="http://www.springframework.org/schema/beans 
  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> 
 
  <bean id="customerDAO" class="com.hebeu.customer.dao.JdbcCustomerDAO"> 
    <property name="dataSource" ref="dataSource" /> 
  </bean> 
 
</beans> 

通過<property name="dataSource" ref="dataSource" />向customerDAO的bean中注入了dataSource

在Spring框架,可以用 auto-wiring 功能會自動裝配Bean。要啟用它,只需要在 <bean>定義“autowire”屬性。

<bean id="customer" class="com.yiibai.common.Customer" autowire="byName" />

在Spring中,支持 5 自動裝配模式。

  • no – 缺省情況下,自動配置是通過“ref”屬性手動設(shè)定
  • byName – 根據(jù)屬性名稱自動裝配。如果一個bean的名稱和其他bean屬性的名稱是一樣的,將會自裝配它。
  • byType – 按數(shù)據(jù)類型自動裝配。如果一個bean的數(shù)據(jù)類型是用其它bean屬性的數(shù)據(jù)類型,兼容并自動裝配它。
  • constructor – 在構(gòu)造函數(shù)參數(shù)的byType方式。
  • autodetect – 如果找到默認的構(gòu)造函數(shù),使用“自動裝配用構(gòu)造”; 否則,使用“按類型自動裝配”?!驹赟pring3.0以后的版本被廢棄,已經(jīng)不再合法了】

第一種自動裝配【根據(jù)屬性名稱自動裝配】

package com.hebeu.model; 
 
public class Customer { 
 
  private Address address; 
   
  public Customer() { 
     
  } 
   
  public Customer(int id, Address address) { 
    super(); 
    this.address = address; 
  } 
 
  public Address getAddress() { 
    return address; 
  } 
 
  public void setAddress(Address address) { 
    this.address = address; 
  } 
   
} 

package com.hebeu.model; 
 
public class Address { 
 
  private String fulladdress; 
   
  public Address(){ 
     
  } 
   
  public Address(String addr){ 
    this.fulladdress = addr; 
  } 
 
  public String getFulladdress() { 
    return fulladdress; 
  } 
 
  public void setFulladdress(String fulladdress) { 
    this.fulladdress = fulladdress; 
  } 
   
} 

<beans xmlns="http://www.springframework.org/schema/beans" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation="http://www.springframework.org/schema/beans 
  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> 
   
  <bean id="customer" class="com.hebeu.model.Customer" autowire="byName"></bean>
   
  <bean id="address" class="com.hebeu.model.Address"> 
    <property name="fulladdress" value="YiLong Road, CA 188"></property> 
  </bean> 
   
</beans>  

這樣就將address注入到Customer中。這就是自動注入ByName.在Customer bean中公開了一個屬性address,Spring容器會找到address bean,并且裝配。這里必須要注意,Customer中要被注入的bean的set方法要求必須是public的,否則會報空指針異常。還有配置的bean的id必須和Customer中聲明的變量名相同。

第二種自動裝配【根據(jù)數(shù)據(jù)類型自動裝配】

聲明的倆個bean同樣為Customer以及Address,將applicationContext.xml轉(zhuǎn)換為這樣的就是實現(xiàn)根據(jù)數(shù)據(jù)類型進行自動裝配。

<beans xmlns="http://www.springframework.org/schema/beans" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation="http://www.springframework.org/schema/beans 
  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> 
   
  <bean id="customer" class="com.hebeu.model.Customer" <strong><span style="color:#FF0000;">autowire="byType"</span></strong>></bean> 
   
  <bean id="bean1" class="com.hebeu.model.Address"> 
    <property name="fulladdress" value="YiLong Road, CA 188"></property> 
  </bean> 
</beans>  

類型自動裝配的意思是如果一個bean的數(shù)據(jù)類型與其他的bean屬性的數(shù)據(jù)類型相同,將會自動兼容裝配它。當(dāng)然要求只能配置一個某一個類型的bean.如果配置成這樣,那么是會出錯的。

<beans xmlns="http://www.springframework.org/schema/beans" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation="http://www.springframework.org/schema/beans 
  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> 
   
  <bean id="customer" class="com.hebeu.model.Customer" autowire="byType"></bean> 
   
  <bean id="bean1" class="com.hebeu.model.Address"> 
    <property name="fulladdress" value="YiLong Road, CA 188"></property> 
  </bean> 
  <bean id="bean2" class="com.hebeu.model.Address"> 
    <property name="fulladdress" value="YiLong Road, CA 188"></property> 
  </bean> 
</beans>  

第三種自動裝配【根據(jù)構(gòu)造方法自動裝配】

案例同上,將applicationContext.xml轉(zhuǎn)換為如下,就實現(xiàn)了按照構(gòu)造方法自動裝配:

<beans xmlns="http://www.springframework.org/schema/beans" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation="http://www.springframework.org/schema/beans 
  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> 
   
  <bean id="customer" class="com.hebeu.model.Customer"> 
    <!-- 構(gòu)造方法的參數(shù) --> 
    <constructor-arg> 
      <ref bean="bean1"/> 
    </constructor-arg> 
  </bean> 
   
  <bean id="bean1" class="com.hebeu.model.Address"> 
    <property name="fulladdress" value="YiLong Road, CA 188"></property> 
  </bean> 
   
</beans> 

 它實際上是構(gòu)造函數(shù)的參數(shù)類型自動裝配。這意味著如果一個bean的數(shù)據(jù)類型與其他bean的構(gòu)造器參數(shù)的數(shù)據(jù)類型是相同的,那么就自動裝配。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java 中HttpURLConnection附件上傳的實例詳解

    Java 中HttpURLConnection附件上傳的實例詳解

    這篇文章主要介紹了Java 中HttpURLConnection附件上傳的實例詳解的相關(guān)資料,希望通過本文大家能掌握這樣的知識內(nèi)容,需要的朋友可以參考下
    2017-09-09
  • 談?wù)凷pring 注入properties文件總結(jié)

    談?wù)凷pring 注入properties文件總結(jié)

    本篇談?wù)凷pring 注入properties文件總結(jié),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-01-01
  • Java開啟線程的四種方法案例詳解

    Java開啟線程的四種方法案例詳解

    這篇文章主要介紹了Java開啟線程的四種方法,本文結(jié)合實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-02-02
  • Java List的remove()方法踩坑

    Java List的remove()方法踩坑

    Java的List在刪除元素時,一般會用list.remove(o)/remove(i)方法。在使用時,容易觸碰陷阱,本文就來介紹一下容易踩的坑,感興趣的可以了解一下
    2021-10-10
  • springboot2.5.0和redis整合配置詳解

    springboot2.5.0和redis整合配置詳解

    本篇文章向大家介紹springboot2.5.0 整合 redis 配置方法,教大家在pom添加依賴的方法如何調(diào)用redis,本文通過實例代碼給大家介紹的非常詳細,需要的朋友可以參考下
    2021-06-06
  • springboot ehcache 配置使用方法代碼詳解

    springboot ehcache 配置使用方法代碼詳解

    EhCache是一個比較成熟的Java緩存框架,Springboot對ehcache的使用非常支持,所以在Springboot中只需做些配置就可使用,且使用方式也簡易,今天給大家分享springboot ehcache 配置使用教程,一起看看吧
    2021-06-06
  • Java中CyclicBarrier的理解與應(yīng)用詳解

    Java中CyclicBarrier的理解與應(yīng)用詳解

    這篇文章主要介紹了Java中CyclicBarrier的理解與應(yīng)用詳解,CyclicBarrier類是JUC框架中的工具類,也是一個同步輔助裝置:允許多個線程去等待直到全部線程抵達了公共的柵欄點,需要的朋友可以參考下
    2023-12-12
  • Springboot指定掃描路徑的實現(xiàn)示例

    Springboot指定掃描路徑的實現(xiàn)示例

    本文主要介紹了Springboot指定掃描路徑的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-05-05
  • 基于Java編寫簡單的Excel工具類

    基于Java編寫簡單的Excel工具類

    這篇文章主要為大家詳細介紹了如何基于Java編寫簡單的Excel工具類,文中的示例代碼講解詳細,具有一定的借鑒價值,有需要的小伙伴可以參考下
    2024-02-02
  • Java 內(nèi)部類的定義與范例

    Java 內(nèi)部類的定義與范例

    說起內(nèi)部類這個詞,想必很多人都不陌生,但是又會覺得不熟悉。原因是平時編寫代碼時可能用到的場景不多,用得最多的是在有事件監(jiān)聽的情況下,并且即使用到也很少去總結(jié)內(nèi)部類的用法。今天我們就來一探究竟
    2021-11-11

最新評論

甘肃省| 遵义市| 庆安县| 庐江县| 喀什市| 西城区| 越西县| 景泰县| 林口县| 青冈县| 武乡县| 禄劝| 桃源县| 宜兰市| 崇仁县| 遵义市| 平南县| 永寿县| 时尚| 富裕县| 延边| 思茅市| 察隅县| 吉木萨尔县| 嘉禾县| 兴国县| 西乌珠穆沁旗| 镇赉县| 柳河县| 鸡东县| 青铜峡市| 晋中市| 施甸县| 宁都县| 常熟市| 珠海市| 通河县| 五台县| 宜都市| 铁力市| 宜章县|