Sping注解入門

          Posted on 2010-06-25 17:05 FaithChen 閱讀(1886) 評論(0)  編輯  收藏

            1.讓@Autowired工作起來的準(zhǔn)備工作

          要使@Autowired能夠工作,首先需要在配置文件中加入以下代碼

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

           說明@Autowired可以對成員變量、方法和構(gòu)造函數(shù)進(jìn)行標(biāo)注,來完成自動裝配的工作。@Autowired的標(biāo)注位置可以不同,這將在下文中說明,它們都會在Spring在初始化bean時,自動裝配@Autowired指定的屬性,注意,@Autowired是通過類型裝配的,而非id,當(dāng)然,可以@Qualifier配合@Autowired來解決使用id進(jìn)行裝配,并解決當(dāng)Spring上下文中受裝配bean不存在的問題,這些將在下文中說明

          2. 使用Spring注解來注入屬性
          2.1. 使用注解以前我們是怎樣注入屬性的
          類的實現(xiàn):

          public class UserManagerImpl implements UserManager {
              
          private
           UserDao userDao;
              
          public void setUserDao(UserDao userDao) 
          {
                  
          this.userDao =
           userDao;
              }

              
          }

          配置文件:

          <bean id="userManagerImpl" class="com.kedacom.spring.annotation.service.UserManagerImpl">
              
          <property name="userDao" ref="userDao" />
          </bean>
          <bean id="userDao" class="com.kedacom.spring.annotation.persistence.UserDaoImpl">
              
          <property name="sessionFactory" ref="mySessionFactory" />
          </bean>


          2.2. 引入@Autowired注解(不推薦使用,建議使用@Resource)
          類的實現(xiàn)(對成員變量進(jìn)行標(biāo)注)


          或者(對方法進(jìn)行標(biāo)注)


          配置文件

          以上兩種不同實現(xiàn)方式中,@Autowired的標(biāo)注位置不同,它們都會在Spring在初始化userManagerImpl這個bean時,自動裝配userDao這個屬性,區(qū)別是:第一種實現(xiàn)中,Spring會直接將UserDao類型的唯一一個bean賦值給userDao這個成員變量;第二種實現(xiàn)中,Spring會調(diào)用setUserDao方法來將UserDao類型的唯一一個bean裝配到userDao這個屬性。

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

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

              
          }


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

          7. @PreDestroy(JSR-250)
          在方法上加上注解@PreDestroy,這個方法就會在Bean初始化之后被Spring容器執(zhí)行。由于我們當(dāng)前還沒有需要用到它的場景,這里不不去演示。其用法同@PostConstruct。

          8. 使用<context:annotation-config />簡化配置
          Spring2.1添加了一個新的context的Schema命名空間,該命名空間對注釋驅(qū)動、屬性文件引入、加載期織入等功能提供了便捷的配置。我們知道注釋本身是不會做任何事情的,它僅提供元數(shù)據(jù)信息。要使元數(shù)據(jù)信息真正起作用,必須讓負(fù)責(zé)處理這些元數(shù)據(jù)的處理器工作起來。
          AutowiredAnnotationBeanPostProcessor和CommonAnnotationBeanPostProcessor就是處理這些注釋元數(shù)據(jù)的處理器。但是直接在Spring配置文件中定義這些Bean顯得比較笨拙。Spring為我們提供了一種方便的注冊這些BeanPostProcessor的方式,這就是<context:annotation-config />:

          <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>
          PersistenceAnnotationBeanPostProcessor以及RequiredAnnotationBeanPostProcessor這4個BeanPostProcessor。

          9. 使用Spring注解完成Bean的定義

          以上我們介紹了通過@Autowired或@Resource來實現(xiàn)在Bean中自動注入的功能,下面我們將介紹如何注解Bean,從而從XML配置文件中完全移除Bean定義的配置。

          只需要在對應(yīng)的類上加上一個@Component注解,就將該類定義為一個Bean了:
          @Component
          public class UserDaoImpl extends HibernateDaoSupport implements UserDao 
          {
              
          }

          @Component("userDao")
          @Component是所有受Spring管理組件的通用形式,Spring還提供了更加細(xì)化的注解形式:@Repository、@Service、@Controller,它們分別對應(yīng)存儲層Bean,業(yè)務(wù)層Bean,和展示層Bean。

          <context:component-scan />還允許定義過濾器將基包下的某些類納入或排除。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)用實例:

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

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

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

           

          @Scope("session")   
          @Component()   
          public class UserSessionBean implements Serializable {   
                 
          }
            

           



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


          網(wǎng)站導(dǎo)航:
           

          posts - 4, comments - 0, trackbacks - 0, articles - 4

          Copyright © FaithChen

          主站蜘蛛池模板: 望城县| 轮台县| 丰城市| 桦川县| 安吉县| 正镶白旗| 晋城| 汝阳县| 延川县| 旌德县| 全州县| 新疆| 余干县| 剑阁县| 文山县| 乳山市| 乌恰县| 闵行区| 北安市| 孝昌县| 安乡县| 桃源县| 台山市| 新乡市| 明溪县| 吕梁市| 托里县| 衡东县| 乌拉特中旗| 垣曲县| 稻城县| 永安市| 昂仁县| 抚松县| 兴安县| 玛多县| 西盟| 和龙市| 湘潭市| 高要市| 石阡县|