一、根據(jù)名稱自動(dòng)裝配屬性(工程spring_autowire_byName
)
這種方式一般用在開(kāi)發(fā)階段偿警,用于提供我們的開(kāi)發(fā)效率躏救。我們通過(guò)一個(gè)示例進(jìn)行說(shuō)明。
相關(guān)實(shí)體:
Bean2.java
private Bean3 bean3;
private Bean4 bean4;
private Bean5 bean5;
Bean3.java
private int id;
private String name;
private String password;
Bean4.java
private int id;
private String name;
Bean5.java
private int age;
配置:applicationContext-beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"
default-autowire="buName">
<!-- <bean id="bean2" class="com.bjsxt.spring.Bean2">
<property name="bean3" ref="bean3" />
<property name="bean4">
<ref bean="bean4" />
</property>
<property name="bean5" ref="bean5" />
</bean> -->
<bean id="bean2" class="com.bjsxt.spring.Bean2" />
<bean id="bean5" class="com.bjsxt.spring.Bean5">
<property name="age" value="20" />
</bean>
</beans>
applicationContext-other.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean id="beanAbstract" abstract="true">
<property name="id" value="1000"/>
<property name="name" value="Jack"/>
</bean>
<bean id="bean3" class="com.bjsxt.spring.Bean3" parent="beanAbstract">
<property name="name" value="Tom"/>
<property name="password" value="123"/>
</bean>
<bean id="bean4" class="com.bjsxt.spring.Bean4" parent="beanAbstract"/>
</beans>
說(shuō)明:這里我們之所以可以這樣簡(jiǎn)單的配置Bean2類螟蒸,是因?yàn)?strong>我們?cè)贐ean2類中的屬性名字和Bean3盒使、Bean4、Bean5這些類中配置的id號(hào)是名字是一樣的七嫌,同時(shí)提供了getter和setter方法少办,這樣spring就會(huì)根據(jù)這個(gè)名字去找相應(yīng)的類,然后實(shí)例化之后注入進(jìn)來(lái)诵原,當(dāng)然這樣配置之后是不會(huì)生效的英妓,我們需要在<beans>
標(biāo)簽中配置default-autowire="byName"
。這種方式一般用在開(kāi)發(fā)階段绍赛,在項(xiàng)目發(fā)布時(shí)不推薦這樣做蔓纠,因?yàn)樵谂渲梦募锌床坏揭粋€(gè)bean的屬性,不利于維護(hù)吗蚌,一般在開(kāi)發(fā)完之后再改成一般方式腿倚。
測(cè)試:AutowireTest.java
package com.bjsxt.spring;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import junit.framework.TestCase;
public class AutowireTest extends TestCase {
private BeanFactory factory;
@Override
protected void setUp() throws Exception {
factory = new ClassPathXmlApplicationContext("applicationContext-*.xml");
}
public void testAutowire() {
Bean2 bean2 = (Bean2)factory.getBean("bean2");
System.out.println("bean2.bean3.id=" + bean2.getBean3().getId());
System.out.println("bean2.bean3.name=" + bean2.getBean3().getName());
System.out.println("bean2.bean3.password=" + bean2.getBean3().getPassword());
System.out.println("bean2.bean4.id=" + bean2.getBean4().getId());
System.out.println("bean2.bean4.name=" + bean2.getBean4().getName());
System.out.println("bean2.bean5.age=" + bean2.getBean5().getAge());
}
}
二、根據(jù)類型自動(dòng)裝配(工程spring_autowrire_byType
)
這里所有的類和配置都和上面一樣蚯妇,但是在<beans>
標(biāo)簽中配置default-autowire="byType"
敷燎,此時(shí)spring會(huì)根據(jù)各個(gè)類的實(shí)際類型去找相關(guān)的實(shí)體類,id可以隨便起名或者忽略箩言。我們推薦使用這種方式進(jìn)行配置硬贯。