以下内容引用自http://wiki.jikexueyuan.com/project/spring/annotation-based-configuration/spring-qualifier-annotation.html

可能会出现这种情况,当你创建多个相同类型的bean并且希望仅使用属性中的一个连接时。在这种情况下,您可以使用@Qualifier注解和@Autowired注解来通过指定要连接哪个确切的bean来消除混淆。

例子:

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.jsoft.testspring</groupId>
<artifactId>testannotationqualifier</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>testannotationqualifier</name>
<url>http://maven.apache.org</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency> <!-- Spring Core -->
<!-- http://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.4.RELEASE</version>
</dependency> <!-- Spring Context -->
<!-- http://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
</dependencies>
</project>

Student.java:

package com.jsoft.testspring.testannotationqualifier;

import org.springframework.beans.factory.annotation.Required;

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

Profile.java:

package com.jsoft.testspring.testannotationqualifier;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier; public class Profile {
@Autowired
@Qualifier("student2")
private Student student; public void getAge(){
System.out.println(this.student.getAge());
} public void getName() {
System.out.println(this.student.getName());
}
}

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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config/> <bean id="student1" class="com.jsoft.testspring.testannotationqualifier.Student">
<property name="name" value="Jim"/>
<property name="age" value="27"/>
</bean> <bean id="student2" class="com.jsoft.testspring.testannotationqualifier.Student">
<property name="name" value="Jim2"/>
<property name="age" value="18"/>
</bean> <bean id="profile" class="com.jsoft.testspring.testannotationqualifier.Profile"></bean> </beans>

App.java:

package com.jsoft.testspring.testannotationqualifier;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
Profile profile = (Profile)applicationContext.getBean("profile");
profile.getAge();
profile.getName();
}
}

测试结果:

测试工程:https://github.com/easonjim/5_java_example/tree/master/springtest/test12/testannotationqualifier

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

  1. 【spring】@Qualifier注解

    近期在捯饬spring的注解,现将遇到的问题记录下来,以供遇到同样问题的童鞋解决~ 先说明下场景,代码如下: 有如下接口: public interface EmployeeService { pub ...

  2. Spring学习(10)--- @Qualifier注解

    按类型自动装配可能多个bean实例的情况,可以使用Spring的@Qualifier注解缩小范围(或指定唯一),也可以指定单独的构造器参数或方法参数 可用于注解集合类型变量 例子: package c ...

  3. spring @qualifier注解

    1.spring @qualifier注解用来在spring按类型装配可能存在多个bean的情况下,@qualifier注解可以用来缩小范围或者指定唯一. 也可以用来指定方法参数 2.@qualifi ...

  4. [Spring]@Autowired,@Required,@Qualifier注解

    @Required注解 @Required注解用于setter方法,表明这个属性是必要的,不可少的,必须注入值 假设有个测试类,里面有name和password两个属性 我给两个属性的setter方法 ...

  5. spring的@primary和@qualifier注解解决一个接口多个实现的注入问题

    Spring中提供了@Primary和@Qualifier注解来解决一个接口多个实现的注入问题. @Primary注解 Spring中有提供一个@Primary注解,具体的作用是在一个接口有多个实现类 ...

  6. Spring的注解@Qualifier注解

    @Qualifier注解了,qualifier的意思是合格者,通过这个标示,表明了哪个实现类才是我们所需要的,我们修改调用代码,添加@Qualifier注解,需要注意的是@Qualifier的参数名称 ...

  7. spring笔记--通过注解(annotation)配置Bean

    Spring能够在classpath下自动扫描,侦测和实例化具有特定注解的组件,这在Spring中成为组件扫描(Component scanning). 特定组件的注解包括: @Component:基 ...

  8. Spring MVC常用注解

    cp by http://www.cnblogs.com/leskang/p/5445698.html 1.@Controller 在SpringMVC 中,控制器Controller 负责处理由Di ...

  9. Spring中@Autowired注解、@Resource注解的区别

    Spring不但支持自己定义的@Autowired注解,还支持几个由JSR-250规范定义的注解,它们分别是@Resource.@PostConstruct以及@PreDestroy. @Resour ...

随机推荐

  1. 11gR2新特性---gipc守护进程

    在这篇文章中,我们会对11gR2 新的守护进程gipcd(资源名称ora.gipcd)进行介绍,其中包括gipc的功能,启动顺序和一些基本的测试. 我们知道,对于oracle集群来说,集群私网是非常重 ...

  2. delphi中使用自定义资源的方法

    如果要在delphi中使用自定义资源文件*.res文件,比如一个光标,此时可以采用下列步骤: 1,创建包含相应的资源文件,这里是创建一个包含自定义光标的res文件. 2,在主窗体的pas文件中加入编译 ...

  3. 最短路 || POJ 1511 Invitation Cards

    已知图中从一点到另一点的距离,从1号点到另一点再从这一点返回1号点,求去到所有点的距离之和最小值 *解法:正着反着分别建图,把到每个点的距离加起来 spfa跑完之后dist数组就是从起点到每一点的最短 ...

  4. ES6 第五章 字符串的新增方法 具体参照 http://es6.ruanyifeng.com

    1.FormCodePoint 对象方法 用于从 Unicode 码点返回对应字符,可以识别原来es5不能识别的大于0xFFFF的码点. String.fromCodePoint(0x20BB7) / ...

  5. 717. 1-bit and 2-bit Characters@python

    We have two special characters. The first character can be represented by one bit 0. The second char ...

  6. Visual C++中error spawning cl.exe错误的两种解决方法.

    可能很多人在安装VC 6.0后有过点击“Compile”或者“Build”后被出现的 “Compiling... ,Error spawning cl.exe”错误提示给郁闷过.很多人的 选择是重装, ...

  7. 编写一个微信小程序

    1.创建项目 2.创建目录及文件,结构如下:

  8. reversing.kr easykeygen 之wp

    补充: int(x,[base]): 就是将x(通常是一个字符串)按照base进制转换成整数. 比如: ’) ##转换成整数10 ) ##'按16进制转换,将得到整数16 ) ##得到255 int( ...

  9. JQuery中如何查找某种类型的所有元素&选择器

    更多的是,有关于选择器的内容. 背景:查找某控件中所有的input元素,代码如下: $("#div1").find("input").each(function ...

  10. c#导出word文档

    为方便下次遇到不知道去哪找先把它存放在这里,以下是保存导出word主要类方法 public class BiultReportForm { /// <summary>word 应用对象 & ...