这一章节我们来具体讨论一下配合@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 { @Autowired
private ApplicationContext applicationContext; @Test
public 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-2.3 注解装配-@autowired(3)-通过构造器方法注入

    这一章节我们来讨论一下注解装配的@autowired是如何通过set方法或者其它方法注入? 1.domain 蛋糕类:(不变) package com.raylee.my_new_spring.my_ ...

  2. 如何在 spring 中启动注解装配?

    默认情况下,Spring 容器中未打开注解装配.因此,要使用基于注解装配,我们 必须通过配置 <context:annotation-config/> 元素在 Spring 配置文件 中启 ...

  3. 从头认识Spring-2.3 注解装配-@autowired(4)-required(2)

    这一章节我们来继续具体讨论一下@autowired里面的參数required.在多构造器注入的情况. 1.domain(重点) 蛋糕类: package com.raylee.my_new_sprin ...

  4. 从头认识Spring-2.3 注解装配-@autowired(4)-required(1)

    这一章节我们来具体讨论一下@autowired里面的參数required. 1.domain(重点) 蛋糕类: package com.raylee.my_new_spring.my_new_spri ...

  5. 模拟实现Spring中的注解装配

    本文原创,地址为http://www.cnblogs.com/fengzheng/p/5037359.html 在Spring中,XML文件中的bean配置是实现Spring IOC的核心配置文件,在 ...

  6. Spring学习笔记--使用注解装配

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

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

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

  8. Spring入门(6)-使用注解装配

    Spring入门(6)-使用注解装配 本文介绍如何使用注解装配. 0. 目录 使用Autowired 可选的自动装配 使用Qualifier选择 1. 使用Autowired package com. ...

  9. Spring框架(3)---IOC装配Bean(注解方式)

    IOC装配Bean(注解方式) 上面一遍文章讲了通过xml来装配Bean,那么这篇来讲注解方式来讲装配Bean对象 注解方式需要在原先的基础上重新配置环境: (1)Component标签举例 1:导入 ...

随机推荐

  1. vue-cli(脚手架) 打包上线

    http://www.cnblogs.com/xueweijie/p/6971146.html

  2. 数据库中解析XML

    简介:OPENXML方法使用一例实现导入功能 DECLARE @strProjGUID AS VARCHAR(50)  DECLARE @strProjCode AS VARCHAR(50)  DEC ...

  3. 动态链接库DLL的创建生成及调用

    一.背景 最近在做CANTOUSB底层驱动的调用,是调用别人已经封装好的库,看不到别人写的源程序.程序中调用的是隐式调用即 x.h+x.lib+x.dll,其中DLL即是动态链接库(Dynamic L ...

  4. PatentTips - Supporting address translation in a virtual machine environment

    BACKGROUND A conventional virtual-machine monitor (VMM) typically runs on a computer and presents to ...

  5. Android-ViewPager+Fragment数据更新问题

    由于FragmentPagerAdapter内部存在缓存.因此调用notifyDataSetChanged()并不可以去更新Fragment的内容. 參考:http://www.devba.com/i ...

  6. Fragment 实现的 分类 效果

    Fragment   实现的 分类 效果 布局文件的信息: <LinearLayout xmlns:android="http://schemas.android.com/apk/re ...

  7. 1.2 Use Cases中 Website Activity Tracking官网剖析(博主推荐)

    不多说,直接上干货! 一切来源于官网 http://kafka.apache.org/documentation/ Website Activity Tracking 网站活动追踪 The origi ...

  8. what happens when changing the DOM via innerHTML

    what happens when changing the DOM via innerHTML

  9. serialport串口通讯

    在.NET Framework 2.0中提供了SerialPort类,该类主要实现串口数据通信 = System.IO.Ports.SerialPort.GetPortNames();获取电脑有哪几个 ...

  10. UVA 11584 - Partitioning by Palindromes DP

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...