Spring学习笔记——Spring的自动装配

自动装配,官方给出的定义是这样:
Spring IoC容器可以自动装配(autowire)相互协作bean之间的关联关系。因此,如果可能的话,可以自
动让Spring通过检查BeanFactory中的内容,来替我们指定bean的协作者(其他被依赖的bean)。由于
autowire可以针对单个bean进行设置,因此可以让有些bean使用autowire,有些bean不采用。autowire的
方便之处在减少或者消除属性或构造器参数的设置,这样可以给我们的配置文件减减肥
其实,自动装配就是让我们少些几个

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> 

(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方式。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值