从头认识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:导入 ...
随机推荐
- 我的第一个JS组件-跨浏览器JS调试工具
武汉九天鸟-p2p网贷系统开发-互联网应用软件开发 公司官网:http://jiutianniao.com 社交问答:http://ask.jiutianniao.com 最近,在看公司一个JS大牛 ...
- JavaScript原型及原型链
代码一: <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF- ...
- Django环境搭建(二)
web框架 本质就是socket服务端 socket服务端:是计算机科学家在TCP/IP基础上进行封装,暴露出一个接口socket,就是一个收发数据的一个接口. 对于真实的web程序来说分为两部分:服 ...
- Java Web学习总结(5)——HttpServletResponse对象详解
一.HttpServletResponse常见应用--生成验证码 1.1.生成随机图片用作验证码 生成图片主要用到了一个BufferedImage类, 生成随机图片范例: package gacl.r ...
- UIViewController所有API的学习。
<欢迎大家加入iOS开发学习交流群:QQ529560119> /* UIViewController is a generic controller base class tha ...
- Android之——图片的内存优化
转载请注明出处:http://blog.csdn.net/l1028386804/article/details/46972817 1. 对图片本身进行操作 尽量不要使用 setImageBitmap ...
- [C/C++]_[0基础]_[static_cast,reinterpret_cast,dynimic_cast的使用场景和差别]
场景: 1. C++的对象差别于C的原因是他们能够有继承关系, 方法有重载, 覆盖关系等, 他们的对象内存数据结构因此也比較复杂. 2. 非常多情况下我们须要一个父类来存储子类的指针对象进行通用方法的 ...
- ajax中打开新页面使用window.open方法被拦截的解决方法
$('.testA').unbind('click').bind('click',function(){ var result=""; $.ajax({ url:'http://l ...
- iOS_04_数据类型、常量、变量
一.数据 1.什么是数据 * 生活中时时刻刻都在跟数据打交道,比如体重数据.血压数据.股价数据等.在我们使用计算机的过程中,会接触到各种各样的数据,有文档数据,图片数据,视频数据,还有聊天QQ产生的文 ...
- 修改IIS7并发连接数目限制
转自原文 修改IIS7并发连接数目限制 1. 调整IIS 7应用程序池队列长度 由原来的默认1000改为65535. IIS Manager > ApplicationPools > Ad ...