自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Spring 3.0 注解注入詳解

開發(fā) 后端

  一、各種注解方式

  1.@Autowired注解(不推薦使用,建議使用@Resource)

  @Autowired可以對成員變量、方法和構(gòu)造函數(shù)進(jìn)行標(biāo)注,來完成自動裝配的工作。@Autowired的標(biāo)注位置不同,它們都會在Spring在初始化這個bean時,自動裝配這個屬性。要使@Autowired能夠工作,還需要在配置文件中加入以下

  Xml代碼

  1. <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />   

 

  2. @Qualifier注解

  @Autowired是根據(jù)類型進(jìn)行自動裝配的。例如,如果當(dāng)Spring上下文中存在不止一個UserDao類型的bean時,就會拋出BeanCreationException異常;如果Spring上下文中不存在UserDao類型的bean,也會拋出BeanCreationException異常。我們可以使用@Qualifier配合@Autowired來解決這些問題。如下:

  1). 可能存在多個UserDao實(shí)例

  Java代碼

  1. @Autowired       
  2. @Qualifier("userServiceImpl")         
  3. public IUserService userService;  

 

  或者

  Java代碼

  1. @Autowired       
  2. public void setUserDao(@Qualifier("userDao") UserDao userDao) {        
  3.     this.userDao = userDao;        

 

  這樣,Spring會找到id為userServiceImpl和userDao的bean進(jìn)行裝配。

  2). 可能不存在UserDao實(shí)例

  Java代碼

  1. @Autowired(required = false)        
  2. public IUserService userService;    

 

  3. @Resource注解

  JSR-250標(biāo)準(zhǔn)注解,推薦使用它來代替Spring專有的@Autowired注解。@Resource的作用相當(dāng)于@Autowired,只不過@Autowired按byType自動注入,而@Resource默認(rèn)按byName自動注入罷了。@Resource有兩個屬性是比較重要的,分別是name和type,Spring將 @Resource注解的name屬性解析為bean的名字,而type屬性則解析為bean的類型。所以如果使用name屬性,則使用byName的自動注入策略,而使用type屬性時則使用byType自動注入策略。如果既不指定name也不指定type屬性,這時將通過反射機(jī)制使用byName自動注入策略。要使@Autowired能夠工作,還需要在配置文件中加入以下:

  Xml代碼

  1. <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" /> 

 

  @Resource裝配順序:

  a.如果同時指定了name和type,則從Spring上下文中找到唯一匹配的bean進(jìn)行裝配,找不到則拋出異常

  b.如果指定了name,則從上下文中查找名稱(id)匹配的bean進(jìn)行裝配,找不到則拋出異常

  c.如果指定了type,則從上下文中找到類型匹配的唯一bean進(jìn)行裝配,找不到或者找到多個,都會拋出異常

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

  4. @PostConstruct(JSR-250)注解

  在方法上加上注解@PostConstruct,這個方法就會在Bean初始化之后被Spring容器執(zhí)行(注:Bean初始化包括,實(shí)例化Bean,并裝配Bean的屬性(依賴注入))。它的一個典型的應(yīng)用場景是,當(dāng)你需要往Bean里注入一個其父類中定義的屬性,而你又無法復(fù)寫父類的屬性或?qū)傩缘膕etter方法時,如:

  Java代碼

  1. public class UserDaoImpl extends HibernateDaoSupport implements UserDao {        
  2.     
  3.     private SessionFactory mySessionFacotry;        
  4.     
  5.     @Resource    
  6.     public void setMySessionFacotry(SessionFactory sessionFacotry)     
  7.     {        
  8.         this.mySessionFacotry = sessionFacotry;        
  9.     }        
  10.     
  11.     @PostConstruct       
  12.     public void injectSessionFactory()     
  13.     {        
  14.         super.setSessionFactory(mySessionFacotry);        
  15.     }     
  16. }  

 

  這里通過@PostConstruct,為UserDaoImpl的父類里定義的一個sessionFactory私有屬性,注入了我們自己定義的 sessionFactory(父類的setSessionFactory方法為final,不可復(fù)寫),之后我們就可以通過調(diào)用 super.getSessionFactory()來訪問該屬性了。

  5. @PreDestroy(JSR-250)注解

  在方法上加上注解@PreDestroy,這個方法就會在Bean初始化之后被Spring容器執(zhí)行。其用法同@PostConstruct。和@PostConstruct 區(qū)別在于:@PostConstruct注釋的方法將在類實(shí)例化后調(diào)用,而標(biāo)注了 @PreDestroy 的方法將在類銷毀之前調(diào)用。

  6. @Component注解 (不推薦使用)

  只需要在對應(yīng)的類上加上一個@Component注解,就將該類定義為一個Bean了。Spring還提供了更加細(xì)化的注解形式:@Repository、@Service、@Controller,它們分別對應(yīng)存儲層Bean,業(yè)務(wù)層Bean,和展示層Bean。目前版本(2.5)中,這些注解與@Component的語義是一樣的,完全通用,在Spring以后的版本中可能會給它們追加更多的語義。所以,我們推薦使用@Repository、@Service、@Controller來替代@Component。

  7.@Scope注解

  在使用XML定義Bean時,我們可能還需要通過bean的scope屬性來定義一個Bean的作用范圍,我們同樣可以通過@Scope注解來完成這項(xiàng)工作:

  Java代碼

  1. @Scope("session")        
  2. @Component()        
  3. public class UserSessionBean implements Serializable{      
  4.  ... ...     
  5. }  

 

  二、配置啟用注解(注意以下配置需要使用spring2.5的頭文件,在spring3.0中不適用)

  1.使用簡化配置

  Spring2.1添加了一個新的context的Schema命名空間,該命名空間對注釋驅(qū)動、屬性文件引入、加載期織入等功能提供了便捷的配置。我們知道注釋本身是不會做任何事情的,它僅提供元數(shù)據(jù)信息。要使元數(shù)據(jù)信息真正起作用,必須讓負(fù)責(zé)處理這些元數(shù)據(jù)的處理器工作起來。

  AutowiredAnnotationBeanPostProcessor和CommonAnnotationBeanPostProcessor就是處理這些注釋元數(shù)據(jù)的處理器。但是直接在Spring配置文件中定義這些Bean顯得比較笨拙。Spring為我們提供了一種方便的注冊這些BeanPostProcessor的方式,這就是,以下是spring的配置。

  Xml代碼

  1. <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"       
  2.     xsi:schemaLocation="http://www.springframework.org/schema/beans        
  3.     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd        
  4.     http://www.springframework.org/schema/context        
  5.     http://www.springframework.org/schema/context/spring-context-2.5.xsd">        
  6.     <context:annotation-config />        
  7. beans>  

 

  將隱式地向Spring容器注冊了

  AutowiredAnnotationBeanPostProcessor 、

  CommonAnnotationBeanPostProcessor 、

  PersistenceAnnotationBeanPostProcessor

  RequiredAnnotationBeanPostProcessor

  這4個BeanPostProcessor。

  2.使用讓Bean定義注解工作起來

  Xml代碼

  1. <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"       
  2.     xsi:schemaLocation="http://www.springframework.org/schema/beans        
  3.     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd        
  4.     http://www.springframework.org/schema/context        
  5.     http://www.springframework.org/schema/context/spring-context-2.5.xsd">        
  6.     <context:component-scan base-package="com.kedacom.ksoa" />        
  7. beans>   

 

  這里,所有通過元素定義Bean的配置內(nèi)容已經(jīng)被移除,僅需要添加一行配置就解決所有問題了——Spring XML配置文件得到了***的簡化(當(dāng)然配置元數(shù)據(jù)還是需要的,只不過以注釋形式存在罷了)。的base-package屬性指定了需要掃描的類包,類包及其遞歸子包中所有的類都會被處理。

  還允許定義過濾器將基包下的某些類納入或排除。Spring支持以下4種類型的過濾方式:

  過濾器類型 | 表達(dá)式范例 | 說明

  注解 | org.example.SomeAnnotation | 將所有使用SomeAnnotation注解的類過濾出來

  類名指定 | org.example.SomeClass | 過濾指定的類

  正則表達(dá)式 | com\.kedacom\.spring\.annotation\.web\..* | 通過正則表達(dá)式過濾一些類

  AspectJ表達(dá)式 | org.example..*Service+ | 通過AspectJ表達(dá)式過濾一些類

  以正則表達(dá)式為例,我列舉一個應(yīng)用實(shí)例:

  Xml代碼

  1. <context:component-scan base-package="com.casheen.spring.annotation">        
  2.     <context:exclude-filter type="regex" expression="com\.casheen\.spring\.annotation\.web\..*" />        
  3. context:component-scan>    

 值得注意的是配置項(xiàng)不但啟用了對類包進(jìn)行掃描以實(shí)施注釋驅(qū)動Bean定義的功能,同時還啟用了注釋驅(qū)動自動注入的功能(即還隱式地在內(nèi)部注冊了AutowiredAnnotationBeanPostProcessor和CommonAnnotationBeanPostProcessor),因此當(dāng)使用后,就可以將移除了。

  3.
   是不支持spring的@Transcation和EJB的Spring's @Transactional or EJB3's @TransactionAttribute annotation。用此配置可以達(dá)到目的。

  4. 使用@Scope來定義Bean的作用范圍

  在使用XML定義Bean時,我們可能還需要通過bean的scope屬性來定義一個Bean的作用范圍,我們同樣可以通過@Scope注解來完成這項(xiàng)工作:

Java代碼

 

  1. @Scope("session")           
  2. @Component()            
  3. public class UserSessionBean implements Serializable {            
  4.     ...           
  5. }    

 

【編輯推薦】

  1. Java EE進(jìn)階之Spring事務(wù)深入淺出
  2. Spring事務(wù)管理高級應(yīng)用難點(diǎn)剖析
  3. Spring的Hibernate事務(wù)管理機(jī)制
  4. 簡單介紹Spring事務(wù)管理

 

責(zé)任編輯:金賀 來源: ITEYE博客
相關(guān)推薦

2009-06-15 17:48:32

Spring注解注入屬性

2016-03-21 17:08:54

Java Spring注解區(qū)別

2020-11-02 07:00:29

Spring Boo注解自動化

2024-10-14 17:18:27

2023-07-11 09:14:12

Beanquarkus

2017-08-10 10:23:59

2011-03-01 13:45:41

Spring3Annotation

2021-02-28 20:41:18

Vue注入Angular

2011-05-31 10:00:21

Android Spring 依賴注入

2009-06-17 15:50:32

2014-04-08 16:02:28

寬字節(jié)注入數(shù)據(jù)安全MYSQL

2009-07-10 11:07:00

WebWork注入Servlet方法

2009-07-27 09:46:28

Silverlight

2020-09-25 07:49:36

策略模式Spring

2009-06-17 16:21:43

Spring3.0新功

2022-03-24 07:38:07

注解SpringBoot項(xiàng)目

2010-11-26 15:38:32

MySQL注入

2023-06-02 16:24:46

SpringBootSSM

2009-09-17 13:32:48

WSUS服務(wù)器

2011-02-13 11:37:45

Android 3.0
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號