当创建多个具有相同类型的 bean 时,并且想要用一个属性只为它们其中的一个进行装配,在这种情况下,你可以使用 @Qualifier 注释和 @Autowired 注释通过指定哪一个真正的 bean 将会被装配来消除混乱。下面显示的是使用 @Qualifier 注释的一个示例。

1.这里是 Student.java 文件的内容:

 package com.spring.chapter7;

 public class Student {
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
private String name;
private int age; }

2.这里是 Profile.java 文件的内容:

 package com.spring.chapter7;

 import java.util.List;
import java.util.Set; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Required; public class Profile { @Autowired
@Qualifier("Student") //这里的值就是bean的id,当我们改变此值跟随我们指定的bean进行注入
private Student student; public void printName() {
System.out.println("age:"+student.getName());
} public void printAge() {
System.out.println("name:"+student.getAge());
} public Profile() {
System.out.println("--------构造方法------------");
}
}

3.下面是 MainApp.java 文件的内容:

 package com.spring.chapter7;

 import java.util.List;

 import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main {
/**
* Spring @Qualifier 注解注入
* author:
* mail:2536201485@qq.com
* 时间:
*/ public static void main(String[] args) {
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring_xml/spring.xml");
Profile profile=(Profile)applicationContext.getBean("Profile");
profile.printName();
profile.printAge(); } }

4.下面配置文件 spring.xml 的示例:

     <!-- @Qualifier 注解 -->
<bean id="Profile" class="com.spring.chapter7.Profile">
</bean>
<bean id="Student" class="com.spring.chapter7.Student">
<property name="name" value="张三"></property>
<property name="age" value="23"></property>
</bean> <bean id="Student2" class="com.spring.chapter7.Student">
<property name="name" value="李四"></property>
<property name="age" value="22"></property>
</bean>

运行结果:

--------构造方法------------
age:张三
name:23

Spring 注解注入—@Qualifier 注释的更多相关文章

  1. 使用Spring注解注入属性

    本文介绍了使用Spring注解注入属性的方法.使用注解以前,注入属性通过类以及配置文件来实现.现在,注入属性可以通过引入@Autowired注解,或者@Resource,@Qualifier,@Pos ...

  2. spring注解注入:<context:component-scan>以及其中的context:include-filter>和 <context:exclude-filter>的是干什么的?

    转自:https://www.cnblogs.com/vanl/p/5733655.html spring注解注入:<context:component-scan>使用说明   sprin ...

  3. spring注解注入properties配置文件

    早期,如果需要通过spring读取properties文件中的配置信息,都需要在XML文件中配置文件读取方式. 基于XML的读取方式: <bean class="org.springf ...

  4. spring注解注入:<context:component-scan>使用说明

    spring从2.5版本开始支持注解注入,注解注入可以省去很多的xml配置工作.由于注解是写入java代码中的,所以注解注入会失去一定的灵活性,我们要根据需要来选择是否启用注解注入. 在XML中配置了 ...

  5. spring注解注入:<context:component-scan>详解

    spring从2.5版本开始支持注解注入,注解注入可以省去很多的xml配置工作.由于注解是写入java代码中的,所以注解注入会失去一定的灵活性,我们要根据需要来选择是否启用注解注入. 我们首先看一个注 ...

  6. spring注解注入的学习

    spring在开发中起到管理bean的作用,不管是web应用还是应用软件开发.注入有多种方式,其中注解注入最为受欢迎.(java api 在1.6版本以上才引入关键的注解类如:@Resource) 配 ...

  7. JAVA框架 Spring 注解注入

    一.首先需要引入jar包:spring-aop-4.2.4.RELEASE.jar.(在spring解压包libs内). 二.如果注解方式注入依赖的对象,需要引用新的约束. 内的:xsd-config ...

  8. spring 注解注入bean

    通过注解方式注入bean,需要在配置类下注入bean 第一步,配置扫描文件夹 首先要在spring.xml中配置需要扫描的配置类 <context:componenet-scan base-pa ...

  9. spring注解注入失败一个原因

    所有的注解看起来都没有任何问题,最后是由于web-xml配置问题. 由于缺少监听器org.springframework.web.context.ContextLoaderListener, 导致无法 ...

随机推荐

  1. TP3快速入门

    一.查询 D方法实例化模型类的时候通常是实例化某个具体的模型类,如果你仅仅是对数据表进行基本的CURD操作的话,使用M方法实例化的话,由于不需要加载具体的模型类,所以性能会更高. $map = arr ...

  2. "三号标题"组件:<h3> —— 快应用组件库H-UI

     <import name="h3" src="../Common/ui/h-ui/text/c_h3"></import> < ...

  3. POj3017 dp+单调队列优化

    传送门 解题思路: 大力推公式:dp[i]=min(dp[k]+max(k+1,i)){k>=0&&k<i},max(j,i)记为max(a[h]){h>k& ...

  4. hive常用函数二

    逻辑运算: 1. 逻辑与操作: AND 语法: A AND B 操作类型:boolean 说明:如果A和B均为TRUE,则为TRUE:否则为FALSE.如果A为NULL或B为NULL,则为NULL 举 ...

  5. coding++:Spring 中的 AOP 原理

    为什么使用 AOP 如下场景: 现在有一个情景: 我们要把大象放进冰箱,步骤为:打开冰箱->放入大象->关闭冰箱 如果再把大象拿出来,步骤为:打开冰箱->拿出大象->关闭冰箱 ...

  6. php.ini配置文件详解(基于5.2.17版本)

    [PHP] ;;;;;;;;;;;;;;;;;;;; About php.ini ;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;; 关于php.ini文件 ;;;;; ...

  7. java的多线程是如何实现的?和操作系统有什么关系?

    本文是作者原创,版权归作者所有.若要转载,请注明出处.本文只贴我觉得比较重要的源码,其他不重要非关键的就不贴了 本文操作系统是centos7 1.查看 pthread_create 函数显示及其示例 ...

  8. Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(一)之Introduction

    Learn Java I found out that I and other speakers tended to give the typical audience too many topics ...

  9. stand up meeting 12/10/2015

    part 组员 今日工作 工作耗时/h 明日计划 工作耗时/h UI 冯晓云 修改了详细释义的自动换行功能:设计并完成了背景图片的切换功能    6 完成单词释义热度排序    6 PDF Reade ...

  10. 使用dynamic和MEF实现轻量级的AOP组件 (2)

    转摘 https://www.cnblogs.com/niceWk/archive/2010/07/21/1782092.html 偷梁换柱 上一篇我们初试了DynamicAspect这把小刀,如果你 ...