1 配置檔案的方法
我們編寫spring 架構的代碼時候。一直遵循是這樣一個規則:所有在spring中注入的bean 都建議定義成私有的域變量。并且要配套寫上 get 和 set方法。
Boss 擁有 Office 和 Car 類型的兩個屬性: 清單 3. Boss.java [java] view plain copy print ?
System.out.println必須實作toString方法 我們在 Spring 容器中将 Office 和 Car 聲明為 Bean,并注入到 Boss Bean 中:下面是使用傳統 XML 完成這個工作的配置檔案 beans.xml: 清單 4. beans.xml 将以上三個類配置成 Bean [xhtml] view plain copy print ?
當我們運作以下代碼時,控制台将正确打出 boss 的資訊: 清單 5. 測試類:AnnoIoCTest.java [java] view plain copy print ?
|
2 @Autowired
Spring 2.5 引入了 @Autowired 注釋,它可以對類成員變量、方法及構造函數進行标注,完成自動裝配的工作。 通過 @Autowired的使用來消除 set ,get方法。
要實作我們要精簡程式的目的。需要這樣來處理:
* 在applicationContext.xml中加入:
[c-sharp] view plain copy print ?
- <!-- 該 BeanPostProcessor 将自動對标注 @Autowired 的 Bean 進行注入 -->
- <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
Spring 通過一個 BeanPostProcessor 對 @Autowired 進行解析,是以要讓 @Autowired 起作用必須事先在 Spring 容器中聲明 AutowiredAnnotationBeanPostProcessor Bean。
* 修改在原來注入spirng容器中的bean的方法。
在域變量上加上标簽@Autowired,并且去掉 相應的get 和set方法
清單 6. 使用 @Autowired 注釋的 Boss.java
[java] view plain copy print ?
- package com.baobaotao;
- import org.springframework.beans.factory.annotation.Autowired;
- public class Boss {
- @Autowired
- private Car car;
- @Autowired
- private Office office;
- …
- }
* 在applicatonContext.xml中 把原來 引用的<porpery >标簽也去掉。
[xhtml] view plain copy print ?
- <?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="com.baobaotao.Boss"/>
- <bean id="office" class="com.baobaotao.Office">
- <property name="officeNo" value="001"/>
- </bean>
- <bean id="car" class="com.baobaotao.Car" scope="singleton">
- <property name="brand" value=" 紅旗 CA72"/>
- <property name="price" value="2000"/>
- </bean>
- </beans>
這樣,當 Spring 容器啟動時,AutowiredAnnotationBeanPostProcessor 将掃描 Spring 容器中所有 Bean,當發現 Bean 中擁有 @Autowired 注釋時就找到和其比對(預設按類型比對)的 Bean,并注入到對應的地方中去。
按照上面的配置,Spring 将直接采用 Java 反射機制對 Boss 中的 car 和 office 這兩個私有成員變量進行自動注入。是以對成員變量使用 @Autowired 後,您大可将它們的 setter 方法(setCar() 和 setOffice())從 Boss 中删除。
當然,您也可以通過 @Autowired 對方法或構造函數進行标注,如果構造函數有兩個入參,分别是 bean1 和 bean2,@Autowired 将分别尋找和它們類型比對的 Bean,将它們作為 CountryService (Bean1 bean1 ,Bean2 bean2) 的入參來建立 CountryService Bean。來看下面的代碼: 對方法
[java] view plain copy print ?
- package com.baobaotao;
- public class Boss {
- private Car car;
- private Office office;
- @Autowired
- public void setCar(Car car) {
- this.car = car;
- }
- @Autowired
- public void setOffice(Office office) {
- this.office = office;
- }
- …
- }
這時,@Autowired 将查找被标注的方法的入參類型的 Bean,并調用方法自動注入這些 Bean。而下面的使用方法則對構造函數進行标注:
[java] view plain copy print ?
- package com.baobaotao;
- public class Boss {
- private Car car;
- private Office office;
- @Autowired
- public Boss(Car car ,Office office){
- this.car = car;
- this.office = office ;
- }
- …
- }
由于 Boss() 構造函數有兩個入參,分别是 car 和 office,@Autowired 将分别尋找和它們類型比對的 Bean,将它們作為 Boss(Car car ,Office office) 的入參來建立 Boss Bean。