当创建多个具有相同类型的 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. jvm入门及理解(一)——简介与体系架构

    最近,在学习java虚拟机的内容中,在此总结和记录下学到的. 一.JVM在计算机中的位置 在我们初学java时,便知道java源文件文件会先通过Java编译器编译成字节码文件,这个过程是java编译过 ...

  2. ElasticSearch 常用查询语句

    为了演示不同类型的 ElasticSearch 的查询,我们将使用书文档信息的集合(有以下字段:title(标题), authors(作者), summary(摘要), publish_date(发布 ...

  3. fork()系统调用的理解

    系统调用fork()用于创建一个新进程.我们可以通过下面的代码来理解,最好是能自己敲一遍运行验证. ​#include<stdio.h> #include<stdlib.h> ...

  4. 【Java】手动编写第一个Java程序,HelloWorld!

    第一个Java程序HelloWorld! 环境前提:确保你已经配置好了JDK8的环境变量,和本体安装 打开文本编辑器,这里我使用的是EditPlus 编写代码: public class Hello{ ...

  5. 【three.js第五课】光线的添加和感光材料

    材料分类: MeshBasicMaterial:基础网孔材料,一个以简单着色(平面或线框)方式来绘制几何形状的材料.MeshLambertMaterial:兰伯特网孔材料,一种非发光材料(兰伯特)的表 ...

  6. 8行代码帮你python建立UDP通信

    首先我们要搭建一个UDP通信,首先我们就要知道什么UDP: UDP用户数据报传输协议,它位于TCP/IP协议的传输层,是一种无连接的协议,它发送的报文不能确定是否完整地到达了另外一端.UDP广泛应用于 ...

  7. ASE课程总结 by 冯晓云

    开始的开始,采访往届ASE班的blog:http://www.cnblogs.com/legs/p/4894362.html 和北航软工M1检查:http://www.cnblogs.com/legs ...

  8. Video tagging systems based on DNNs

    Need: With the ever-growth large-scale video in the mobile phone, so what will everyone get from the ...

  9. 【Jenkins】参数化引用

    我们在Jenkins里设置了参数如下 1. Jenkins中引用 shell引用  $env windows bat引用 %env% 在git等源码管理时,调用参数的格式${env} 2. jmete ...

  10. Linux下安装Redis4.0版本(简便方法)

    Redis介绍: Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库. Redis 与其他 key - value 缓存产品有以下三个特点: Redis支持数据的持久 ...