我們編寫spring 框架的代碼時候。一直遵循是這樣一個規則:所有在spring中注入的bean 都建議定義成私有的域變量。並且要配套寫上 get 和 set方法。雖然可以通過eclipse等工具來自動生成。但是還是會引起程序閱讀性上的不便。那么既然注解這么強大。是否可以也把他精簡掉呢?
當 然可以。這個標簽就是@Autowired
Spring 2.5 引入了 @Autowired 注釋,它可以對類成員變量、方法及構造函數進行標注,完成自動裝配的工作。
要實現我們要精簡程序的目的。需要這樣來處理:
* 在applicationContext.xml中加入:
<!-- 該 BeanPostProcessor 將自動對標注 @Autowired 的 Bean 進行注入 -->
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
* 修改在原來注入spirng容器中的bean的方法。
在域變量上加上標簽@Autowired,並且去掉 相應的get 和set方法
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.beans.factory.annotation.Autowired;
import com.firemax.test.hibernate.AlcorTCitys;
import com.firemax.test.hibernate.AlcorTCitysDAO;
import com.firemax.test.hibernate.AlcorTCountries;
import com.firemax.test.hibernate.AlcorTCountriesDAO;
import com.firemax.test.hibernate.AlcorTProvinces;
import com.firemax.test.hibernate.AlcorTProvincesDAO;
import com.firemax.test.hibernate.AlcotTDistrict;
import com.firemax.test.hibernate.AlcotTDistrictDAO;
public class CountryService {
private static Log logger = LogFactory.getLog(CountryService.class);
@Autowired
private AlcorTCountriesDAO alcorTCountriesDAO;
@Autowired
private AlcorTProvincesDAO alcorTProvincesDAO;
@Autowired
private AlcorTCitysDAO alcorTCitysDAO;
@Autowired
private AlcotTDistrictDAO alcotTDistrictDAO;
public CountryService(){
}
public void updateCountry(AlcorTCountries alcorTCountries ) throws Exception{
this.alcorTCountriesDAO.update(alcorTCountries);
}
....
//這里去掉了哪些DAO 變量的get 和set 方法。
}
* 在applicatonContext.xml中 把原來 引用的<porpery >標簽也去掉。
<bean id="CountryService" class="com.firemax.test.service.CountryService">
<property name="alcorTCountriesDAO" ref="AlcorTCountriesDAO" />
<property name="alcorTProvincesDAO" ref="AlcorTProvincesDAO" />
<property name="alcorTCitysDAO" ref="AlcorTCitysDAO" />
<property name="alcotTDistrictDAO" ref="AlcotTDistrictDAO" />
</bean>
修改成
<bean id="CountryService" class="com.firemax.test.service.CountryService">
</bean>
當然,我們也可以在構造函數上使用@Auwowired 注解 。如果構造函數有兩個入參,分別是 bean1 和 bean2,@Autowired 將分別尋找和它們類型匹配的 Bean,將它們作為 CountryService (Bean1 bean1 ,Bean2 bean2) 的入參來創建 CountryService Bean。
@Resource
Spring使用@Resource注解完成屬性裝配
使用Field注入(用於注解方式)
注入依賴對象可以采用手工裝配或自動裝配,在實際應用中建議使用手工裝配,因為自動裝配會產生未知情況,開發人員
無法預見最終的裝配結果
1.手工裝配依賴對象
2.自動裝配依賴對象
手工裝配依賴對象,這匯總方式中又有兩種編程方式
1.在xml配置文件中,通過在bean節點下配置 如
<bean id="personDao" class="com.qn.service.impl.PersonDaoBean"></bean>
<bean id="personService" class="com.qn.service.impl.PersonServiceBean">
<constructor-arg index="0" type="com.qn.dao.PersonDao" ref="personDao"></constructor-arg>
<constructor-arg index="1" value="齊寧"></constructor-arg>
</bean>
2.在java代碼中使用@Autowired或@Resource注解方式進行裝配,但我們需要在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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config/>
</beans>
這個配置隱式注冊了多個對注釋進行解析處理的處理器:AutowiredAnnotationBeanPostProcessor,CommonAnnotationBeanPostProcessorPersistenceAnnotationBeanPostProcessor,RequiredAnnotationBeanPostProcessor
注:@Resource注解在spring安裝目錄的common-annotations.jar
.在java代碼中使用@Autowired或@Resource注解方式進行裝配,但我們需要在xml配置文件中配置如下信息
這個配置隱式注冊了多個對注釋進行解析處理的處理器:AutowiredAnnotationBeanPostProcessor,
CommonAnnotationBeanPostProcessorPersistenceAnnotationBeanPostProcessor,RequiredAnnotationBeanPostProcesso
r
注:@Resource注解在spring安裝目錄的common-annotations.jar
在java代碼中使用@Autowired或@Resource注解方式進行裝配,這兩個注解的區別是:@Autowired默認按類型裝配。
@Resource默認按名車裝配,,當找不到與名稱匹配的bean才會按類型裝配
@Autowired
用於字段上面
@Autowired
用於屬性的setter()方法上
@Autowired注解是按類型裝配依賴對象,默認情況下它要求依賴對象必須存在,如果允許null值,可以設置它required的
屬性為false。如果想使用按名稱裝配,可以結合@Qualifier注解一起使用。如下:
@Autowired @Qualifier(“personDaoBean”)
private PersonDao personDao;
@resource注解和@Autowiredyiyang ,也可以標注在字段或屬性的setter方法上,但默認的是按名稱裝配,名稱可以通過
@rResource的name屬性指定,如果沒有指定name屬性,當注解標注在字段上面,即默認去字段的名稱作為bean名稱尋找依
賴對象,當注解標注在屬性的setter方法上,即默認取屬性的名稱作為bean名稱尋找依賴對象
@Resource(name=“personDaoBean”)
private PersonDao personDao;//用於字段上
注意:如果沒有指定name屬性,並且按照默認的名稱仍然找不到依賴對象時,@Resource注解會回退到按類型裝配,但一
旦指定了name屬性,就只能按名稱裝配了
事例
1.定義接口PersonDao
package com.qn.dao;
public interface PersonDao {
void add();
}
2.定義接口PersonService
package com.qn.dao;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public interface PersonService {
void save();
}
3.定義類PersonDaoBean實現PersonDao
package com.qn.service.impl;
import com.qn.dao.PersonDao;
public class PersonDaoBean implements PersonDao{
public void add() {
System.out.println("PersonDaoBean的添加方法");
}
}
4.定義類PersonServiceBean實現PersonService
package com.qn.service.impl;
import javax.annotation.Resource;
import com.qn.dao.PersonDao;
import com.qn.dao.PersonService;
public class PersonServiceBean implements PersonService {
@Resource
private PersonDao personDao;
private String name;
public PersonServiceBean(){}
public PersonServiceBean(PersonDao personDao, String name) {
this.personDao = personDao;
this.name = name;
}
public void save(){
personDao.add();
System.out.println(name);
}
}
5.在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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config/>
<bean name="personDao" class="com.qn.service.impl.PersonDaoBean"></bean>
<bean name="personService" class="com.qn.service.impl.PersonServiceBean"></bean>
</beans>
6.在test中進行測試
package com.qn.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.qn.dao.PersonService;
public class test {
public static void main(String []args){
AbstractApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");
PersonService personService=(PersonService) ctx.getBean("personService");
personService.save();
}
}
結果

這個時候在PersonServiceBean類中
@Resource
private PersonDao personDao;
的注解先是按名稱看beans.xml中看是否存在personDao名稱如果存在則顯示結果

如果beans.xml中沒有personDao名字呢?
如下配置
<bean name="personDaoxxxx" class="com.qn.service.impl.PersonDaoBean"></bean>
<bean name="personService" class="com.qn.service.impl.PersonServiceBean"></bean>
結果

這個時候會按類型查找看是否有類型是PersonDao
當然也可以注明name
@Resource(name="personDaoxxxx")
private PersonDao personDao;
結果

也可以把注解寫到sett方法上
@Resource
public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
}
結果

