中文JAVA技術(shù)平等自由協(xié)作創(chuàng)造

          Java專(zhuān)題文章博客和開(kāi)源

          常用鏈接

          統(tǒng)計(jì)

          最新評(píng)論

          Spring 3.0 注解注入詳解

           一、各種注解方式

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

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

          Xml代碼

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

          2. @Qualifier注解

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

          1)。 可能存在多個(gè)UserDao實(shí)例

          Java代碼

          @Autowired

          @Qualifier("userServiceImpl")

          public IUserService userService;

          或者

          Java代碼

          @Autowired

          public void setUserDao(@Qualifier("userDao") UserDao userDao) {

          this.userDao = userDao;

          }

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

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

          Java代碼

          @Autowired(required = false)

          public IUserService userService;

          3. @Resource注解

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

          Xml代碼

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

          @Resource裝配順序:

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

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

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

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

          4. @PostConstruct(JSR-250)注解

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

          Java代碼

          public class UserDaoImpl extends HibernateDaoSupport implements UserDao {

          private SessionFactory mySessionFacotry;

          @Resource

          public void setMySessionFacotry(SessionFactory sessionFacotry)

          {

          this.mySessionFacotry = sessionFacotry;

          }

          @PostConstruct

          public void injectSessionFactory()

          {

          super.setSessionFactory(mySessionFacotry);

          }

          }

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

          5. @PreDestroy(JSR-250)注解

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

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

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

          7.@Scope注解

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

          Java代碼

          @Scope("session")

          @Component()

          public class UserSessionBean implements Serializable{

          … …

          }

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

          1.使用簡(jiǎn)化配置

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

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

          Xml代碼

          <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/context

          http://www.springframework.org/schema/context/spring-context-2.5.xsd">

          <context:annotation-config />

          beans>

          將隱式地向Spring容器注冊(cè)了

          AutowiredAnnotationBeanPostProcessor 、

          CommonAnnotationBeanPostProcessor 、

          PersistenceAnnotationBeanPostProcessor

          RequiredAnnotationBeanPostProcessor

          這4個(gè)BeanPostProcessor.

          2.使用讓Bean定義注解工作起來(lái)

          Xml代碼

          <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/context

          http://www.springframework.org/schema/context/spring-context-2.5.xsd">

          <context:component-scan base-package="com.kedacom.ksoa" />

          beans>

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

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

          過(guò)濾器類(lèi)型 | 表達(dá)式范例 | 說(shuō)明

          注解 | org.example.SomeAnnotation | 將所有使用SomeAnnotation注解的類(lèi)過(guò)濾出來(lái)

          類(lèi)名指定 | org.example.SomeClass | 過(guò)濾指定的類(lèi)

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

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

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

          Xml代碼

          <context:component-scan base-package="com.casheen.spring.annotation">

          <context:exclude-filter type="regex" expression="com\.casheen\.spring\.annotation\.web\*" />

          context:component-scan>

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

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

          4. 使用@Scope來(lái)定義Bean的作用范圍

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

          Java代碼

          @Scope("session")

          @Component()

          public class UserSessionBean implements Serializable {



          }

           

          posted on 2014-02-06 19:50 好不容易 閱讀(202) 評(píng)論(0)  編輯  收藏


          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          PK10開(kāi)獎(jiǎng) PK10開(kāi)獎(jiǎng)
          主站蜘蛛池模板: 岚皋县| 聂荣县| 曲松县| 金门县| 洪湖市| 潞城市| 上犹县| 旺苍县| 洛扎县| 定日县| 承德县| 新巴尔虎左旗| 吴川市| 柳州市| 永德县| 大关县| 永州市| 巨野县| 沈阳市| 昂仁县| 乐昌市| 集贤县| 高清| 南岸区| 济源市| 青河县| 边坝县| 乳山市| 陇西县| 长兴县| 封开县| 互助| 文山县| 西贡区| 芮城县| 卫辉市| 巴东县| 新晃| 平安县| 河北省| 松桃|