Beans 的自動(dòng)裝配
Spring 容器可以在不使用和元素的清空下自動(dòng)裝配相互協(xié)作的bean之間的關(guān)系,這有助于減少編寫一個(gè)答的基于Spring的應(yīng)用程序的XML配置的數(shù)量.
自動(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的加載過程圖解:
圖片中只理解了對(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>
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>