• <tfoot id="ukgsw"><input id="ukgsw"></input></tfoot>
    
    • 久久精品精选,精品九九视频,www久久只有这里有精品,亚洲熟女乱色综合一区
      分享

      主題:Spring注解入門

       狼志凌云 2011-10-08

      1. 使用Spring注解來注入屬性
      1.1. 使用注解以前我們是怎樣注入屬性的
      類的實現:

      Java代碼 復制代碼 收藏代碼
      1. public class UserManagerImpl implements UserManager {   
      2.     private UserDao userDao;   
      3.     public void setUserDao(UserDao userDao) {   
      4.         this.userDao = userDao;   
      5.     }   
      6.     ...   
      7. }  


      配置文件:

      Java代碼 復制代碼 收藏代碼
      1. <bean id="userManagerImpl" class="com.kedacom.spring.annotation.service.UserManagerImpl">   
      2.     <property name="userDao" ref="userDao" />   
      3. </bean>   
      4. <bean id="userDao" class="com.kedacom.spring.annotation.persistence.UserDaoImpl">   
      5.     <property name="sessionFactory" ref="mySessionFactory" />   
      6. </bean>  



      1.2. 引入@Autowired注解(不推薦使用,建議使用@Resource)
      類的實現(對成員變量進行標注)

      Java代碼 復制代碼 收藏代碼
      1. public class UserManagerImpl implements UserManager {   
      2.     @Autowired  
      3.     private UserDao userDao;   
      4.     ...   
      5. }  


      或者(對方法進行標注)

      Java代碼 復制代碼 收藏代碼
      1. public class UserManagerImpl implements UserManager {   
      2.     private UserDao userDao;   
      3.     @Autowired  
      4.     public void setUserDao(UserDao userDao) {   
      5.         this.userDao = userDao;   
      6.     }   
      7.     ...   
      8. }  


      配置文件

      Java代碼 復制代碼 收藏代碼
      1. <bean id="userManagerImpl" class="com.kedacom.spring.annotation.service.UserManagerImpl" />   
      2. <bean id="userDao" class="com.kedacom.spring.annotation.persistence.UserDaoImpl">   
      3.     <property name="sessionFactory" ref="mySessionFactory" />   
      4. </bean>  


      @Autowired可以對成員變量、方法和構造函數進行標注,來完成自動裝配的工作。以上兩種不同實現方式中,@Autowired的標注位置不同,它們都會在Spring在初始化userManagerImpl這個bean時,自動裝配userDao這個屬性,區別是:第一種實現中,Spring會直接將UserDao類型的唯一一個bean賦值給userDao這個成員變量;第二種實現中,Spring會調用setUserDao方法來將UserDao類型的唯一一個bean裝配到userDao這個屬性。

      1.3. 讓@Autowired工作起來
      要使@Autowired能夠工作,還需要在配置文件中加入以下代碼

      Java代碼 復制代碼 收藏代碼
      1. <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />  



      1.4. @Qualifier
      @Autowired是根據類型進行自動裝配的。在上面的例子中,如果當Spring上下文中存在不止一個UserDao類型的bean時,就會拋出BeanCreationException異常;如果Spring上下文中不存在UserDao類型的bean,也會拋出BeanCreationException異常。我們可以使用@Qualifier配合@Autowired來解決這些問題。
      1. 可能存在多個UserDao實例

      Java代碼 復制代碼 收藏代碼
      1. @Autowired  
      2. public void setUserDao(@Qualifier("userDao") UserDao userDao) {   
      3.     this.userDao = userDao;   
      4. }  


      這樣,Spring會找到id為userDao的bean進行裝配。
      2. 可能不存在UserDao實例

      Java代碼 復制代碼 收藏代碼
      1. @Autowired(required = false)   
      2. public void setUserDao(UserDao userDao) {   
      3.     this.userDao = userDao;   
      4. }  



      1.5. @Resource(JSR-250標準注解,推薦使用它來代替Spring專有的@Autowired注解)
      Spring 不但支持自己定義的@Autowired注解,還支持幾個由JSR-250規范定義的注解,它們分別是@Resource、@PostConstruct以及@PreDestroy。
      @Resource的作用相當于@Autowired,只不過@Autowired按byType自動注入,而@Resource默認按byName自動注入罷了。@Resource有兩個屬性是比較重要的,分別是name和type,Spring將@Resource注解的name屬性解析為bean的名字,而type屬性則解析為bean的類型。所以如果使用name屬性,則使用byName的自動注入策略,而使用type屬性時則使用byType自動注入策略。如果既不指定name也不指定type屬性,這時將通過反射機制使用byName自動注入策略。
      @Resource裝配順序

      1. 如果同時指定了name和type,則從Spring上下文中找到唯一匹配的bean進行裝配,找不到則拋出異常
      2. 如果指定了name,則從上下文中查找名稱(id)匹配的bean進行裝配,找不到則拋出異常
      3. 如果指定了type,則從上下文中找到類型匹配的唯一bean進行裝配,找不到或者找到多個,都會拋出異常
      4. 如果既沒有指定name,又沒有指定type,則自動按照byName方式進行裝配(見2);如果沒有匹配,則回退為一個原始類型(UserDao)進行匹配,如果匹配則自動裝配;



      1.6. @PostConstruct(JSR-250)
      在方法上加上注解@PostConstruct,這個方法就會在Bean初始化之后被Spring容器執行(注:Bean初始化包括,實例化Bean,并裝配Bean的屬性(依賴注入))。
      它的一個典型的應用場景是,當你需要往Bean里注入一個其父類中定義的屬性,而你又無法復寫父類的屬性或屬性的setter方法時,如:

      Java代碼 復制代碼 收藏代碼
      1. public class UserDaoImpl extends HibernateDaoSupport implements UserDao {   
      2.     private SessionFactory mySessionFacotry;   
      3.     @Resource  
      4.     public void setMySessionFacotry(SessionFactory sessionFacotry) {   
      5.         this.mySessionFacotry = sessionFacotry;   
      6.     }   
      7.     @PostConstruct  
      8.     public void injectSessionFactory() {   
      9.         super.setSessionFactory(mySessionFacotry);   
      10.     }   
      11.     ...   
      12. }  


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

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

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

      Java代碼 復制代碼 收藏代碼
      1. <beans xmlns="http://www./schema/beans" xmlns:xsi="http://www./2001/XMLSchema-instance" xmlns:context="http://www./schema/context"  
      2.     xsi:schemaLocation="http://www./schema/beans   
      3.     http://www./schema/beans/spring-beans-2.5.xsd   
      4.     http://www./schema/context   
      5.     http://www./schema/context/spring-context-2.5.xsd">   
      6.     <context:annotation-config />   
      7. </beans>  


      <context:annotationconfig />將隱式地向Spring容器注冊AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、 PersistenceAnnotationBeanPostProcessor以及RequiredAnnotationBeanPostProcessor這4個BeanPostProcessor。

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

      2.1. @Component(不推薦使用)、@Repository、@Service、@Controller
      只需要在對應的類上加上一個@Component注解,就將該類定義為一個Bean了:

      Java代碼 復制代碼 收藏代碼
      1. @Component  
      2. public class UserDaoImpl extends HibernateDaoSupport implements UserDao {   
      3.     ...   
      4. }  


      使用@Component注解定義的Bean,默認的名稱(id)是小寫開頭的非限定類名。如這里定義的Bean名稱就是userDaoImpl。你也可以指定Bean的名稱:
      @Component("userDao")
      @Component是所有受Spring管理組件的通用形式,Spring還提供了更加細化的注解形式:@Repository、@Service、@Controller,它們分別對應存儲層Bean,業務層Bean,和控制層Bean。目前版本(2.5)中,這些注解與@Component的語義是一樣的,完全通用,在Spring以后的版本中可能會給它們追加更多的語義。所以,我們推薦使用@Repository、@Service、@Controller來替代@Component。

      2.2. 使用<context:component-scan />讓Bean定義注解工作起來

      Java代碼 復制代碼 收藏代碼
      1. <beans xmlns="http://www./schema/beans" xmlns:xsi="http://www./2001/XMLSchema-instance" xmlns:context="http://www./schema/context"  
      2.     xsi:schemaLocation="http://www./schema/beans   
      3.     http://www./schema/beans/spring-beans-2.5.xsd   
      4.     http://www./schema/context   
      5.     http://www./schema/context/spring-context-2.5.xsd">   
      6.     <context:component-scan base-package="com.kedacom.ksoa" />   
      7. </beans>  


      這里,所有通過<bean>元素定義Bean的配置內容已經被移除,僅需要添加一行<context:component-scan />配置就解決所有問題了——Spring XML配置文件得到了極致的簡化(當然配置元數據還是需要的,只不過以注釋形式存在罷了)。<context:component-scan />的base-package屬性指定了需要掃描的類包,類包及其遞歸子包中所有的類都會被處理。
      <context:component-scan />還允許定義過濾器將基包下的某些類納入或排除。Spring支持以下4種類型的過濾方式:

      • 過濾器類型 表達式范例 說明
      • 注解 org.example.SomeAnnotation 將所有使用SomeAnnotation注解的類過濾出來
      • 類名指定 org.example.SomeClass 過濾指定的類
      • 正則表達式 com\.kedacom\.spring\.annotation\.web\..* 通過正則表達式過濾一些類
      • AspectJ表達式 org.example..*Service+ 通過AspectJ表達式過濾一些類


      以正則表達式為例,我列舉一個應用實例:

      Java代碼 復制代碼 收藏代碼
      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>  


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

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

      Java代碼 復制代碼 收藏代碼
      1. @Scope("session")   
      2. @Component()   
      3. public class UserSessionBean implements Serializable {   
      4.     ...   
      5. }  

        本站是提供個人知識管理的網絡存儲空間,所有內容均由用戶發布,不代表本站觀點。請注意甄別內容中的聯系方式、誘導購買等信息,謹防詐騙。如發現有害或侵權內容,請點擊一鍵舉報。
        轉藏 分享 獻花(0

        0條評論

        發表

        請遵守用戶 評論公約

        類似文章 更多

        主站蜘蛛池模板: 国产99视频精品免费视频36| 国产日韩入口一区二区| 狠狠色噜噜狠狠狠狠777米奇| 白嫩少妇无套内谢视频| 亚洲卡1卡2卡新区网站| 国产精品V欧美精品V日韩精品| 在线 欧美 中文 亚洲 精品| 日本无遮挡吸乳视频| 国产成人亚洲日韩欧美| GV无码免费无禁网站男男| 国内精品久久久久久久小说 | 亚洲欧美日韩在线码| 成码无人AV片在线电影网站| 亚洲精品一区二区麻豆| 亚洲处破女AV日韩精品| 成人3D动漫一区二区三区| 亚洲精品国产电影| 无遮挡拍拍拍免费观看| 国产成熟女人性满足视频| 亚洲日本高清一区二区三区| 亚洲国产精品成人AV在线| 又粗又紧又湿又爽的视频| 中文字幕国产精品av| 国产波霸爆乳一区二区| 日韩中文字幕人妻精品| 欧美人妻一区二区三区| 亚洲旡码欧美大片| 樱花草视频www日本韩国| 久久精品国产免费观看三人同眠| 18禁无遮挡啪啪无码网站破解版| 欧美精品亚洲精品日韩专区 | 亚洲成AV人无码综合在线| 国产成人啪精品午夜网站| 2020无码专区人妻系列日韩| 国产免费踩踏调教视频| 无码免费大香伊蕉在人线国产| 中国女人高潮hd| 亚洲AV永久无码精品天堂动漫| 在线理论三级午夜电影| 亚洲AV成人片不卡无码| 一本精品99久久精品77|