Spring3之 bean AutoWire

本文深入探讨Spring框架中的自动装配机制,介绍如何通过不同配置减少显式依赖注入的需求,包括按名称、类型等自动装配的方式及其优缺点。同时,文章还提供了具体的测试代码示例。

Autowiring collaborators  自动装配

Spring通过检查BeanFactory中的内容,来替指定其他被依赖的bean

优点:

1、显著减少配置的数量

2、以使配置与java代码同步更新

XML配置过程中可<bean>标签中指定autowire属性,它有5个值(3中官方英文文档中只有前4个):

no :No autowiringbean之间的关系必须通过ref标签来指定

byName :根据属性名自动装配。此选项将检查容器并根据名字查找与属性完全一致的bean,并将其与属性自动装配。

byType :如果容器中存在一个与指定属性类型相同的bean,那么将与该属性自动装配。如果存在多个该类型的bean,那么将会抛出异常,并指出不能使用byType方式进行自动装配。若没有找到相匹配的bean,则什么事都不发生,属性也不会被设置。如果你不希望这样,那么可以通过设置dependency-check="objects"Spring抛出异常。

constructor :byType的方式类似,不同之处在于它应用于构造器参数。如果在容器中没有找到与构造器参数类型一致的bean,那么将会抛出异常。

@deprecated,autowire="autodetect"这个在spring2.5文档中还有见,但是在3中已经没了,3中的XML配置中添加些值会报错。

autodetect :通过bean类的自省机制(introspection)来决定是使用constructor还是byType方式进行自动装配。如果发现默认的构造器,那么将使用byType方式。

但是在spring3的xsd文件中找到default这个值项,应该是替代autodetect 的吧:

spring3.0Xsd 文件中

default-autowire的属性只有no ,byName ,byType ,byType 这里既没有autodetect  也没有default

default-autowire-candidates 中值的设置解释:

A default bean name pattern for identifying autowire candidates:

e.g. "*Service", "data*", "*Service*", "data*Service".

Also accepts a comma-separated list of patterns: e.g. "*Service,*Dao".

See the documentation for the 'autowire-candidate' attribute of the

'<bean/>' element for the semantic details of autowire candidate beans.

但是,我测试了下,发现并没有作用?搜到一外文网页上说如同上写法,但是那是07年的帖子了。是不是spring3中这个配置没了作用?

/*

dependency-check依赖检查自动装配中:autowire-candidate属性设为false,这样容器在查找自动装配对象时将不考虑该bean

(这块在spring3中已经移除了?文档中没有找到

依赖检查也可以针对每一个bean进行设置。依赖检查默认为not,它有几种不同的使用模式,在xml配置文件中,可以在bean定义中为dependency-check属性使用以下几种值:

none: 没有依赖检查,如果bean的属性没有值的话可以不用设置。

simple: 对于原始类型及集合(除其他被依赖的bean外的一切东西)执行依赖检查

object: 仅对其他被依赖的bean执行依赖检查

all: 对其他被依赖的bean,原始类型及集合执行依赖检查

也找不到:default-dependency-check

*/

bean排除在自动装配之外

一、当采用XML格式配置bean时,<bean/>元素的 autowire-candidate属性可被设为false,这样容器在查找自动装配对象时将不考虑该bean

、使用对bean名字进行模式匹配来对自动装配进行限制。其做法是在<beans/>元素的'default-autowire-candidates'属性中进行设置。比如,将自动装配限制在名字以'Repository'结尾的bean,那么可以设置为"*Repository“。对于多个匹配模式则可以使用逗号进行分隔。注意,如果在bean定义中的'autowire-candidate'属性显式的设置为'true' 或 'false',那么该容器在自动装配的时候优先采用该属性的设置,而模式匹配将不起作用。

<!--EndFragment-->

 

测试代码:

com.spring305.test.autowire.po.IdCard.java

public class IdCard {

	private String cardNo;
	private String desc;

。。。getter .setter
}

 com.spring305.test.autowire.po.People.java

public class People {

	private int id;
	private String name;
	private IdCard idCard;

	public People(){
	}
	
	//autowire="constructor" 要有此构造方法
	public People(IdCard idCard){
		this.idCard = idCard;
	}
getter setter
}

 

src/testAutoWire.xml

<beans.... http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
     "   default-autowire-candidates="*idCard" >
     <!--  default-autowire-candidates="*ple*,*ard*" 配置了,但是没有感觉到作用,并没有限制相应的 -->
     <!--beans标签中加上:  default-autowire="default"  or default-autowire="byName"  (后面的是延迟加载,就不是了)default-lazy-init="true" -->
     
    <bean id="idCard" class="com.spring305.test.autowire.po.IdCard"></bean>
    <bean id="people" class="com.spring305.test.autowire.po.People" autowire="byName"></bean>
    
    <!--
     dependency-check="object"  这个在spring3中貌似没了
     
     单个autowire
    <bean id="people" class="com.spring305.test.autowire.po.People"  autowire="byName"></bean>
     
     <bean id="people" class="com.spring305.test.autowire.po.People"   autowire="constructor"></bean>
     
     autowire-candidate="false" 不会自动注入到其它的bean中
    <bean id="idCard" class="com.spring305.test.autowire.po.IdCard"  autowire-candidate="false"></bean>
     -->

 测试:

	//@Test//xml
	public void AutoWireTestXML() {
		ApplicationContext context =  new ClassPathXmlApplicationContext("testAutoWire.xml");
		People people = (People) context.getBean("people");
		IdCard idCard = people.getIdCard();
		// autowire="byName" 当people bean上没有加这个标签时,打印值为null,加后则打印出了对象
		System.out.println(idCard);
	}

 

annotation测试:

com.spring305.test.autowire.po.IdCardAnnotation.java

@Configuration
public class IdCardAnnotation {

	private String cardNo;
	private String desc;

。。。。getter...setter
}

 com.spring305.test.autowire.po.PeopleAnnotation.java

@Configuration
public class PeopleAnnotation {

	private int id;
	private String name;
	private IdCardAnnotation  idCardAnnotation;

	@Autowired(required = false)
	//@Qualifier("idCardAnnotation")
	public void setIdCardAnnotation(@Qualifier("idCardAnnotation")IdCardAnnotation idCardAnnotation) {
		this.idCardAnnotation = idCardAnnotation;
	}

getter.setter
}

测试:

@Test
	public void AutoWireTestAnnotation() {
		ApplicationContext context =  new AnnotationConfigApplicationContext(IdCardAnnotation.class,PeopleAnnotation.class);
		PeopleAnnotation people = (PeopleAnnotation) context.getBean("peopleAnnotation");
		IdCardAnnotation idCard = people.getIdCardAnnotation();
		System.out.println(idCard);
	}

  

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值