Spring_10_Beans 的自動(dòng)裝配

Beans 的自動(dòng)裝配

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

自動(dòng)裝配模式

自動(dòng)裝配:

Spring是受歡迎的企業(yè)級(jí)Java應(yīng)用程序開(kāi)發(fā)框架,數(shù)以百萬(wàn)的來(lái)自世界各地的開(kāi)發(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容器為 來(lái)使用自動(dòng)裝配進(jìn)行依賴注入. 你可以使用元素的autowire屬性為一個(gè)bean定義指定的自動(dòng)裝配模式

模式 描述
no 這個(gè)是默認(rèn)的設(shè)置,它以為著沒(méi)有自動(dòng)裝配,你應(yīng)該使用顯示的bean引用來(lái)連接.你可不用為了連接做特殊的事.
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ù)類型,如果在容器中沒(méi)有一個(gè)構(gòu)造函數(shù)類型的bean,則一個(gè)指明的錯(cuò)誤將會(huì)發(fā)生.
autodetect Spring首先嘗試通過(guò)constructor使用自動(dòng)裝配來(lái)連接,如果它不執(zhí)行,Spring嘗試通過(guò)byType來(lái)自動(dòng)裝配

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

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

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

限制 描述
重寫(xiě)的可能性 你可以使用總是重寫(xiě)自動(dòng)裝配<controller-arg>和<property>設(shè)置來(lái)指定依賴關(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,并且用它來(lái)設(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é)中已說(shuō)明。
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的加載過(guò)程圖解:

img

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

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,并且用它來(lái)設(shè)置這個(gè)屬性.你仍然可以使用<property>標(biāo)簽連接其余屬性.下面的例子說(shuō)明這個(gè)概念,你會(huì)發(fā)現(xiàn)和上面的例子沒(méi)什么區(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è)置通過(guò)構(gòu)造函數(shù)自動(dòng)裝配,而且它帶有一個(gè)SpellChecker類型的參數(shù)之一的構(gòu)造函數(shù),那么Spring就會(huì)查找定義名為SpellChecker的bean,并且用它來(lái)設(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é)中已說(shuō)明.
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)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

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

友情鏈接更多精彩內(nèi)容