这一章节我们来具体讨论一下配合@autowired一起使用的限定器@Qualifier。

1.domain(重点)

蛋糕类:

package com.raylee.my_new_spring.my_new_spring.ch02.topic_1_11;public class Cake {private String name = "";public String getName() {return name;}public void setName(String name) {this.name = name;}}

厨师类:

package com.raylee.my_new_spring.my_new_spring.ch02.topic_1_11;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;public class Chief {@Autowired@Qualifier("cake")private Cake cake = null;private String name = "";public String getName() {return name;}public Cake makeOneCake() {if (cake != null) {System.out.println(getName() + " make " + cake.getName());} else {System.out.println(cake);}return cake;}public void setName(String name) {this.name = name;}}

厨师类这里须要注意的是,因为配置文件可能出现多个满足cake的bean。因此我们在chief里面使用限定器@Qualifier,限定了cake属性的注入对象仅仅可以是beanid是cake的bean。

2.測试类:(不变)

package com.raylee.my_new_spring.my_new_spring.ch02.topic_1_11;import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/com/raylee/my_new_spring/my_new_spring/ch02/topic_1_11/ApplicationContext-test.xml" })
public class ChiefTest {@Autowiredprivate ApplicationContext applicationContext;@Testpublic void testChief() {Chief jack = applicationContext.getBean(Chief.class);jack.makeOneCake();}
}

3.配置文件

<?

xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="blueberryCheeseCake" class="com.raylee.my_new_spring.my_new_spring.ch02.topic_1_11.Cake" p:name="blueberryCheeseCake" scope="prototype" /> <bean id="cake" class="com.raylee.my_new_spring.my_new_spring.ch02.topic_1_11.Cake" p:name="cake" scope="prototype" /> <bean id="jack" class="com.raylee.my_new_spring.my_new_spring.ch02.topic_1_11.Chief" p:name="jack" /> </beans>

配置文件须要注意的地方是:我们为了測试上面的样例。有益定义了两个Cake类型的bean

測试输出:

jack make cake

总结:这一章节我们主要讨论了@autowired里面的限定器@Qualifier。

文件夹:http://blog.csdn.net/raylee2007/article/details/50611627

我的github:https://github.com/raylee2015/my_new_spring

从头认识Spring-2.3 注解装配-@autowired(5)-限定器@Qualifier(1)相关推荐

  1. Spring框架最终注解标签注入方法

    使用注解装配bean 使用@Autowired注解 从Spring2.5开始,最有趣的一种装配Spring Bean的方式是使用注解自动装配Bean的属性. Spring默认禁用注解装配,最简单的启用 ...

  2. Spring自动装配----注解装配----Spring自带的@Autowired注解

    Spring自动装配----注解装配----Spring自带的@Autowired注解 父类 package cn.ychx;public interface Person {public void ...

  3. Spring bean注入之注解注入-- @Autowired原理

    之前我们已经讲述过bean注入是什么了,也使用了xml的配置文件进行bean注入,这也是Spring的最原始的注入方式(xml注入). 本节课就讲注解注入. 主要讲解的注解有以下几个: @Autowi ...

  4. Spring学习(六)bean装配详解之 【通过注解装配 Bean】【基础配置方式】

    本文借鉴:Spring学习(特此感谢!) 通过注解装配 Bean 1.前言 优势 1.可以减少 XML 的配置,当配置项多的时候,XML配置过多会导致项目臃肿难以维护 2.功能更加强大,既能实现 XM ...

  5. spring注解( @Autowired、@Qualifier、@Resource、@PostConstruct、@PreDestroy、 @Component、@Scope)-描述的比较清楚

    概述: 注释配置相对于 XML 配置具有很多的优势: 它可以充分利用 Java 的反射机制获取类结构信息,这些信息可以有效减少配置的工作.如使用 JPA 注释配置 ORM 映射时,我们就不需要指定 P ...

  6. Spring中的自动装配和Autowired

    彻底搞明白Spring中的自动装配和Autowired 一.自动装配 当Spring装配Bean属性时,有时候非常明确,就是需要将某个Bean的引用装配给指定属性.比如,如果我们的应用上下文中只有一个 ...

  7. Spring通过注解装配Bean

    通过注解实现ServiceImpl业务 一.使用@Component装配Bean 1. 定义类:User 在类上面加@Component注解,在属性上面加@Value值 package com.wbg ...

  8. Spring Boot2.x-04Spring Boot基础-使用注解装配bean

    文章目录 概述 通过Java配置文件@Bean的方式定义Bean 通过注解扫描的方式(@Component/@ComponentScan)装配Bean 使用excludeFilters属性不让IoC加 ...

  9. Spring下的@Inject、@Autowired、@Resource注解区别(转)

    1.@Inject javax.inject JSR330 (Dependency Injection for Java) 这是JSR330中的规范,通过AutowiredAnnotationBean ...

最新文章

  1. 函数 tostring_Kotlin实战之Fuel的高阶函数
  2. java封装省市区三级json格式,2016中国省市区三级联动json格式.pdf
  3. 树莓派局域网文件共享samba
  4. ios开发读取剪切板的内容_为你找到3款Mac平台好用的剪切板工具,你值得拥有!...
  5. Codeforces Round #740 (Div. 2, based on VK Cup 2021 - Final (Engine)) A-F全题解
  6. python可变参数函数二阶导数公式_python中函数的可变参数
  7. android出现错误,在做一个安卓的一个登陆操作的时候,出现错误
  8. vue自定义下拉菜单,点击下拉其它空白区域,下拉消失
  9. STRAIGHT_JOIN
  10. 极简浏览器主页网址导航自定义网址壁纸云端同步简洁清爽
  11. Mac微信更新 可备份手机聊天记录
  12. 机器视觉-相机选择方法-缺陷检测
  13. thinkpad 重装--AHCI 导致系统蓝屏---迅盘
  14. 理解COCO的评价指标:AP,AP50,AP70,mAP,AP[.50:.05:.95]
  15. 坯子库无法一键安装插件没用_坯子插件库下载-坯子插件库下载 v2021.1官方版--pc6下载站...
  16. tableau数据分析实战:明星艺人数据分析
  17. Mybatis 查询总数
  18. 管理罗盘-管理者角色认知与定位
  19. 激活office时,提示CScript错误:无法找到ospp.vbs脚本引擎vbscript
  20. 刷题day65:整数拆分

热门文章

  1. 使用Python统计深圳市轮候保障房申请人省份年龄统计
  2. 回首2013 展望2014
  3. Ruby 3 有望引入静态类型
  4. MyBatis批量插入
  5. SQL点滴31—SQL语句中@@IDENTITY和@@ROWCOUNT区别
  6. SQL Server 2008行数据和页数据压缩解密
  7. 轻轻的我走了,正如我轻轻的来…——ADO.NET核心类的灭度与SQLHelper的诞生——十八相送(下)...
  8. 智能指针和内存管理小结
  9. window 下Eclipse c++的开发环境配置
  10. 字符串与整数之间进行转换