Spring中@Autowired注解與自動裝配


1 使用配置文件的方法來完成自動裝配
我們編寫spring 框架的代碼時候。一直遵循是這樣一個規則:所有在spring中注入的bean 都建議定義成私有的域變量。並且要配套寫上 get 和 set方法。
比如:Boss 擁有 Office 和 Car 類型的兩個屬性:
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:
beans.xml 將以上三個類配置成 Bean,然后Spring容器會自動裝配起來。
<?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="com.baobaotao.Boss">
<property name="car" ref="car"/>
<property name="office" ref="office" />
</bean>
<bean id="office" class="com.baobaotao.Office">
<property name="officeNo" value="002"/>
</bean>
<bean id="car" class="com.baobaotao.Car" scope="singleton">
<property name="brand" value=" 紅旗 CA72"/>
<property name="price" value="2000"/>
</bean>
</beans>
當我們運行以下代碼時,控制台將正確打出 boss 的信息:
public class AnnoIoCTest {

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);
}
}
這說明 Spring 容器已經正確完成了 Bean 創建和裝配的工作。

2 使用@Autowired 注解來完成自動裝配
Spring 2.5 引入了 @Autowired 注釋,它可以對類成員變量、方法及構造函數進行標注,完成自動裝配的工作。 通過 @Autowired的使用來消除 set ,get方法,簡化程序代碼。
要實現我們要精簡程序的目的。需要這樣來處理:
① 在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方法
public class Boss {

@Autowired
private Car car;

@Autowired
private Office office;


}

③在applicatonContext.xml中 把原來引用的<porpery >標簽也去掉。
<?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 對方法或構造函數進行標注。
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。而下面的使用方法則對構造函數進行標注:
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。

注意:在第一步中,在applicationContext.xml中加入的那個bean是對@Autowired注解進行解析的處理器,但只能實現對標注了@Autowired 的 Bean 進行注入,無法對其他注解的注入,所以如果項目中還使用了別的注解的話,是不推薦這樣做的!一般在applicationContext.xml中常用開啟注解配置的是顯式的配置<context:annotation-config/>,就可以開啟多個注解,因為該配置隱式注冊了多個對注解進行解析的處理器,如下列舉 :
AutowiredAnnotationBeanPostProcessor CommonAnnotationBeanPostProcessor
PersistenceAnnotationBeanPostProcessor RequiredAnnotationBeanPostProcessor
更通用的方法就是:顯式的配置<context:component-scan base-package=" "/>。因為component-scan標簽默認情況下自動掃描指定路徑下的包(含所有子包),將帶有@Component、@Repository、@Service、@Controller標簽的類自動注冊到spring容器。對標記了@Required、@Autowired(spring);@PostConstruct、@PreDestroy、@Resource(JSR250);@WebServiceRef(JAX-WS);@EJB(EJB3); @PersistenceContext、@PersistenceUnit(JPA)等注解的類進行對應的操作使注解生效(這其實就包含了annotation-config標簽的作用,所以配置了<context:component-scan base-package=""/>之后,便無需再配置<context:annotation-config/>)。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM