- Spring核心基础
- Spring - 首页
- Spring - 概览
- Spring - 架构
- Spring - 环境设置
- Spring - Hello World 示例
- Spring - IoC 容器
- Spring - Bean 定义
- Spring - Bean 作用域
- Spring - Bean 生命周期
- Spring - Bean 后处理器
- Spring - Bean 定义继承
- Spring - 依赖注入
- Spring - 注入内部 Bean
- Spring - 注入集合
- Spring - Bean 自动装配
- 基于注解的配置
- Spring - 基于 Java 的配置
- Spring - Spring 中的事件处理
- Spring - Spring 中的自定义事件
- Spring - 使用 Spring 框架的 AOP
- Spring - JDBC 框架
- Spring - 事务管理
- Spring - Web MVC 框架
- Spring - 使用 Log4J 进行日志记录
- Spring 问题及解答
- Spring - 问题及解答
- Spring 有用资源
- Spring - 快速指南
- Spring - 有用资源
- Spring - 讨论
Spring @Qualifier 注解
可能存在这样一种情况:您创建了多个相同类型的 Bean,并且只想将其中一个与某个属性关联。在这种情况下,您可以将@Qualifier注解与@Autowired一起使用,通过指定要关联的确切 Bean 来消除混淆。以下是一个展示@Qualifier注解用法的示例。
示例
让我们准备好一个正在运行的 Eclipse IDE,并采取以下步骤创建一个 Spring 应用程序:
| 步骤 | 描述 |
|---|---|
| 1 | 创建一个名为SpringExample的项目,并在创建的项目中的src文件夹下创建一个com.tutorialspoint包。 |
| 2 | 使用添加外部 JAR选项添加所需的 Spring 库,如Spring Hello World 示例章节中所述。 |
| 3 | 在com.tutorialspoint包下创建 Java 类Student、Profile和MainApp。 |
| 4 | 在src文件夹下创建 Bean 配置文件Beans.xml。 |
| 5 | 最后一步是创建所有 Java 文件和 Bean 配置文件的内容,并按照以下说明运行应用程序。 |
以下是Student.java文件的内容:
package com.tutorialspoint;
public class Student {
private Integer age;
private String name;
public void setAge(Integer age) {
this.age = age;
}
public Integer getAge() {
return age;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
以下是Profile.java文件的内容
package com.tutorialspoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
public class Profile {
@Autowired
@Qualifier("student1")
private Student student;
public Profile(){
System.out.println("Inside Profile constructor." );
}
public void printAge() {
System.out.println("Age : " + student.getAge() );
}
public void printName() {
System.out.println("Name : " + student.getName() );
}
}
以下是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");
Profile profile = (Profile) context.getBean("profile");
profile.printAge();
profile.printName();
}
}
考虑以下配置文件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"
xmlns:context = "http://www.springframework.org/schema/context"
xsi:schemaLocation = "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config/>
<!-- Definition for profile bean -->
<bean id = "profile" class = "com.tutorialspoint.Profile"></bean>
<!-- Definition for student1 bean -->
<bean id = "student1" class = "com.tutorialspoint.Student">
<property name = "name" value = "Zara" />
<property name = "age" value = "11"/>
</bean>
<!-- Definition for student2 bean -->
<bean id = "student2" class = "com.tutorialspoint.Student">
<property name = "name" value = "Nuha" />
<property name = "age" value = "2"/>
</bean>
</beans>
创建源文件和 Bean 配置文件后,让我们运行应用程序。如果您的应用程序一切正常,它将打印以下消息:
Inside Profile constructor. Age : 11 Name : Zara
spring_annotation_based_configuration.htm
广告