为了解决配置文件中面出现多个同类型的Bean而byType无法匹配的问题。引入了primary和autowire-candidate属性。

1.primary

因为全部bean默认的primary都是true。因此笔者觉得这个属性没有太大的用处

2.autowire-candidate

这个属性的意思是,是否排除自己,假设排除了自己。 那么当使用byType的时候,这个Bean就不列入候选bean里面。看以下的样例:

(1)domain

蛋糕类:(不变)

package com.raylee.my_new_spring.my_new_spring.ch02.topic_1_3;

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_3;

public class Chief {
private Cake cake = null; private String name = ""; public Cake getCake() {
return cake;
} public String getName() {
return name;
} public Cake makeOneCake() {
System.out.println(getName() + " make " + getCake().getName());
return cake;
} public void setCake(Cake cake) {
this.cake = cake;
} public void setName(String name) {
this.name = name;
} }

(2)測试类:(不变)

package com.raylee.my_new_spring.my_new_spring.ch02.topic_1_3;

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_3/ApplicationContext-test.xml" })
public class ChiefTest { @Autowired
private ApplicationContext applicationContext; @Test
public void testChief() {
Chief jack = (Chief) applicationContext.getBean("jack");
jack.makeOneCake();
Chief rose = (Chief) applicationContext.getBean("rose");
rose.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_3.Cake"
p:name="blueberryCheeseCake" scope="prototype" /> <bean id="cake"
class="com.raylee.my_new_spring.my_new_spring.ch02.topic_1_3.Cake"
p:name="cake" scope="prototype" autowire-candidate="false" /> <bean id="jack"
class="com.raylee.my_new_spring.my_new_spring.ch02.topic_1_3.Chief"
p:name="jack">
<property name="cake" ref="cake" />
</bean> <bean id="rose"
class="com.raylee.my_new_spring.my_new_spring.ch02.topic_1_3.Chief"
autowire="byType" p:name="rose" />
</beans>

上面的配置文件使用autowire-candidate排除了cake这个bean,因此会出现以下的输出。

測试输出:

jack make cake
rose make blueberryCheeseCake

总结:这一章节主要介绍解决配置文件中面出现多个同类型的Bean而byType无法匹配的问题。

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

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

从头认识Spring-2.1 自己主动装配(2)-byType(2)的更多相关文章

  1. spring自己主动装配Bean属性

    spring提供了3种类型的自己主动装配 byName:把与Bean的属性具有同样名字(或者ID)的其它Bean自己主动装配到Bean的相应属性中. byType:把与Bean的属性具有同样类型的其它 ...

  2. Spring 新手教程(三) 注入和自己主动装配

         Spring注入是指在启动Spring容器载入bean配置的时候.对类变量的赋值. 两种经常使用注入方式:设值注入和构造注入 以下就这部分知识看代码以及代码中的注解: 1.首先看Spring ...

  3. spring 配置bean-自己主动装配

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/qilixiang012/article/details/28260477 概要:(蓝色为本节所讲) ...

  4. Spring -- Bean自己主动装配&amp;Bean之间关系&amp;Bean的作用域

    对于学习spring有帮助的站点:http://jinnianshilongnian.iteye.com/blog/1482071 Bean的自己主动装配 Spring IOC 容器能够自己主动装配 ...

  5. Spring中自己主动装配

    自己主动装配 在我们了解过constructor-arg和property装配中.都须要配置对应的属性和值或者引用,假设在比較复杂的项目中.就会使得XML的配置变得复杂,自己主动装配能够使用较少的配置 ...

  6. [原创]java WEB学习笔记99:Spring学习---Spring Bean配置:自动装配,配置bean之间的关系(继承/依赖),bean的作用域(singleton,prototype,web环境作用域),使用外部属性文件

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  7. (转)java之Spring(IOC)注解装配Bean详解

    java之Spring(IOC)注解装配Bean详解   在这里我们要详细说明一下利用Annotation-注解来装配Bean. 因为如果你学会了注解,你就再也不愿意去手动配置xml文件了,下面就看看 ...

  8. Spring学习(六)-----Spring使用@Autowired注解自动装配

    Spring使用@Autowired注解自动装配 在上一篇 Spring学习(三)-----Spring自动装配Beans示例中,它会匹配当前Spring容器任何bean的属性自动装配.在大多数情况下 ...

  9. Spring入门2. IoC中装配Bean

    Spring入门2. IoC中装配Bean 20131125 前言: 上一节学习了Spring在JavaProject中的配置,通过配置文件利用BeanFactory和ApplicationConte ...

  10. Spring 自动注册及自动装配

    Spring支持三种注册Bean及装配Bean的方式: 显式地在Java代码中注册及装配 显示地在Xml文件中注册及装配 隐式地装配,即自动注册及装配 这三种方式可以混合使用.选择哪种更多地是看个人品 ...

随机推荐

  1. Spring 中的事务操作、注解、以及 XML 配置

    事务 事务全称叫数据库事务,是数据库并发控制时的基本单位,它是一个操作集合,这些操作要么不执行,要么都执行,不可分割.例如我们的转账这个业务,就需要进行数据库事务的处理. 转账中至少会涉及到两条 SQ ...

  2. 【Ray Tracing in One Weekend 超详解】 光线追踪1-8 自定义相机设计

    今天,我们来学习如何设计自定义位置的相机 ready 我们只需要了解我们之前的坐标体系,或者说是相机位置 先看效果   Chapter10:Positionable camera 这一章我们直接用概念 ...

  3. Gift动图分解小工具

    gif 动图分解小工具 Overview 因为自己有时候需要将一些gif图片分解,但是没有在网上找到合适的工具,所有就自己写了一个,在这里与大家分享,其实实现很简单,是通过C#实现的.文章下方有下载链 ...

  4. 将NX模型导入Process Designer的方法

    如何把一个有焊点的零件从nx中输入到process designer 中?   用户在NX中做了一个prt文件, 想把它输入到process designer中, 并且包括焊点信息, 该如何做? 解决 ...

  5. WatermarkMaker

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Dr ...

  6. lvs+keepalived 02

    LVS keepalived 高可用负载均衡 环境 IP HOSTNAME Describe 192.168.100.30 lvs01 主负载 192.168.100.31 lvs02 备负载 192 ...

  7. Codeforces Round #375 (Div. 2) B. Text Document Analysis 模拟

    B. Text Document Analysis 题目连接: http://codeforces.com/contest/723/problem/B Description Modern text ...

  8. 使用CefSharp在.Net程序中嵌入Chrome浏览器(八)——Cookie

    CEF中的Cookie是通过CookieManager来管理的,可以用它来设置发送的Cookie. 发送Cookie 发送Cookie的一个基本示例如下: var cookieManager = _c ...

  9. Eclipse Mark Occurrences

    Mark Occurrences The Mark Occurrences feature enables you to see where an element is referenced by s ...

  10. 解决svn中文乱码的问题

    需要的工具:sqlitexiaz 工具下载: 链接:https://pan.baidu.com/s/1cz1Pvw 密码:yp64 1 首先在项目的根目录下,找到.svn(如果找不到,需要设置将隐藏文 ...