从头认识Spring-2.3 注解装配-@autowired(5)-限定器@Qualifier(1)
这一章节我们来具体讨论一下配合@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
从头认识Spring-2.3 注解装配-@autowired(5)-限定器@Qualifier(1)的更多相关文章
- 从头认识Spring-2.3 注解装配-@autowired(3)-通过构造器方法注入
这一章节我们来讨论一下注解装配的@autowired是如何通过set方法或者其它方法注入? 1.domain 蛋糕类:(不变) package com.raylee.my_new_spring.my_ ...
- 如何在 spring 中启动注解装配?
默认情况下,Spring 容器中未打开注解装配.因此,要使用基于注解装配,我们 必须通过配置 <context:annotation-config/> 元素在 Spring 配置文件 中启 ...
- 从头认识Spring-2.3 注解装配-@autowired(4)-required(2)
这一章节我们来继续具体讨论一下@autowired里面的參数required.在多构造器注入的情况. 1.domain(重点) 蛋糕类: package com.raylee.my_new_sprin ...
- 从头认识Spring-2.3 注解装配-@autowired(4)-required(1)
这一章节我们来具体讨论一下@autowired里面的參数required. 1.domain(重点) 蛋糕类: package com.raylee.my_new_spring.my_new_spri ...
- 模拟实现Spring中的注解装配
本文原创,地址为http://www.cnblogs.com/fengzheng/p/5037359.html 在Spring中,XML文件中的bean配置是实现Spring IOC的核心配置文件,在 ...
- Spring学习笔记--使用注解装配
使用@Autowired注解 从Spring2.5开始,最有趣的一种装配Spring Bean的方式是使用注解自动装配Bean的属性.Spring默认禁用注解装配,最简单的启用方式是使用Spring的 ...
- Spring自动装配----注解装配----Spring自带的@Autowired注解
Spring自动装配----注解装配----Spring自带的@Autowired注解 父类 package cn.ychx; public interface Person { public voi ...
- Spring入门(6)-使用注解装配
Spring入门(6)-使用注解装配 本文介绍如何使用注解装配. 0. 目录 使用Autowired 可选的自动装配 使用Qualifier选择 1. 使用Autowired package com. ...
- Spring框架(3)---IOC装配Bean(注解方式)
IOC装配Bean(注解方式) 上面一遍文章讲了通过xml来装配Bean,那么这篇来讲注解方式来讲装配Bean对象 注解方式需要在原先的基础上重新配置环境: (1)Component标签举例 1:导入 ...
随机推荐
- Flume的Collector
Collector的作用是将多个Agent的数据汇总后,加载到Storage中.它的source和sink与agent类似. 数据源(source),如: collectorSource[(port) ...
- WebService 的Description 属性说明(转)
转自:http://exception.thinksaas.cn/0/173/173623.html 在WebMethod的description 中可使用超文本, 举例: 如上图中,红框类的WebS ...
- vue中添加favicon
基于vue-cli 2 首先将favicon.ico图片放在根目录下,通过以下两种方法使其显示正确. 方法一:修改index.html文件 <link rel="shortcut ic ...
- 判断移动端跳转,从移动端来的不跳转。利用localStorage保存状态,window.location.pathname跳转不同的url
手机访问 www.yourdomain.com 跳转,从m.yourdomain.com来的不跳转. 访问www.yourdomain.com/category8, 跳转到m.yourdomain.c ...
- Spider_reg
# 解析 数据的分类 结构化数据 有固定的格式,如 :HTML.XML.JSON 非结构化数据 图片.音频.视频,这类数据一般都存储为二进制 # 正则表达式 re 使用流程 创建编译对象:p = re ...
- SQLITE数据表主键设置Id自增方法
SQLITE数据表主键设置Id自增方法 标签: sqliteintegerinsertnulltableapi 2010-01-12 08:39 35135人阅读 评论(8) 收藏 举报 分类: S ...
- 洛谷 P1334 瑞瑞的木板
P1334 瑞瑞的木板 题目描述 瑞瑞想要亲自修复在他的一个小牧场周围的围栏.他测量栅栏并发现他需要N(1≤N≤20,000)根木板,每根的长度为整数Li(1≤Li≤50,000).于是,他神奇地买了 ...
- Android学习路线(十四)Activity生命周期——停止和重新启动(Stopping and Restarting)一个Activity
正确地停止和重新启动你的activity在activity的生命周期中是一个非常重要的过程.这样可以确保你的用户感觉到你的应用一直都活着而且没有丢失进度.你的activity的停止和重新启动时有几个重 ...
- Iptables-主机防火墙设置
基于Iptables构建主机防火墙 Iptables优点: 数据包过滤机制,它会对数据包包头数据进行分析. 1.1.1 加载相关薄块到内核 [root@centos7 ~]# lsmod | egre ...
- uiautomator_python使用汇总
1.判断按钮状态 if d(resourceId="id",enabled=False): #判断当前按钮是否为未激活状态,为True则为激活状态2.获取toast提示语 d.to ...