1 配置文件的方法
我們編寫spring框架的代碼時候。一直遵循是這樣一個規則:所有在spring中注入的bean都建議定義成私有的域變量。並且要配套寫上get和set方法。
Boss擁有Office和Car類型的兩個屬性:
|
1
2
3
4
5
6
7
8
9
10
11
|
public class Boss
{
private Car car;
private Office office;
//省略 get/setter
@Override
public String toString()
{
return "car:" + car + "/n" + "office:" + office;
}
}
|
我們在Spring容器中將Office和Car聲明為Bean,並注入到Boss Bean中,下面是使用傳統XML完成這個工作的配置文件beans.xml:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<?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.5.xsd">
<bean id="boss" class="Boss">
<property name="car" ref="car"/>
<property name="office" ref="office" />
</bean>
<bean id="office" class="Office">
<property name="officeNo" value="002"/>
</bean>
<bean id="car" class="Car" scope="singleton">
<property name="brand" value="紅旗CA72"/>
<property name="price" value="2000"/>
</bean>
</beans>
|
當我們運行以下代碼時,控制台將正確打出boss的信息:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test
{
public static void main(String[] args)
{
String[] locations = {"beans.xml"};
ApplicationContext ctx = new ClassPathXmlApplicationContext(locations);
Boss boss = (Boss) ctx.getBean("boss");
System.out.println(boss);
}
}
|
2 @Autowired的使用
Spring 2.5引入了@Autowired注釋,它可以對類成員變量、方法及構造函數進行標注,完成自動裝配的工作。通過@Autowired的使用來消除set,get方法。
下面是@Autowired的定義:
|
1
2
3
4
5
6
7
8
|
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.CONSTRUCTOR, ElementType.FIELD, ElementType.METHOD})
public @interface Autowired
{
//是否必須滿足依賴性檢查,默認時,凡是應用了@Autowired注解的屬性和方法都必須找到合適的協作者,否則Spring容器會拋出異常,
//通過調整required屬性取值能夠改變這一行為。
boolean required() default true;
}
|
注意:@Autowired注解能夠作用於構建器、屬性、方法。這里的方法不局限於設值方法,即setter方法,常見的各種方法都可以應用這一注解。
要使用@Autowired實現我們要精簡程序的目的,需要這樣來處理:
在applicationContext.xml中加入:
<!-- 該 BeanPostProcessor 將自動對標注 @Autowired 的 Bean 進行注入 -->
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
Spring通過一個BeanPostProcessor對@Autowired進行解析,所以要讓@Autowired起作用必須事先在Spring容器中聲明AutowiredAnnotationBeanPostProcessor Bean。
修改在原來注入Spirng容器中的bean的方法,在域變量上加上標簽@Autowired,並且去掉相應的get和set方法。
使用 @Autowired 注釋的 Boss.java
|
1
2
3
4
5
6
7
8
|
import org.springframework.beans.factory.annotation.Autowired;
public class Boss
{
@Autowired
private Car car;
@Autowired
private Office office;
}
|
在applicatonContext.xml中把原來引用的<porpery>標簽也去掉。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<?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.5.xsd">
<!-- 該 BeanPostProcessor 將自動起作用,對標注 @Autowired 的 Bean 進行自動注入 -->
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
<!-- 移除 boss Bean 的屬性注入配置的信息 -->
<bean id="boss" class="Boss"/>
<bean id="office" class="Office">
<property name="officeNo" value="001"/>
</bean>
<bean id="car" class="Car" scope="singleton">
<property name="brand" value="紅旗 CA72"/>
<property name="price" value="2000"/>
</bean>
</beans>
|
這樣,當 Spring容器啟動時,AutowiredAnnotationBeanPostProcessor將掃描Spring容器中所有Bean,當發現Bean中擁有@Autowired 注釋時就找到和其匹配(默認按類型匹配)的Bean,並注入到對應的地方中去。
3 @Autowired注入規則
@Autowired默認是按照byType進行注入的,但是當byType方式找到了多個符合的bean,又是怎么處理的?Autowired默認先按byType,如果發現找到多個bean,則又按照byName方式比對,如果還有多個,則報出異常。
例子:
@Autowired
private Car redCar;
1. spring先找類型為Car的bean
2. 如果存在且唯一,則OK;
3. 如果不唯一,在結果集里,尋找name為redCar的bean。因為bean的name有唯一性,所以,到這里應該能確定是否存在滿足要求的bean了
@Autowired也可以手動指定按照byName方式注入,使用@Qualifier標簽,例如:
@Autowired()
@Qualifier("baseDao" )
因為bean的name具有唯一性,理論上是byName會快一點,但spring默認使用byType的方式注入。另外補充一點:@Resource(這個注解屬於J2EE的)的標簽,默認是按照byName方式注入的。
