javascript
Spring 自动装配 ‘byName’
轉載自??Spring 自動裝配 ‘byName’
Spring 自動裝配 ‘byName’
這種模式由屬性名稱指定自動裝配。Spring 容器看作 beans,在 XML 配置文件中 beans 的?auto-wire?屬性設置為?byName。然后,它嘗試將它的屬性與配置文件中定義為相同名稱的 beans 進行匹配和連接。如果找到匹配項,它將注入這些 beans,否則,它將拋出異常。
例如,在配置文件中,如果一個 bean 定義設置為自動裝配?byName,并且它包含?spellChecker?屬性(即,它有一個?setSpellChecker(...)?方法),那么 Spring 就會查找定義名為?spellChecker?的 bean,并且用它來設置這個屬性。你仍然可以使用 <property> 標簽連接其余的屬性。下面的例子將說明這個概念。
讓我們在恰當的位置使用 Eclipse IDE,然后按照下面的步驟來創建一個 Spring 應用程序:
| 1 | 創建一個名稱為?SpringExample?的項目,并且在已創建的項目的?src?文件夾中創建一個包?com.tutorialspoint。 |
| 2 | 使用?Add External JARs?選項,添加所需的 Spring 庫,在?Spring Hello World Example?章節中已說明。 |
| 3 | 在?com.tutorialspoint?包中創建 Java 類?TextEditor,SpellChecker?和?MainApp。 |
| 4 | 在?src?文件夾中創建 Beans 的配置文件?Beans.xml。 |
| 5 | 最后一步是創建所有 Java 文件和 Bean 配置文件的內容,并運行該應用程序,正如下面解釋的一樣。 |
這里是?TextEditor.java?文件的內容:
package com.tutorialspoint; 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();} }下面是另一個依賴類文件?SpellChecker.java?的內容:
package com.tutorialspoint; public class SpellChecker {public SpellChecker() {System.out.println("Inside SpellChecker constructor." );}public void checkSpelling() {System.out.println("Inside 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"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd"><!-- Definition for textEditor bean --><bean id="textEditor" class="com.tutorialspoint.TextEditor"><property name="spellChecker" ref="spellChecker" /><property name="name" value="Generic Text Editor" /></bean><!-- Definition for spellChecker bean --><bean id="spellChecker" class="com.tutorialspoint.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/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd"><!-- Definition for textEditor bean --><bean id="textEditor" class="com.tutorialspoint.TextEditor" autowire="byName"><property name="name" value="Generic Text Editor" /></bean><!-- Definition for spellChecker bean --><bean id="spellChecker" class="com.tutorialspoint.SpellChecker"></bean></beans>一旦你完成了創建源代碼和 bean 的配置文件,我們就可以運行該應用程序。如果你的應用程序一切都正常,它將打印下面的消息:
Inside SpellChecker constructor. Inside checkSpelling.總結
以上是生活随笔為你收集整理的Spring 自动装配 ‘byName’的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring 注入集合
- 下一篇: Pamir Web Host为任何规模的