@Autowired 的作用是什麼?
@Autowired 是一個註釋,它可以對類成員變量、方法及構造函數進行標註,讓 spring 完成 bean 自動裝配的工作。
@Autowired 默認是按照類去匹配,配合 @Qualifier 指定按照名稱去裝配 bean。
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import blog.service.ArticleService;
import blog.service.TagService;
import blog.service.TypeService;br/>@Controller
public class TestController {
//成員屬性字段使用 @Autowired,無需字段的 set 方法br/>@Autowired
private TypeService typeService;
//set 方法使用 @Autowired
private ArticleService articleService;br/>@Autowired
public void setArticleService(ArticleService articleService) {
this.articleService = articleService;
}
//構造方法使用 @Autowired
private TagService tagService;br/>@Autowired
public TestController(TagService tagService) {
this.tagService = tagService; br/>}
}
@Autowired的用法
這個註解就是spring可以自動幫你把bean裏面引用的對象的setter/getter方法省略,它會自動幫你set/get。
<bean id="userDao" class="..."/>
<bean id="userService" class="...">
<property name="userDao">
<ref bean="userDao"/>br/> </property>
</bean>
這樣你在userService裏面要做一個userDao的setter/getter方法。
但如果你用了@Autowired的話,你只需要在UserService的實現類中聲明即可。
@Autowired
private IUserDao userdao;br/>Spring@Autowired註解與自動裝配
1、 配置文件的方法
我們編寫spring 框架的代碼時候。一直遵循是這樣一個規則:所有在spring中注入的bean 都建議定義成私有的域變量。並且要配套寫上 get 和 set方法。
Boss 擁有 Office 和 Car 類型的兩個屬性:
清單 3. Boss.java
package com.baobaotao;
public class Boss {
private Car car;
private Office office; br/> // 省略 get/setter
@Override
public String toString() {
return "car:" + car + "/n" + "office:" + office;
}
}
System.out.println必須實現toString方法
我們在 Spring 容器中將 Office 和 Car 聲明爲 Bean,並注入到 Boss Bean 中:下面是使用傳統 XML 完成這個工作的配置文件 beans.xml:
清單 4. beans.xml 將以上三個類配置成 Bean
<?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 的信息:
清單 5. 測試類:AnnoIoCTest.java
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AnnoIoCTest {
public static void main(String[] args) {
String[] locations = {"beans.xml"};
ApplicationContext ctx =
new ClassPathXmlApplicationContext(locations);
Boss boss = (Boss) ctx.getBean("boss"); br/> 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的方法。br/> 在域變量上加上標籤@Autowired,並且去掉 相應的get 和set方法
清單 6. 使用 @Autowired 註釋的 Boss.java
package com.baobaotao;
import org.springframework.beans.factory.annotation.Autowired;
public class Boss { br/> @Autowired
private Car car; br/> @Autowired
private Office office;
…
}
* 在applicatonContext.xml中 把原來 引用的<porpery >標籤也去掉。
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- 該 BeanPostProcessor 將自動起作用,對標註 @Autowired 的 Bean 進行自動注入 -->
br/> 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"/> br/> </bean>
</beans>
這樣,當 Spring 容器啓動時,AutowiredAnnotationBeanPostProcessor 將掃描 Spring 容器中所有 Bean,當發現 Bean 中擁有 @Autowired 註釋時就找到和其匹配(默認按類型匹配)的 Bean,並注入到對應的地方中去。
按照上面的配置,XM代理申請www.fx61.com/brokerlist/xm.html,Spring 將直接採用 Java 反射機制對 Boss 中的 car 和 office 這兩個私有成員變量進行自動注入。所以對成員變量使用 @Autowired 後,您大可將它們的 setter 方法(setCar() 和 setOffice())從Boss 中刪除。
當然,您也可以通過 @Autowired 對方法或構造函數進行標註,如果構造函數有兩個入參,分別是 bean1 和bean2,@Autowired 將分別尋找和它們類型匹配的 Bean,將它們作爲 CountryService (Bean1 bean1 ,Bean2 bean2) 的入參來創建CountryService Bean。來看下面的代碼: 對方法
package com.baobaotao;
public class Boss {
private Car car;
private Office office; br/> @Autowired
public void setCar(Car car) {
this.car = car; br/> }
@Autowired
public void setOffice(Office office) {
this.office = office; br/> }
…
}
這時,@Autowired 將查找被標註的方法的入參類型的 Bean,並調用方法自動注入這些 Bean。而下面的使用方法則對構造函數進行標註:
package com.baobaotao;
public class Boss {
private Car car;
private Office office; br/> @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。
@Autowired 的作用和用法是什麼?
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.