使用@Qualifier可以分别为同样类型的Bean分别注入不同的依赖值。看个例子:

Student.java:

package com.sap;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;}
}

Profile:

package com.sap;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
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() );}
}

Main.app:

package com.sap;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
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 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/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd"><context:annotation-config/><!-- Definition for profile bean --><bean id="profile" class="com.sap.Profile"></bean><!-- Definition for student1 bean --><bean id="student1" class="com.sap.Student"><property name="name"  value="Zara" /><property name="age"  value="11"/></bean><!-- Definition for student2 bean --><bean id="student2" class="com.sap.Student"><property name="name"  value="Nuha" /><property name="age"  value="2"/></bean></beans>

输出:

如果把profile类里的id改成student2:

输出:

要获取更多Jerry的原创文章,请关注公众号"汪子熙":

Spring 基于注解(annotation)的配置之@Qualifier注解相关推荐

  1. Spring 基于注解(annotation)的配置之@Required注解

    从 Spring 2.5 开始就可以使用注解来配置依赖注入.注解连线在默认情况下在 Spring 容器中不打开.因此,在可以使用基于注解的连线之前,我们将需要在我们的 Spring 配置文件中启用它: ...

  2. Spring 基于注解(annotation)的配置之@Autowired注解

    Setter 方法中的 @Autowired 当 Spring遇到一个在 setter 方法中使用的 @Autowired 注解,它会试图执行 byType 自动连接.换言之,加了@Autowired ...

  3. spring中自定义注解(annotation)与AOP中获取注解___使用aspectj的@Around注解实现用户操作和操作结果日志

    spring中自定义注解(annotation)与AOP中获取注解 一.自定义注解(annotation) 自定义注解的作用:在反射中获取注解,以取得注解修饰的类.方法或属性的相关解释. packag ...

  4. SpringBoot数据访问Mybatis注解版,配置版,注解与配置一体版

    SpringBoot数据访问Mybatis注解版,配置版,注解与配置一体版 注解版: 1.改druid 连接池,不改可以跳过这步 添加依赖 <dependency><groupId& ...

  5. hibernate mysql annotation_Hibernate基于注解annotation的配置

    Annotation在框架中是越来越受欢迎了,因为annotation的配置比起XML的配置来说方便了很多,不需要大量的XML来书写,方便简单了很多,只要几个annotation的配置,就可以完成我们 ...

  6. Spring基于@Configuration的类配置的内部源码实现

    概述 Spring容器启动时,即ApplicationContext接口实现类的对象实例执行refresh方法时,主要是通过执行ConfigurationClassPostProcessor这个Bea ...

  7. spring基于aspectj的AOP配置 aop:aspectj-autoproxy proxy-target-class=true

    精通Spring4.x--企业应用开发实战 8.5.1@AfterReturning("@annotation()")切点函数详解 代码实现的目标是为NaugthyWaiter类的 ...

  8. Java spring基于XML的aop配置实现

    1.依赖包 2.文件结构 3.接口类ISomeService package com.buckwheats.test;public interface ISomeService {public voi ...

  9. 面向切面(AOP)之Spring接口方式 schema配置方式 aspectj注解方式

    一.初识AOP   关于AOP的学习可以参看帮助文档:spring-3.2.0.M2\docs\reference\html目录下index.html的相关章节      1.AOP:Aspect-O ...

最新文章

  1. XDP/eBPF — BPF
  2. 消费者驱动的微服务契约测试套件:Spring Cloud Contract
  3. 2022-01-22
  4. 世界公认最好的记忆方法_全球公认最好的12个教育孩子的方法,值得每个家长收藏学习!...
  5. 【转】C#、面向对象、设计模式学习
  6. 趣学 C 语言(十二)—— 文件操作
  7. 多媒体计算机教室模式图,多媒体教室设备连接示意图.doc
  8. Java面试时,该如何准备亮点?
  9. 自己的服务器进不去显示403,HTTP 403错误:含义和解决方法
  10. Ardunio开发实例-VCNL4010 接近度和环境光传感器
  11. CMake入门1——CMake与VS编译器和nmake的结合使用
  12. 黑马程序员--typedef关键字和结构体 枚举类型
  13. Cocos Creator 微信创意小游戏《蛇它虫》团队专访:玩法画面均是新意
  14. 信息学奥赛一本通:1311:【例2.5】求逆序对
  15. UE4:打包项目前设置
  16. 刚入职就带领公司走上了上云之路
  17. Android开发常用工具,编译调试工具,性能优化工具,工具集
  18. 求坐标轴上的点与点之间的距离
  19. 野蛮生长-HW上机6
  20. 巨龙信息大数据集成系统 多元数据汇聚的一站式集成治理

热门文章

  1. 【python】【scrapy】使用方法概要(三)
  2. 自制Win7安装U盘!
  3. VMware 7.1.4安装Mac.OS.X.Lion.操作系统 key:安装 系统
  4. Spring Cloud Config git版
  5. GBDT分类和回归例子
  6. javas的四种状态 无锁状态 偏向锁状态 轻量级锁状态 重量级锁状态
  7. Ecos笔记--2014/5/3
  8. 微软的面试题(超变态但是很经典)
  9. 完成MSP430的IAP升级程序(总结)
  10. 文献学习(part87)--Linearized Alternating Direction Method with Adaptive Penalty for Low-Rank ...