天天看點

Spring的bean注入方式

Spring中依賴注入有三種注入方式:

一、構造器注入;

二、設值注入(setter方式注入);

三、Feild方式注入(注解方式注入)。

一、構造器注入

     構造器注入顧名思義就是在程式元件中實作構造器,構造器可以是一個也可以是多個。廢話不多說,直接上代碼。

Java代碼  

Spring的bean注入方式
  1. package cn.glzaction.service.impl;  
  2. import java.util.List;  
  3. import cn.glzaction.service.interfaces.PersonDaoIF;  
  4. import cn.glzaction.service.interfaces.PersonServiceIF;  
  5. public class PersonServiceBean implements PersonServiceIF{  
  6. //自定義類  
  7.     private PersonDaoIF personDaoBean;  
  8. //String類型  
  9.     private String name;  
  10. //集合類型    
  11.     private List list;  
  12. //構造器     
  13.     public PersonServiceBean(PersonDaoBean personDaoBean,String name,List list){  
  14.         this.personDaoBean = personDaoBean;  
  15.         this.name = name;  
  16.         this.list = list;  
  17.     }  
  18. //方法,用于顯示  
  19.     public void display(){  
  20.         personDaoBean.add();  
  21.         System.out.println(name);  
  22.         System.out.println(list);  
  23.     }  
  24. }  

上面的代碼中構造器要注入三個參數,同時這三個參數也是三種不同的類型,自定義類、String類型,集合類型,其中自定義類PersonDaoBean 具體的實作我們這裡就不累述了,因為這不是重點。下面我們再來看一下在Spring的配置檔案中如何來配置。

Xml代碼  

Spring的bean注入方式
  1. <bean id="personDao" class="cn.glzaction.service.impl.PersonDaoBean"></bean >  
  2. <!--構造器方式注入-->  
  3. <bean id="personService" class="cn.glzaction.service.impl.PersonServiceBean">  
  4.     <constructor-arg index="0" type="cn.glzaction.service.impl.PersonDaoBean" ref="personDao"/>  
  5.     <constructor-arg index="1" type="java.lang.String" value="glzaction"/>  
  6.     <constructor-arg index="2" type="java.util.List">  
  7.         <list>  
  8.             <value>list1</value>  
  9.             <value>list2</value>  
  10.             <value>list3</value>  
  11.         </list>  
  12.     </constructor-arg>  
  13. </bean>  

      java代碼如何實作構造器我想大家一定都很清楚,否則的話就有點講不過去了,呵呵,是以這裡我也就不加以說明了,主要說一下上面的xml配置。id為“personDao”的bean是PersonServiceBean的私有屬性,它的注入是采用無參構造器的注入方式注入的,這也不詳細說明。主要說一下id為“personService”類的構造器注入。<coustructor-arg>是構造器标簽元素,通過設定它的屬性可以往構造器傳遞參數,index屬性值表示要設定的參數在構造器形參中的索引順序,例如上面的配置,list是第三個參數,是以它對應的索引為2,index是可選屬性,所謂可選并不是說在任何情況下都可以不使用,要視具體情況而定,type為參數的類型,這個也是可選參數。還有兩個很重要的屬性就是ref和value,如果注入的是bean,就要使用ref,ref的值就是對應的bean。如果注入的是基本類型或者string類型就用value,直接将對應的值填入即可。

二、設值注入(setter方式注入)

設值注入就是通過setXxxx方法将bean注入到元件中,自定義類如下

Java代碼  

Spring的bean注入方式
  1. package cn.glzaction.service.impl;  
  2. import cn.glzaction.service.interfaces.PersonDaoIF;  
  3. import java.util.*;  
  4. public class PersonDaoBean implements PersonDaoIF {  
  5.     private String name;  
  6.     private Integer id;  
  7.     private List list;  
  8.     private Map map;  
  9.     public void setName(String name) {  
  10.         this.name = name;  
  11.     }  
  12.     public void setId(Integer id) {  
  13.         this.id = id;  
  14.     }  
  15.     public void setList(List list) {  
  16.         this.list = list;  
  17.     }  
  18.     public void setMap(Map map) {  
  19.         this.map = map;  
  20.     }  
  21.     @Override  
  22.     public void add() {  
  23.         // TODO Auto-generated method stub  
  24.         System.out.println(map);  
  25.         System.out.println(list);  
  26.         System.out.println(id);  
  27.         System.out.println(name);  
  28.     }  
  29. }  

采用設定注入隻要有setter方法即可,但是有時由于程式設計習慣也會講getter方法引進,但是要清楚:設值注入與getter方法無關。這裡還有一點需要注意,那就是能使用基本類型,如果非要使用基本類型的話就要使用其對應的包裝類型,如上面使用的是Integer而不是int。Spring的xml配置如下:

Xml代碼  

Spring的bean注入方式
  1. <bean id="personDao" class="cn.glzaction.service.impl.PersonDaoBean">  
  2.     <property name="name" type="java.lang.String" value="glzaction"/>  
  3.     <property name="id" type="java.lang.Integer" value="1"/>  
  4.     <property name="list" type="java.util.List">  
  5.         <list>  
  6.             <value>list1</value>  
  7.             <value>list2</value>  
  8.             <value>list3</value>  
  9.         </list>  
  10.     </property>  
  11.     <property name="map" type="java.util.Map">  
  12.         <map>  
  13.             <entry key="key1" value="value1"></entry>  
  14.             <entry key="key2" value="value2"></entry>  
  15.         </map>  
  16.     </property>  
  17. </bean>  

 設值注入采用的是<property>标簽元素,其中的name屬性對應的是要注入的變量名,type屬性值對應的該變量的類型,可以是自定義類或者包裝類型。value屬性對應的是相應的值,還有一個ref屬性,該屬性值對應的是bean。

3,注解注入

1、古老的注入方式: 

實作類: 

Java代碼  

Spring的bean注入方式
  1. public class UserServiceImpl implements UserService {  
  2.     private UserDAO userDAO;  
  3.     public void setUserDAO(UserDAO userDAO) {  
  4.         this.userDAO = userDAO;  
  5.     }  
  6. ...  
  7. }  

配置檔案: 

Xml代碼  

Spring的bean注入方式
  1. <bean id="userDAO" class="springlive.dao.impl.UserDAOImpl">  
  2.     <property name="sessionFactory">  
  3.         <ref local="sessionFactory" />  
  4.     </property>  
  5. </bean>  
  6. <bean id="userServiceImpl"  
  7.     class="springlive.service.impl.UserServiceImpl">  
  8.     <property name="userDAO">  
  9.         <ref local="userDAO" />  
  10.     </property>  
  11. </bean>  

2、使用注解的方式: 

2.1、Autowired注解 

<1>對成員變量注解 

實作類: 

Java代碼  

Spring的bean注入方式
  1.     @Autowired  
  2.     private IndustryDao industryDao;  
  3. ...  

<2>set方法注解 

Java代碼  

Spring的bean注入方式
  1. @Autowired  
  2. public void setDao(IndustryDao industryDao)  
  3. {  
  4.     super.setDao(industryDao);  
  5. }  

配置檔案: 

Xml代碼  

Spring的bean注入方式
  1. <!-- 使用 <context:annotation-config/> 簡化配置  
  2. Spring 2.1 添加了一個新的 context 的 Schema 命名空間,該命名空間對注釋驅動、屬性檔案引入、加載期織入等功能提供了便捷的配置。我們知道注釋本身是不會做任何事情的,它僅提供中繼資料資訊。要使中繼資料資訊真正起作用,必須讓負責處理這些中繼資料的處理器工作起來。   
  3. 而我們前面所介紹的 AutowiredAnnotationBeanPostProcessor 和 CommonAnnotationBeanPostProcessor 就是處理這些注釋中繼資料的處理器。但是直接在 Spring 配置檔案中定義這些 Bean 顯得比較笨拙。Spring 為我們提供了一種友善的注冊這些 BeanPostProcessor 的方式,這就是 <context:annotation-config/>。  
  4.  -->  
  5. <context:annotation-config />  
  6.     <bean id="industryDao"  
  7.         class="efs.sadapter.system.industry.dao.hibernate.HibernateIndustryDao" />  
  8.     <bean id="industryService"  
  9.         class="efs.sadapter.system.industry.service.impl.IndustryServiceImpl" />  

@Autowired可以對成員變量、方法和構造函數進行标注,來完成自動注入。 

@Autowired的标注位置不同,它們都會在Spring在初始化industryService這個bean時,自動裝配industryDao這個屬性,差別是:第一種實作中,Spring會直接将IndustryDao類型的唯一一個bean指派給industryDao這個成員變量;第二種實作中,Spring會調用setDao方法來将IndustryDao類型的唯一一個bean裝配到industryDao這個屬性。 

2.2、@Qualifier 

@Autowired是根據類型進行自動注入的,如果spring配置檔案中存在多個IndustryDao類型的bean時,或者不存在IndustryDao類型的bean,都會抛出異常。 

存在多個類型的執行個體時,按id注入@Qualifier("core.system.hibernateService") 

Java代碼  

Spring的bean注入方式
  1. HibernateService hibernateService;  
  2. @Autowired  
  3. public void setHibernateService(@Qualifier("core.system.hibernateService")  
  4. HibernateService hibernateService)  
  5. {  
  6.     this.hibernateService = hibernateService;  
  7. }  

若不存在某類型的執行個體:告訴 Spring:在找不到比對 Bean 時也不報錯 

Java代碼  

Spring的bean注入方式
  1. @Autowired(required = false)  
  2.     public void setHibernateService(@Qualifier("core.system.hibernateService")  
  3.     HibernateService hibernateService)  
  4.     {  
  5.         this.hibernateService = hibernateService;  
  6.     }  

2.3、使用 JSR-250 的注解 

<1> @Resource 

spring支援@Resource、@PostConstruct以及@PreDestroyJSR-250的标準注解。 

@Resource可以按type注入,也可以按name注入。@Resource預設按byName自動注入。 

Spring将@Resource注解的name屬性解析為bean的名字,而type屬性則解析為bean的類型。是以如果使用name屬性,則使用byName的自動注入政策,而使用type屬性時則使用byType自動注入政策。如果既不指定name也不指定type屬性,這時将通過反射機制使用byName自動注入政策。 

@Resource裝配順序 

如果同時指定了name和type,則從Spring上下文中找到唯一比對的bean進行裝配,找不到則抛出異常 

如果指定了name,則從上下文中查找名稱(id)比對的bean進行裝配,找不到則抛出異常 

如果指定了type,則從上下文中找到類型比對的唯一bean進行裝配,找不到或者找到多個,都會抛出異常 

如果既沒有指定name,又沒有指定type,則自動按照byName方式進行裝配(見2);如果沒有比對,則回退為一個原始類型(UserDao)進行比對,如果比對則自動裝配; 

<2> @PostConstruct 和 @PreDestroy 

Spring 容器中的 Bean 是有生命周期的,Spring 允許在 Bean 在初始化完成後以及 Bean 銷毀前執行特定的操作,您既可以通過實作 InitializingBean/DisposableBean 接口來定制初始化之後 / 銷毀之前的操作方法,也可以通過 <bean> 元素的 init-method/destroy-method 屬性指定初始化之後 / 銷毀之前調用的操作方法。 

JSR-250 為初始化之後/銷毀之前方法的指定定義了兩個注釋類,分别是 @PostConstruct 和 @PreDestroy,這兩個注釋隻能應用于方法上。标注了 @PostConstruct 注釋的方法将在類執行個體化後調用,而标注了 @PreDestroy 的方法将在類銷毀之前調用。 

使用 @PostConstruct 和 @PreDestroy 注釋的 Boss.java 

Java代碼  

Spring的bean注入方式
  1. package com.baobaotao;  
  2. import javax.annotation.Resource;  
  3. import javax.annotation.PostConstruct;  
  4. import javax.annotation.PreDestroy;  
  5. public class Boss {  
  6.     @Resource  
  7.     private Car car;  
  8.     @Resource(name = "office")  
  9.     private Office office;  
  10.     @PostConstruct  
  11.     public void postConstruct1(){  
  12.         System.out.println("postConstruct1");  
  13.     }  
  14.     @PreDestroy  
  15.     public void preDestroy1(){  
  16.         System.out.println("preDestroy1");   
  17.     }  
  18.     …  
  19. }  

測試類代碼: 

Java代碼  

Spring的bean注入方式
  1. package com.baobaotao;  
  2. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  3. public class AnnoIoCTest {  
  4.     public static void main(String[] args) {  
  5.         String[] locations = {"beans.xml"};  
  6.         ClassPathXmlApplicationContext ctx =   
  7.             new ClassPathXmlApplicationContext(locations);  
  8.         Boss boss = (Boss) ctx.getBean("boss");  
  9.         System.out.println(boss);  
  10.         ctx.destroy();// 關閉 Spring 容器,以觸發 Bean 銷毀方法的執行  
  11.     }  
  12. }  

标注了 @PostConstruct 的 postConstruct1() 方法将在 Spring 容器啟動時,建立 Boss Bean 的時候被觸發執行,而标注了 @PreDestroy 注釋的 preDestroy1() 方法将在 Spring 容器關閉前銷毀 Boss Bean 的時候被觸發執行。 

3、使用 @Component 

雖然我們可以通過 @Autowired 或 @Resource 在 Bean 類中使用自動注入功能,但是 Bean 還是在 XML 檔案中通過 <bean> 進行定義 —— 也就是說,在 XML 配置檔案中定義 Bean,通過 @Autowired 或 @Resource 為 Bean 的成員變量、方法入參或構造函數入參提供自動注入的功能。能否也通過注釋定義 Bean,從 XML 配置檔案中完全移除 Bean 定義的配置呢?答案是肯定的,我們通過 Spring 2.5 提供的 @Component 注釋就可以達到這個目标了。 

下面,我們完全使用注釋定義 Bean 并完成 Bean 之間裝配: 

使用 @Component 注釋的Woman.java 

Java代碼  

Spring的bean注入方式
  1. @Component  
  2. public class Woman{  
  3.     …  
  4. }  

使用 @Component 注釋的Woman.java 

Java代碼  

Spring的bean注入方式
  1. @Component  
  2. public class Man{  
  3.     …  
  4. }  

這樣,我們就可以在 Human類中通過 @Autowired 注入前面定義的 Woman和 Man Bean 了。 

Java代碼  

Spring的bean注入方式
  1. @Component("human")  
  2. public class Human{  
  3.     @Autowired  
  4.     private Woman woman;  
  5.     @Autowired  
  6.     private Man man;  
  7.     …  
  8. }  

一般情況下,Bean 都是 singleton 的,需要注入 Bean 的地方僅需要通過 byType 政策就可以自動注入了,是以大可不必指定 Bean 的名稱。如果需要使用其它作用範圍的 Bean,可以通過 @Scope 注釋來達到目标: 

Java代碼  

Spring的bean注入方式
  1. @Scope("prototype")  
  2. @Component("human")  
  3. public class Human{  
  4.        …  
  5. }  

在使用 @Component 注釋後,Spring 容器必須啟用類掃描機制以啟用注釋驅動 Bean 定義和注釋驅動 Bean 自動注入的政策。Spring 2.5 對 context 命名空間進行了擴充,提供了這一功能,請看下面的配置: 

Xml代碼  

Spring的bean注入方式
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  6.  http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  7.  http://www.springframework.org/schema/context   
  8.  http://www.springframework.org/schema/context/spring-context-2.5.xsd">  
  9.     <context:component-scan base-package="springlive.learn.component "/>  
  10. </beans>  

參考資料: 

使用 Spring 2.5 注釋驅動的 IoC 功能http://www.ibm.com/developerworks/cn/java/j-lo-spring25-ioc/?ca=drs-tp0808