Spring_10_Beans 的自動(dòng)裝配

Beans 的自動(dòng)裝配

Spring 容器可以在不使用和元素的清空下自動(dòng)裝配相互協(xié)作的bean之間的關(guān)系,這有助于減少編寫一個(gè)答的基于Spring的應(yīng)用程序的XML配置的數(shù)量.

自動(dòng)裝配模式

自動(dòng)裝配:

Spring是受歡迎的企業(yè)級(jí)Java應(yīng)用程序開發(fā)框架,數(shù)以百萬的來自世界各地的開發(fā)人員都在使用 Spring 框架創(chuàng)建高性能沼琉、易于測(cè)試和可重用的代碼北苟。

自動(dòng)裝配是Spring框架的重要功能,是使用Spring滿足bean依賴的一種方式打瘪,Spring會(huì)在應(yīng)用中為某個(gè)bean尋找其依賴的bean友鼻,Spring中bean有三種裝配機(jī)制,分別是:在xml中顯示配置闺骚、在java中顯示配置以及隱式的bean發(fā)現(xiàn)機(jī)制和自動(dòng)裝配彩扔。

自動(dòng)裝配模式

下列是自動(dòng)裝配模式,他們可以用于指示Spring容器為 來使用自動(dòng)裝配進(jìn)行依賴注入. 你可以使用元素的autowire屬性為一個(gè)bean定義指定的自動(dòng)裝配模式

模式 描述
no 這個(gè)是默認(rèn)的設(shè)置,它以為著沒有自動(dòng)裝配,你應(yīng)該使用顯示的bean引用來連接.你可不用為了連接做特殊的事.
byName 由屬性名自動(dòng)裝配,Spring容器看到在XML配置文件中bean的自動(dòng)裝配的屬性設(shè)置為byName.然后嘗試匹配,并且將它的屬性與在配置文件中被定義為相同名稱的bean的屬性進(jìn)行連接
byType 由屬性類型自動(dòng)裝配.Spring容器看到在XML配置文件中bean的自動(dòng)裝配的屬性為byType.然后如果它的類型匹配配置文件中的一個(gè)確切的bean名稱,它將嘗試匹配連接屬性的類型,如果存在不止一個(gè)這樣的bean,則一個(gè)指明的異常將會(huì)被拋出.
constructor 類似于byType,但該類型適用于構(gòu)造函數(shù)參數(shù)類型,如果在容器中沒有一個(gè)構(gòu)造函數(shù)類型的bean,則一個(gè)指明的錯(cuò)誤將會(huì)發(fā)生.
autodetect Spring首先嘗試通過constructor使用自動(dòng)裝配來連接,如果它不執(zhí)行,Spring嘗試通過byType來自動(dòng)裝配

可以使用byType或者constructor自動(dòng)裝配模式來連接數(shù)組和其他類型的集合.

自動(dòng)裝配的局限性

當(dāng)自動(dòng)裝配始終在同一個(gè)項(xiàng)目中使用時(shí),它的效果最好.如果通常不使用自動(dòng)裝配,它可能會(huì)使開發(fā)人員混淆的使用它來連接只有一個(gè)或者兩個(gè)bean定義.不過,自動(dòng)裝配可以顯著減少需要指定的屬性構(gòu)造器參數(shù),但你應(yīng)該在使用他們之前考慮到自動(dòng)裝配的局限性和缺點(diǎn).

限制 描述
重寫的可能性 你可以使用總是重寫自動(dòng)裝配<controller-arg>和<property>設(shè)置來指定依賴關(guān)系
原始數(shù)據(jù)類型 你不能自動(dòng)渣u哪個(gè)品牌所謂的簡(jiǎn)單類型包括基本類型,字符串和類
混亂的本質(zhì) 自動(dòng)裝配不如顯示裝配精確,所以如果可能的話盡可能的使用顯示裝配.

Spring 自動(dòng)裝配'byName'

這種模式有屬性名稱指定自動(dòng)裝配.Springle容器看做beans,在XML配置文件中beans的auto-wire屬性設(shè)置為byName.然后,它嘗試將它的屬性與配置文件中定義為相同名稱的beans進(jìn)行匹配和連接.如果找到匹配項(xiàng),它將注入這些beans,否則它拋出異常.

例如:

在配置文件中,如果一個(gè)bean定義設(shè)置為自動(dòng)裝配byName,并且它包含SpellChecker屬性(即,它有一個(gè)setSpellChecker()的方法),那么S平均日那個(gè)就會(huì)查找定義名為spellChecker的bean,并且用它來設(shè)置屬性.你仍可以使用<property>標(biāo)簽連接其余的屬性.

步驟 描述
1 創(chuàng)建一個(gè)名稱為 SpringExample 的項(xiàng)目,并且在已創(chuàng)建的項(xiàng)目的 c src 文件夾中創(chuàng)建一個(gè)包 com.tutorialspoint 僻爽。
2 使用 Add External JARs 選項(xiàng)虫碉,添加所需的 Spring 庫(kù),在 Spring Hello World Example 章節(jié)中已說明进泼。
3 在 com.tutorialspoint 包中創(chuàng)建 Java 類 TextEditor 蔗衡, SpellChecker 和 MainApp
4 在src 文件夾中創(chuàng)建 Beans 的配置文件 Beans.xml 。
5 最后一步是創(chuàng)建所有 Java 文件和 Bean 配置文件的內(nèi)容乳绕,并運(yùn)行該應(yīng)用程序绞惦,正如下面解釋的一樣。

TextEditor.java

package com.tutorialspoint;

public class TextEditor {
    private SpellChecker spellChecker;
    private String name;

    public SpellChecker getSpellChecker() {
        return spellChecker;
    }

    public void setSpellChecker(SpellChecker spellChecker) {
        this.spellChecker = spellChecker;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    
    public void spellCheck(){
        spellChecker.checkSpelling();
    }

}

SpellChecker.java

package com.tutorialspoint;

public class SpellChecker {


    public SpellChecker() {
        System.out.println(" SpellChecker     的 構(gòu)造方法");
    }

    public void checkSpelling() {
        System.out.println("SpellChecker 中的  checkSpelling() 方法");
    }

}

MainApp.java

package com.tutorialspoint;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext(
                "Beans.xml");
        TextEditor te = (TextEditor) context.getBean("textEditor");
        te.spellCheck();
    }
}

下面是正常清空下的配置文件

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

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

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

如果使用自動(dòng)裝配"byName" , 那么你的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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

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

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

</beans>

byName的加載過程圖解:

img

圖片中只理解了對(duì)byName的加載,其他的執(zhí)行過程都是一樣的.

Spring 自動(dòng)裝配 byType

這種模式由屬性類型指定自動(dòng)裝配.自動(dòng)裝配.Spring容器看做beans,在XML配置文件中beans的autowire屬性設(shè)置為byType.然后,如果它的type恰好與配置文件中beans名稱中的一個(gè)相匹配,它將嘗試匹配和連接它的屬性.如果找不到匹配項(xiàng),它將注入這些beans,否則,它將拋出異常.

例如:

在配置文件中,如果一個(gè)bean定義設(shè)置為自動(dòng)裝配byType,并且包含SpellChecker類型的spellChecker屬性,那么Spring就會(huì)查找定義名為SpellChecker的bean,并且用它來設(shè)置這個(gè)屬性.你仍然可以使用<property>標(biāo)簽連接其余屬性.下面的例子說明這個(gè)概念,你會(huì)發(fā)現(xiàn)和上面的例子沒什么區(qū)別.除了XML文件的配置發(fā)生了少許的改變.

代碼與上面的byName的代碼一致,但Beans.xml中的類型需要該成byType.

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

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

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

</beans>
img

Spring 由 構(gòu)造函數(shù)自動(dòng)裝配

這種模式與byType非常相似,但它應(yīng)用于構(gòu)造器參數(shù).Spring容器看做beans,在XML配置文件中beans的autowire屬性設(shè)置為 constructot . 然后 , 它嘗試把構(gòu)造函數(shù)的參數(shù)與配置文件中的beans名稱中的一個(gè)進(jìn)行匹配和連接,如果找到匹配項(xiàng),它會(huì)注入這些bean,否則拋出異常.

例如

在配置文件中,如果一個(gè)本定義設(shè)置通過構(gòu)造函數(shù)自動(dòng)裝配,而且它帶有一個(gè)SpellChecker類型的參數(shù)之一的構(gòu)造函數(shù),那么Spring就會(huì)查找定義名為SpellChecker的bean,并且用它來設(shè)置構(gòu)造函數(shù)的參數(shù),你仍然可以使用<constructor-arg>標(biāo)簽連接其余屬性.

案例:

步驟 描述
1 創(chuàng)建一個(gè)名稱為 SpringExample 的項(xiàng)目洋措,并且在已創(chuàng)建的項(xiàng)目的 c src 文件夾中創(chuàng)建一個(gè)包 com.tutorialspoint 济蝉。
2 使用 Add External JARs 選項(xiàng),添加所需的 Spring 庫(kù),在 Spring Hello World Example 章節(jié)中已說明.
3 在 com.tutorialspoint 包中創(chuàng)建 Java 類 TextEditor 王滤, SpellChecker 和 MainApp
4 在src 文件夾中創(chuàng)建 Beans 的配置文件 Beans.xml 贺嫂。
5 最后一步是創(chuàng)建所有 Java 文件和 Bean 配置文件的內(nèi)容,并運(yùn)行該應(yīng)用程序雁乡,正如下面解釋的一樣第喳。

TextEditor.java

package com.tutorialspoint;

public class TextEditor {
    private SpellChecker spellChecker;
    
    private String name;
    

    public TextEditor(SpellChecker spellChecker, String name) {
        this.spellChecker = spellChecker;
        this.name = name;
    }

    public SpellChecker getSpellChecker() {
        return spellChecker;
    }

    public void setSpellChecker(SpellChecker spellChecker) {
        this.spellChecker = spellChecker;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    
    public void spellCheck(){
        spellChecker.checkSpelling();
    }

}

SpellChecker.java

package com.tutorialspoint;

public class SpellChecker {

    public SpellChecker() {
        System.out.println(" SpellChecker     的 構(gòu)造方法");
    }

    public void checkSpelling() {
        System.out.println("SpellChecker 中的  checkSpelling() 方法");
    }

}

MainApp.java

package com.tutorialspoint;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext(
                "Beans.xml");
        TextEditor te = (TextEditor) context.getBean("textEditor");
        te.spellCheck();
    }
}

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

    <bean id="textEditor" class="com.tutorialspoint.TextEditor"
        autowire="constructor">
        <constructor-arg value="Generic Text Editor" />
    </bean>

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

</beans>
img
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市踱稍,隨后出現(xiàn)的幾起案子曲饱,更是在濱河造成了極大的恐慌,老刑警劉巖珠月,帶你破解...
    沈念sama閱讀 217,277評(píng)論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件扩淀,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡啤挎,警方通過查閱死者的電腦和手機(jī)驻谆,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,689評(píng)論 3 393
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來庆聘,“玉大人胜臊,你說我怎么就攤上這事√途酰” “怎么了区端?”我有些...
    開封第一講書人閱讀 163,624評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵值漫,是天一觀的道長(zhǎng)澳腹。 經(jīng)常有香客問我,道長(zhǎng)杨何,這世上最難降的妖魔是什么酱塔? 我笑而不...
    開封第一講書人閱讀 58,356評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮危虱,結(jié)果婚禮上羊娃,老公的妹妹穿的比我還像新娘。我一直安慰自己埃跷,他們只是感情好蕊玷,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,402評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著弥雹,像睡著了一般垃帅。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上剪勿,一...
    開封第一講書人閱讀 51,292評(píng)論 1 301
  • 那天贸诚,我揣著相機(jī)與錄音,去河邊找鬼。 笑死酱固,一個(gè)胖子當(dāng)著我的面吹牛械念,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播运悲,決...
    沈念sama閱讀 40,135評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼龄减,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了班眯?” 一聲冷哼從身側(cè)響起欺殿,我...
    開封第一講書人閱讀 38,992評(píng)論 0 275
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎鳖敷,沒想到半個(gè)月后脖苏,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,429評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡定踱,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,636評(píng)論 3 334
  • 正文 我和宋清朗相戀三年棍潘,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片崖媚。...
    茶點(diǎn)故事閱讀 39,785評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡亦歉,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出畅哑,到底是詐尸還是另有隱情肴楷,我是刑警寧澤,帶...
    沈念sama閱讀 35,492評(píng)論 5 345
  • 正文 年R本政府宣布荠呐,位于F島的核電站赛蔫,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏泥张。R本人自食惡果不足惜呵恢,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,092評(píng)論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望媚创。 院中可真熱鬧渗钉,春花似錦、人聲如沸钞钙。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,723評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)芒炼。三九已至瘫怜,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間焕议,已是汗流浹背戚绕。 一陣腳步聲響...
    開封第一講書人閱讀 32,858評(píng)論 1 269
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留蟹腾,地道東北人挽霉。 一個(gè)月前我還...
    沈念sama閱讀 47,891評(píng)論 2 370
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,713評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理株憾,服務(wù)發(fā)現(xiàn),斷路器晒衩,智...
    卡卡羅2017閱讀 134,654評(píng)論 18 139
  • 1.1 spring IoC容器和beans的簡(jiǎn)介 Spring 框架的最核心基礎(chǔ)的功能是IoC(控制反轉(zhuǎn))容器嗤瞎,...
    simoscode閱讀 6,713評(píng)論 2 22
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 46,808評(píng)論 6 342
  • 1,廢室 我呆愣地面對(duì)著桌上那束殘損的干花听系,那上一個(gè)夏季的遺物——只不過褪去了色彩贝奇,一切曾經(jīng)的生機(jī)都無從尋覓。...
    反光物閱讀 337評(píng)論 0 2
  • 周末同學(xué)聚會(huì)靠胜,有個(gè)同學(xué)A很聊的來掉瞳,深諳修行之道,堅(jiān)持每天打坐靜心浪漠。飯桌上聊到孩子的培養(yǎng)方法陕习,我們每個(gè)同學(xué)都有自己的...
    何魏閱讀 259評(píng)論 1 2