- Spring DI 教程
- Spring DI - 首页
- Spring DI - 概述
- Spring DI - 环境设置
- Spring DI - IOC 容器
- Spring 依赖注入
- Spring DI - 创建项目
- 基于构造函数的注入示例
- Spring DI - 基于构造函数
- Spring DI - 内部 Bean 构造函数
- Spring DI - 集合构造函数
- Spring DI - 集合引用构造函数
- Spring DI - Map 构造函数
- Spring DI - Map 引用构造函数
- 基于 Setter 的注入示例
- Spring DI - 基于 Setter
- Spring DI - 内部 Bean Setter
- Spring DI - 集合 Setter
- Spring DI - 集合引用 Setter
- Spring DI - Map Setter
- Spring DI - Map 引用 Setter
- 自动装配示例
- Spring DI - 自动装配
- Spring DI - 按名称自动装配
- Spring DI - 按类型自动装配
- Spring DI - 构造函数自动装配
- 工厂方法
- Spring DI - 静态工厂
- Spring DI - 非静态工厂
- Spring DI 有用资源
- Spring DI - 快速指南
- Spring DI - 有用资源
- Spring DI - 讨论
Spring DI - 按名称自动装配
此模式指定按属性名称自动装配。Spring 容器查找 XML 配置文件中 auto-wire 属性设置为 byName 的 Bean。然后,它尝试将这些 Bean 的属性与配置文件中定义的同名 Bean 进行匹配和连接。如果找到匹配项,它将注入这些 Bean。否则,Bean 将不会被连接。
例如,如果 Bean 定义在配置文件中设置为按名称自动装配,并且它包含一个 spellChecker 属性(即,它具有 setSpellChecker(...) 方法),Spring 会查找名为 spellChecker 的 Bean 定义,并使用它来设置该属性。您仍然可以使用 <property> 标签连接其余属性。以下示例将说明此概念。
示例
以下示例显示了一个类 TextEditor,它只能使用纯基于 Setter 的注入进行依赖注入。
让我们更新在 Spring DI - 创建项目 章节中创建的项目。我们正在添加以下文件 -
TextEditor.java - 包含 SpellChecker 作为依赖项的类。
SpellChecker.java - 依赖项类。
MainApp.java - 运行和测试的主应用程序。
以下是 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("applicationcontext.xml");
TextEditor te = (TextEditor) context.getBean("textEditor");
te.spellCheck();
}
}
以下是配置文件 applicationcontext.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 = "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.
广告