Spring的@Qualifier注解
可能会出现这种情况,当你创建多个相同类型的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注解的更多相关文章
- 【spring】@Qualifier注解
近期在捯饬spring的注解,现将遇到的问题记录下来,以供遇到同样问题的童鞋解决~ 先说明下场景,代码如下: 有如下接口: public interface EmployeeService { pub ...
- Spring学习(10)--- @Qualifier注解
按类型自动装配可能多个bean实例的情况,可以使用Spring的@Qualifier注解缩小范围(或指定唯一),也可以指定单独的构造器参数或方法参数 可用于注解集合类型变量 例子: package c ...
- spring @qualifier注解
1.spring @qualifier注解用来在spring按类型装配可能存在多个bean的情况下,@qualifier注解可以用来缩小范围或者指定唯一. 也可以用来指定方法参数 2.@qualifi ...
- [Spring]@Autowired,@Required,@Qualifier注解
@Required注解 @Required注解用于setter方法,表明这个属性是必要的,不可少的,必须注入值 假设有个测试类,里面有name和password两个属性 我给两个属性的setter方法 ...
- spring的@primary和@qualifier注解解决一个接口多个实现的注入问题
Spring中提供了@Primary和@Qualifier注解来解决一个接口多个实现的注入问题. @Primary注解 Spring中有提供一个@Primary注解,具体的作用是在一个接口有多个实现类 ...
- Spring的注解@Qualifier注解
@Qualifier注解了,qualifier的意思是合格者,通过这个标示,表明了哪个实现类才是我们所需要的,我们修改调用代码,添加@Qualifier注解,需要注意的是@Qualifier的参数名称 ...
- spring笔记--通过注解(annotation)配置Bean
Spring能够在classpath下自动扫描,侦测和实例化具有特定注解的组件,这在Spring中成为组件扫描(Component scanning). 特定组件的注解包括: @Component:基 ...
- Spring MVC常用注解
cp by http://www.cnblogs.com/leskang/p/5445698.html 1.@Controller 在SpringMVC 中,控制器Controller 负责处理由Di ...
- Spring中@Autowired注解、@Resource注解的区别
Spring不但支持自己定义的@Autowired注解,还支持几个由JSR-250规范定义的注解,它们分别是@Resource.@PostConstruct以及@PreDestroy. @Resour ...
随机推荐
- 原创:shell两个整数的比较 思想版
思想是学的 代码创作是自己的 很喜欢前几行的逻辑严谨 #!/bin/bash#判断两个整数的大小read -p "请输入两个整数a b :" a b #或者使用a=$1[ -z & ...
- Urlrewritefilte
Urlrewritefilter是通过filter的形式,过滤所有的请求,然后再根据配置文件来转换成真正要访问的URL. 好处是隐藏真正的URL和美化提供给客户的URL. 比如,你的首页是www.** ...
- 【前端】pid2children iview tree json
<script> import inBody from '../inBody' export default { components:{ inBody } ,data () { retu ...
- Eaton Char-Lynn Motor : Performance Of Small Displacement Motors
The small-displacement supercharged motor replaces the large-displacement motor with the speed of li ...
- 从零实现一个http服务器
我始终觉得,天生的出身很重要,但后天的努力更加重要,所以如今的很多“科班”往往不如后天努力的“非科班”.所以,我们需要重新给“专业”和“专家”下一个定义:所谓专业,就是别人搞你不搞,这就是你的“专业” ...
- springmvc请求小例子
1.welcome.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" ...
- ES6中Generator
ES6中Generator Generator是ES6一个很有意思的特性,也是不容易理解的特性.不同于let/const提供了块级作用域这样明显的目的,这玩意儿被搞出来到底是干嘛的? 首先我们需要明确 ...
- python基础知识12-异常
几个命令: sudo apt install tree tree 当前目录以树形结构展示出来. tree a 查看a内的所有目录.也可以跟绝对路径. a = input('请输入您的密码') 将用户输 ...
- fstream,sstream的学习记录
fstream: #include<iostream> #include<fstream> using namespace std; int main(){ ofstream ...
- 洛谷 P2008 大朋友的数字
DP,动态规划 树状数组 最长不下降子序列 by GeneralLiu 题目 就是说给一串由 0~9 组成的序列 求 以 i (1~n) 结尾 的 最长不下降子序列 的 和 (最长不下降子序 ...