1、当你创建多个具有相同类型的 bean 时,并且想要用一个属性只为它们其中的某一个进行装配,在这种情况下,你可以使用 @Qualifier 注释和 @Autowired 注释来精确配置。

2、示例

java

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;
}
}
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() );
}
}
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>
<context:annotation-config/>
<bean id="profile" class="com.tutorialspoint.Profile"></bean>
<bean id="student1" class="com.tutorialspoint.Student">
<property name="name" value="Zara" />
<property name="age" value="11"/>
</bean> <bean id="student2" class="com.tutorialspoint.Student">
<property name="name" value="Nuha" />
<property name="age" value="2"/>
</bean>
</beans>

打印输出

Inside Profile constructor.
Age : 11
Name : Zara

  

最新文章

  1. js base64加密,后台解密
  2. html5手机常见问题与工具分享
  3. 用jxl解析excel内容
  4. 动态修改ActionBar Menu的显示
  5. HibernateTemplate和HibernateDaoSupport
  6. 在ubuntu16.04中一键创建LAMP环境
  7. iOS开发经验总结
  8. 【朝花夕拾】Android性能篇之(七)Android跨进程通信篇
  9. C++常见问题解答博客合集
  10. Y1S002 xshell脚本编写示意
  11. Gogs 部署安装(windows)
  12. k8s定义Deployment,和service
  13. VS2013平台安装Qt插件过程
  14. UIView的alpha属性和hidden属性
  15. [osgearth]oe学习的一些经验(别人的)
  16. VMware VSAN 入门与配置(一)
  17. Jquery重新学习之九[Ajax运用总结C]
  18. Android 卡顿优化 4 布局优化实际技巧
  19. struts2之ModelDriven的用法
  20. Sliverlight变换特性

热门文章

  1. WinMerge文件编码设置
  2. redis使用Lua脚本
  3. CodeForces 456A
  4. iOS核心笔记—源代码管理工具-SVN
  5. We Chall-Training: Get Sourced-Writeup
  6. Omi原理-Hello Omi
  7. 记录UITextField删除状态
  8. java程序测试之字符流
  9. Python 接口测试(一)
  10. HTML5 File接口(在web页面上使用文件)