Spring 构造函数自动装配
此模式与 byType 非常相似,但它适用于构造函数参数。 Spring 容器查看在 XML 配置文件中设置了 autowire 属性的 bean。 然后,它会尝试将其构造函数的参数与配置文件中的一个 bean 名称进行匹配并连接。 如果找到匹配项,它将注入这些 bean。 否则,bean 将不会被连接。
例如,如果配置文件中的 constructor 将 bean 定义设置为 autowire,并且它有一个带有 SpellChecker 类型参数的构造函数,则 Spring 会查找 bean 定义名为 SpellChecker,并使用它来设置构造函数的参数。 您仍然可以使用 <constructor-arg> 标签连接剩余的参数。 以下示例将说明该概念。
让我们有一个可以工作的 Eclipse IDE,并按照以下步骤创建一个 Spring 应用程序 −
| 步骤 | 描述 |
|---|---|
| 1 | 创建一个名为 SpringExample 的项目,并在创建的项目中的 src 文件夹下创建一个包 com.tutorialspoint。 |
| 2 | 使用 Add External JARs 选项添加所需的 Spring 库,如 Spring Hello World 示例 一章中所述。 |
| 3 | 在 com.tutorialspoint 包下创建 Java 类 |
| 4 | 在 src 文件夹下创建 Beans 配置文件 Beans.xml。 |
| 5 | 最后一步是创建所有 Java 文件和 Bean 配置文件的内容,然后运行应用程序,如下所述。 |
这是 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 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/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- Definition for textEditor bean -->
<bean id = "textEditor" class = "com.tutorialspoint.TextEditor">
<constructor-arg ref = "spellChecker" />
<constructor-arg value = "Generic Text Editor"/>
</bean>
<!-- Definition for spellChecker bean -->
<bean id = "spellChecker" class = "com.tutorialspoint.SpellChecker"></bean>
</beans>
但是如果你要使用'by constructor'按构造函数自动装配,那么你的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.tutorialspoint.TextEditor"
autowire = "constructor">
<constructor-arg value = "Generic Text Editor"/>
</bean>
<!-- Definition for spellChecker bean -->
<bean id = "SpellChecker" class = "com.tutorialspoint.SpellChecker"></bean>
</beans>
完成创建源和 bean 配置文件后,让我们运行应用程序。 如果您的应用程序一切正常,它将打印以下消息 −
Inside SpellChecker constructor. Inside checkSpelling.

