詳解Java的Spring框架下bean的自動裝載方式
Spring容器可以自動裝配相互協(xié)作bean之間的關(guān)系,這有助于減少對XML配置,而無需編寫一個大的基于Spring應(yīng)用程序的較多的<constructor-arg>和<property>元素。
自動裝配模式:
有下列自動裝配模式,可用于指示Spring容器使用自動裝配依賴注入。使用<bean/>元素的autowire屬性為一個bean定義中指定自動裝配模式。
byName模式
這種模式規(guī)定由自動裝配屬性名稱。Spring容器在外觀上自動線屬性設(shè)置為byName的XML配置文件中的bean。然后,它嘗試匹配和接線其屬性與配置文件中相同的名稱定義的Bean。如果找到匹配項,它會注入這些bean,否則,它會拋出異常。
例如,如果一個bean定義設(shè)置為自動裝配byName的配置文件,它包含aspellChecker屬性(即,它有一個 setSpellChecker(...)方法),Spring就會查找名為拼寫檢查一個bean定義,并用它來設(shè)置該屬性。仍然可以使用的<property>標(biāo)簽連線其余屬性。下面的例子將說明這個概念。
來創(chuàng)建一個Spring應(yīng)用程序:
這里是TextEditor.java文件的內(nèi)容:
package com.yiibai; public class TextEditor { private SpellChecker spellChecker; private String name; public void setSpellChecker( SpellChecker spellChecker ){ this.spellChecker = spellChecker; } public SpellChecker getSpellChecker() { return spellChecker; } public void setName(String name) { this.name = name; } public String getName() { return name; } public void spellCheck() { spellChecker.checkSpelling(); } }
下面是另外一個相關(guān)的類文件SpellChecker.java內(nèi)容:
package com.yiibai; public class SpellChecker { public SpellChecker() { System.out.println("Inside SpellChecker constructor." ); } public void checkSpelling() { System.out.println("Inside checkSpelling." ); } }
以下是MainApp.java文件的內(nèi)容:
package com.yiibai; 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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <!-- Definition for textEditor bean --> <bean id="textEditor" class="com.yiibai.TextEditor"> <property name="spellChecker" ref="spellChecker" /> <property name="name" value="Generic Text Editor" /> </bean> <!-- Definition for spellChecker bean --> <bean id="spellChecker" class="com.yiibai.SpellChecker"> </bean> </beans>
但是,如果要使用自動裝配“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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <!-- Definition for textEditor bean --> <bean id="textEditor" class="com.yiibai.TextEditor" autowire="byName"> <property name="name" value="Generic Text Editor" /> </bean> <!-- Definition for spellChecker bean --> <bean id="spellChecker" class="com.yiibai.SpellChecker"> </bean> </beans>
創(chuàng)建源代碼和bean配置文件完成后,讓我們運行應(yīng)用程序。如果一切順利,這將打印以下信息:
Inside SpellChecker constructor. Inside checkSpelling.
byType模式
式規(guī)定由自動裝配屬性類型。Spring容器在外觀上autowire屬性設(shè)置為byType的XML配置文件中的bean。然后,它嘗試匹配和連接一個屬性,如果它的類型有完全相同的豆子名稱的一個匹配的配置文件。如果找到匹配項,它會注入這些bean,否則,它會拋出異常。
例如,如果一個bean定義設(shè)置為自動裝配byType的配置文件,它包含拼寫檢查類型的aspellChecker屬性,春季查找名為拼寫檢查一個bean定義,并用它來設(shè)置該屬性。仍然可以使用<property>標(biāo)簽接線其余屬性。下面的例子將說明這個概念,會發(fā)現(xiàn)和上面的例子沒有什么區(qū)別,除了XML配置文件已被更改。
同樣,來創(chuàng)建一個Spring應(yīng)用程序說明:
這里是TextEditor.java文件的內(nèi)容:
package com.yiibai; public class TextEditor { private SpellChecker spellChecker; private String name; public void setSpellChecker( SpellChecker spellChecker ) { this.spellChecker = spellChecker; } public SpellChecker getSpellChecker() { return spellChecker; } public void setName(String name) { this.name = name; } public String getName() { return name; } public void spellCheck() { spellChecker.checkSpelling(); } }
下面是另外一個相關(guān)的類文件SpellChecker.java內(nèi)容:
package com.yiibai; public class SpellChecker { public SpellChecker(){ System.out.println("Inside SpellChecker constructor." ); } public void checkSpelling() { System.out.println("Inside checkSpelling." ); } }
以下是MainApp.java文件的內(nèi)容:
package com.yiibai; 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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <!-- Definition for textEditor bean --> <bean id="textEditor" class="com.yiibai.TextEditor"> <property name="spellChecker" ref="spellChecker" /> <property name="name" value="Generic Text Editor" /> </bean> <!-- Definition for spellChecker bean --> <bean id="spellChecker" class="com.yiibai.SpellChecker"> </bean> </beans>
但是,如果要使用自動裝配“byType”,那么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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <!-- Definition for textEditor bean --> <bean id="textEditor" class="com.yiibai.TextEditor" autowire="byType"> <property name="name" value="Generic Text Editor" /> </bean> <!-- Definition for spellChecker bean --> <bean id="SpellChecker" class="com.yiibai.SpellChecker"> </bean> </beans>
當(dāng)創(chuàng)建源代碼和bean配置文件完成后,讓我們運行應(yīng)用程序。如果一切順利,這將打印以下信息:
Inside SpellChecker constructor. Inside checkSpelling.
由構(gòu)造函數(shù)自動裝配
這種模式是非常相似byType,但它應(yīng)用于構(gòu)造器參數(shù)。 Spring容器在外觀上autowire屬性被設(shè)置XML配置文件中bean的。然后,它嘗試匹配和連線它的構(gòu)造函數(shù)的參數(shù)與bean名稱的配置文件只有一個。如果找到匹配項,它會注入這些bean,否則,它會拋出異常。
例如,如果一個bean定義設(shè)置為通過構(gòu)造配置文件自動裝配,它具有與拼寫檢查類型的參數(shù)之一的構(gòu)造函數(shù),春天尋找一個bean定義namedSpellChecker,并用它來設(shè)置構(gòu)造函數(shù)的參數(shù)。仍然可以使用<constructor-arg>標(biāo)簽連線剩余的參數(shù)。下面的例子將說明這個概念。
這里是TextEditor.java文件的內(nèi)容:
package com.yiibai; 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 String getName() { return name; } public void spellCheck() { spellChecker.checkSpelling(); } }
下面是另外一個相關(guān)的類文件SpellChecker.java內(nèi)容:
package com.yiibai; public class SpellChecker { public SpellChecker(){ System.out.println("Inside SpellChecker constructor." ); } public void checkSpelling() { System.out.println("Inside checkSpelling." ); } }
以下是MainApp.java文件的內(nèi)容:
package com.yiibai; 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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <!-- Definition for textEditor bean --> <bean id="textEditor" class="com.yiibai.TextEditor"> <constructor-arg ref="spellChecker" /> <constructor-arg value="Generic Text Editor"/> </bean> <!-- Definition for spellChecker bean --> <bean id="spellChecker" class="com.yiibai.SpellChecker"> </bean> </beans>
但是,如果要使用由“構(gòu)造函數(shù)”自動裝配,那么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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <!-- Definition for textEditor bean --> <bean id="textEditor" class="com.yiibai.TextEditor" autowire="constructor"> <constructor-arg value="Generic Text Editor"/> </bean> <!-- Definition for spellChecker bean --> <bean id="SpellChecker" class="com.yiibai.SpellChecker"> </bean> </beans>
創(chuàng)建源代碼和bean配置文件完成后,讓我們運行應(yīng)用程序。如果一切順利,這將打印以下信息:
Inside SpellChecker constructor. Inside checkSpelling.
除此之外,還有autodetect和默認(rèn)方式,這里就不再細(xì)講。
自動裝配的局限性:
自動裝配最好效果是它始終在一個項目中使用。如果自動裝配不一般的使用,它可能會被混淆為開發(fā)人員可以使用它來連接只有一個或兩個bean定義。不過,自動裝配可以顯著減少需要指定屬性或構(gòu)造器參數(shù),但你應(yīng)該使用它們之前考慮自動裝配的局限性和缺點。
相關(guān)文章
springcloud引入spring-cloud-starter-openfeign失敗的解決
這篇文章主要介紹了springcloud?引入spring-cloud-starter-openfeign失敗的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03java學(xué)習(xí)之jar包的下載和導(dǎo)入
我們經(jīng)常碰到有些jar包在中央倉庫沒有的情況,這時候我們需要導(dǎo)入,這篇文章主要給大家介紹了關(guān)于java學(xué)習(xí)之jar包的下載和導(dǎo)入的相關(guān)資料,需要的朋友可以參考下2023-06-06詳解SpringBoot獲得Maven-pom中版本號和編譯時間戳
這篇文章主要介紹了詳解SpringBoot獲得Maven-pom中版本號和編譯時間戳,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01解決IDEA集成Docker插件后出現(xiàn)日志亂碼的問題
這篇文章主要介紹了解決IDEA集成Docker插件后出現(xiàn)日志亂碼的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11