Spring的注释@Autowired、@Qualifier使用说明
来源:互联网 发布:httppost发送json数据 编辑:程序博客网 时间:2024/06/14 06:12
可能会有这样一种情况,当你创建多个具有相同类型的 bean 时,并且想要用一个属性只为它们其中的一个进行装配,在这种情况下,你可以使用 @Qualifier 注释和 @Autowired 注释通过指定哪一个真正的 bean 将会被装配来消除混乱。下面显示的是使用 @Qualifier 注释的一个示例。
示例
让我们使 Eclipse IDE 处于工作状态,请按照下列步骤创建一个 Spring 应用程序:
步骤 描述
1 创建一个名为 SpringExample 的项目,并且在所创建项目的 src 文件夹下创建一个名为 com.tutorialspoint 的包。
2 使用 Add External JARs 选项添加所需的 Spring 库文件,就如在 Spring Hello World Example 章节中解释的那样。
3 在 com.tutorialspoint 包下创建 Java 类 Student,Profile 和 MainApp。
4 在 src 文件夹下创建 Beans 配置文件 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 : 11Name : Zara
- Spring的注释@Autowired、@Qualifier使用说明
- @Autowired 注释与@Qualifier 注释
- spring的注解使用,Autowired和Qualifier
- Spring Autowired(required = false) 说明与@Qualifier 注释说明
- Spring Annotation -- @Autowired, @Qualifier
- spring @Autowired @Qualifier
- Spring注解@Autowired、@Qualifier
- @Autowired 注释遇到的问题,@Qualifier 帮助解决问题
- @Autowired 注释遇到的问题,@Qualifier 帮助解决问题
- @Autowired 注释遇到的问题,@Qualifier 帮助解决问题
- Spring 之 Annotation-->>@Autowired & @Qualifier
- Spring Annotation(注解) Autowired Qualifier
- spring注解 @Autowired @Qualifier @Resource
- Spring @Autowired和@Qualifier注解
- spring注解 @Autowired @Qualifier @Resource
- Spring注解标签 @Autowired @Qualifier
- Spring注解@Autowired @Repository @Qualifier
- spring的Autowired,Qualifier注解介绍以及自动装配
- 1078. Hashing (25)
- C#和XML
- iOS流布局UICollectionView系列一——初识与简单使用UICollectionView
- 使用外部应用打开pdf
- PCI总线枚举
- Spring的注释@Autowired、@Qualifier使用说明
- OKhttp的用法
- IOS自适应前段库-Masonry的使用
- C++遍历文件夹
- 判断iPhone型号
- onCreate()中设置屏幕常亮
- kafka -- 入门与环境搭建
- js中的Date()兼容浏览器
- 总冠军-克里夫兰骑士