Spring Dependency Injection 简明教程
Spring DI - Autowiring ByName
该模式指定按属性名称自动注入。Spring 容器查看在 XML 配置文件中将 auto-wire 属性设置为 byName 的 bean。然后,它尝试匹配并将其属性与配置中由相同名称定义的 bean 进行关联。如果找到匹配项,它将注入这些 bean。否则,将不关联 bean。
This mode specifies autowiring by property name. Spring container looks at the beans on which auto-wire attribute is set to byName in the XML configuration file. It then tries to match and wire its properties with the beans defined by the same names in the configuration file. If matches are found, it will inject those beans. Otherwise, bean(s) will not be wired.
例如,如果将 bean 定义设置为在配置按 byName 自动关联,并且它包含一个 spellChecker 属性(也就是说,它有一个 setSpellChecker(…)方法),Spring 将寻找名为 spellChecker 的 bean 定义,并使用它设置该属性。您仍然可以使用 <property> 标记关联剩余的属性。以下示例将说明该概念。
For example, if a bean definition is set to autowire byName in the configuration file, and it contains a spellChecker property (that is, it has a setSpellChecker(…)method), Spring looks for a bean definition named spellChecker, and uses it to set the property. Still you can wire the remaining properties using <property> tags. The following example will illustrate the concept.
Example
以下示例展示了 TextEditor 类,它只能使用纯基于 setter 的注入进行依赖项注入。
The following example shows a class TextEditor that can only be dependency-injected using pure setter-based injection.
让我们更新在 Spring DI - Create Project 章节中创建的项目。我们将添加以下文件 −
Let’s update the project created in Spring DI - Create Project chapter. We’re adding following files −
-
TextEditor.java − A class containing a SpellChecker as dependency.
-
SpellChecker.java − A dependency class.
-
MainApp.java − Main application to run and test.
以下为 TextEditor.java 文件的内容 −
Here is the content of TextEditor.java file −
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 的内容 −
Following is the content of another dependent class file 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 文件的内容−
Following is the content of the MainApp.java file −
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("applicationcontext.xml");
TextEditor te = (TextEditor) context.getBean("textEditor");
te.spellCheck();
}
}
下面是配置了自动装配的配置 applicationcontext.xml −
Following is the configuration file applicationcontext.xml which has configuration for autowiring byName −
<?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.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>