您的位置:首页 > 编程语言 > Java开发

spring的自动装配(default-autowire="byName")

2014-02-17 20:56 591 查看
自动装配,官方给出的定义是这样:

Spring IoC容器可以自动装配(autowire)相互协作bean之间的关联关系。因此,如果可能的话,可以自

动让Spring通过检查BeanFactory中的内容,来替我们指定bean的协作者(其他被依赖的bean)。由于

autowire可以针对单个bean进行设置,因此可以让有些bean使用autowire,有些bean不采用。autowire的

方便之处在减少或者消除属性或构造器参数的设置,这样可以给我们的配置文件减减肥

其实,自动装配就是让我们少些几个 <ref ="...">.

我们还是从需求出发.我们假设有4个bean.分别是bean2,bean3,bean4,bean5..其中,bean2里面有后面几个

的引用..我只放出来bean2.java.

package com.test.model;

public class Bean2 {

private Bean3 bean3;

private Bean4 bean4;

private Bean5 bean5;

public Bean3 getBean3() {

return bean3;

}

public void setBean3(Bean3 bean3) {

this.bean3 = bean3;

}

public Bean4 getBean4() {

return bean4;

}

public void setBean4(Bean4 bean4) {

this.bean4 = bean4;

}

public Bean5 getBean5() {

return bean5;

}

public void setBean5(Bean5 bean5) {

this.bean5 = bean5;

}

}

我们看不用自动装配的写法

<bean id="bean2" class="com.test.model.Bean2">

<property name="bean3" ref="bean3"/>

<property name="bean4">

<ref bean="bean4"/>

</property>

<property name="bean5" ref="bean5"/>

</bean>

很明显,下面的几个属性设置是很繁琐的..我们假设使用自动装配.我们只需要这样

<bean id="bean2" class="com.test.model.Bean2" />

里面的装配都不用写.

当然,自动装配必须满足两点

(1)bean2.java里面的属性名字必须和applicationContext.xml里面对应的bean id的名字相同..也就是

private Bean3 bean3; 这个bean3(其实对应的是get,set方法)必须和

<bean id="bean3"
class="com.test.model.Bean3"

parent="abstractBean">

<property name="name" value="Tom" />

<property name="password" value="123" />

</bean> 这个bean3相同.否则不能自动装配

(2)在申明里配置一个属性.default-autowire="byName"(通过名字自动装配)

配置文件为.

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"
default-autowire="byName">

<bean id="abstractBean" abstract="true">

<property name="id">

<value>1000</value>

</property>

<property name="name" value="java" />

</bean>

<bean id="bean2" class="com.test.model.Bean2" />

<bean id="bean3" class="com.test.model.Bean3"

parent="abstractBean">

<property name="name" value="Tom" />

<property name="password" value="123" />

</bean>

<bean id="bean4" class="com.test.model.Bean4" parent="abstractBean" />

<bean id="bean5" class="com.test.model.Bean5">

<property name="age" value="20" />

</bean>

default-autowire="x"

x有4个选择:byName,byType,constructor和autodetect

1. byName:

Service.java

public class Service

{

Source source;

public void setSource(Source
source)

{

this.source = source;

}

}

applicationContext.xml

<beans

...

default-autowire="byName">

<bean id="source"
class="cn.hh.spring.DBCPSource" scope="prototype"/>

<bean id="service" class="cn.hh.spring.Service" scope="prototype">

</bean>

</beans>

cn.hh.spring.DBCPSource实现了Source接口

xml中并没有给 bean service配Source属性,但在beans中设置了autowire="byName",这样配置文件会自

动根据 cn.hh.spring.Service 中的setSource找bean id="Source"的bean
,然后自动配上去,如果没找

到就不装配。

注意:byName的name是java中setXxxx 的Xxxx, 和上面设置的Source source中source拼写毫无关系,完

全可以是

public class Service

{

Source source1;

public void setSource(Source source1)

{

this.source1 = source1;

}

}

2. byType:

Service.java同上

applicationContext.xml

<beans

...

default-autowire="byType">

<bean id="dbcpSource" class="cn.hh.spring.DBCPSource"
scope="prototype"/>

<bean id="service" class="cn.hh.spring.Service" scope="prototype">

</bean>

</beans>

同样没有配置setSource,autowire改成 "byType",配置文件会找实现了Source接口的bean,这里

cn.hh.spring.DBCPSource 实现了Source接口,所以自动装配,如果没找到则不装配。

如果同个配制文件中两个bean实现了Source接口,则报错。

这里的 Type是指setSource(Source source)中参数的类型。

3. constructor:

试图在容器中寻找与需要自动装配的bean的构造函数参数一致的一个或多个bean,如果没找到则抛出异常



4. autodetect:

首先尝试使用constructor来自动装配,然后再使用byType方式。

转载自http://blog.csdn.net/fengyun111999/article/details/6320486
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: