日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

Spring Annotation 筆記——IOC篇

 臨風笛 2011-11-23
@Autowired 
1、Spring 通過一個 BeanPostProcessor 對 @Autowired 進行解析,所以要讓 @Autowired 起作用必須事先在 Spring 容器中聲明 AutowiredAnnotationBeanPostProcessor Bean。 
Java代碼  收藏代碼
  1. <!-- 該 BeanPostProcessor 將自動起作用,對標注 @Autowired 的 Bean 進行自動注入 -->  
  2. <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>       

或者使用隱式注冊(隱式注冊 post-processors 包括了 AutowiredAnnotationBeanPostProcessor,CommonAnnotationBeanPostProcessor,PersistenceAnnotationBeanPostProcessor,RequiredAnnotationBeanPostProcessor。) 
Java代碼  收藏代碼
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www./schema/beans"             
  3. xmlns:xsi="http://www./2001/XMLSchema-instance"             
  4. xmlns:context="http://www./schema/context"             
  5. xsi:schemaLocation="http://www./schema/beans  
  6. http://www./schema/beans/spring-beans-2.5.xsd  
  7. http://www./schema/context                 
  8. http://www./schema/context/spring-context-2.5.xsd">  
  9.   
  10. <context:annotation-config/>   
  11. </beans>  

2、@Autowired默認按照類型匹配的方式進行注入 
3、@Autowired注解可以用于成員變量、setter方法、構造器函數(shù)等 
4、使用@Autowired注解須有且僅有一個與之匹配的Bean,當找不到匹配的 Bean 或者存在多個匹配的Bean時,Spring 容器將拋出 異常 
5、Spring 允許我們通過 @Qualifier 注釋指定注入 Bean 的名稱。@Autowired 和 @Qualifier 結合使用時,自動注入的策略就從 byType 轉變成 byName 了。 
Java代碼  收藏代碼
  1. public class MovieRecommender {  
  2.   
  3. @Autowired  
  4. @Qualifier("mainCatalog")  
  5. private MovieCatalog movieCatalog;  
  6.       
  7.     private CustomerPreferenceDao customerPreferenceDao;  
  8.   
  9.     @Autowired  
  10.     public MovieRecommender(CustomerPreferenceDao customerPreferenceDao) {  
  11.         this.customerPreferenceDao = customerPreferenceDao;  
  12.     }  
  13.   
  14.     // ...  
  15. }  



@Resource 
1、@Resource 的作用相當于 @Autowired,只不過 @Autowired 按 byType 自動注入,@Resource 默認按 byName 自動注入罷了。 
2、要讓 JSR-250 的注釋生效,除了在 Bean 類中標注這些注釋外,還需要在 Spring 容器中注冊一個負責處理這些注釋的 BeanPostProcessor 
Java代碼  收藏代碼
  1. <bean  class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/>   

3、@Resource 有兩個屬性是比較重要的,分別是 name 和 type,Spring 將 @Resource 注釋的 name 屬性解析為 Bean 的名字,而 type 屬性則解析為 Bean 的類型。所以如果使用 name 屬性,則使用 byName 的自動注入策略,而使用 type 屬性時則使用 byType 自動注入策略。如果既不指定 name 也不指定 type 屬性,這時將通過反射機制使用 byName 自動注入策略。 
Java代碼  收藏代碼
  1. public class SimpleMovieLister {  
  2.   
  3.     private MovieFinder movieFinder;  
  4.   
  5.     @Resource  
  6.     public void setMovieFinder(MovieFinder movieFinder) {  
  7.         this.movieFinder = movieFinder;  
  8.     }  
  9. }  



@PostConstruct 和 @PreDestroy 
標注了 @PostConstruct 注釋的方法將在類實例化后調用,而標注了 @PreDestroy 的方法將在類銷毀之前調用。 
Java代碼  收藏代碼
  1. public class CachingMovieLister {  
  2.   
  3.     @PostConstruct  
  4.     public void populateMovieCache() {  
  5.         // populates the movie cache upon initialization...  
  6.     }  
  7.       
  8.     @PreDestroy  
  9.     public void clearMovieCache() {  
  10.         // clears the movie cache upon destruction...  
  11.     }  
  12. }  



@Component 
1、使用@Component注解可以直接定義Bean,而無需在xml定義。但是若兩種定義同時存在,xml中的定義會覆蓋類中注解的Bean定義。 
2、@Component 有一個可選的入?yún)?,用于指?Bean 的名稱。 
Java代碼  收藏代碼
  1. @Component  
  2. public class ActionMovieCatalog implements MovieCatalog {  
  3.     // ...  
  4. }  

3、<context:component-scan/> 允許定義過濾器將基包下的某些類納入或排除。Spring 支持以下 4 種類型的過濾方式: 
過濾器類型表達式范例
annotationorg.example.SomeAnnotation
assignableorg.example.SomeClass
regexorg\.example\.Default.*
aspectjorg.example..*Service+

下面這個XML配置會忽略所有的@Repository注解并用“stub”儲存庫代替。 
Java代碼  收藏代碼
  1. <beans ...>  
  2.   
  3.      <context:component-scan base-package="org.example">  
  4.         <context:include-filter type="regex" expression=".*Stub.*Repository"/>  
  5.         <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>  
  6.      </context:component-scan>  
  7.   
  8. </beans>  

4、默認情況下通過 @Component 定義的 Bean 都是 singleton 的,如果需要使用其它作用范圍的 Bean,可以通過 @Scope 注釋來達到目標 
Java代碼  收藏代碼
  1. @Scope("prototype")  
  2. @Repository  
  3. public class MovieFinderImpl implements MovieFinder {  
  4.     // ...  
  5. }  

5、Spring 2.5引入了更多典型化注解(stereotype annotations): @Component、@Service和 @Controller。 @Component是所有受Spring管理組件的通用形式; 而@Repository、@Service和 @Controller則是@Component的細化, 用來表示更具體的用例(例如,分別對應了持久化層、服務層和表現(xiàn)層) 
Java代碼  收藏代碼
  1. @Service  
  2. public class SimpleMovieLister {  
  3.   
  4.     private MovieFinder movieFinder;  
  5.   
  6.     @Autowired  
  7.     public SimpleMovieLister(MovieFinder movieFinder) {  
  8.         this.movieFinder = movieFinder;  
  9.     }  
  10. }  
  11.   
  12. @Repository  
  13. public class JpaMovieFinder implements MovieFinder {  
  14.     // implementation elided for clarity  
  15. }  

6、要檢測這些類并注冊相應的bean,需要在XML中包含以下元素,其中'basePackage'是兩個類的公共父包 (或者可以用逗號分隔的列表來分別指定包含各個類的包)。 
Java代碼  收藏代碼
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www./schema/beans"  
  3.        xmlns:xsi="http://www./2001/XMLSchema-instance"  
  4.        xmlns:context="http://www./schema/context"  
  5.        xsi:schemaLocation="http://www./schema/beans   
  6.            http://www./schema/beans/spring-beans-2.5.xsd  
  7.            http://www./schema/context  
  8.            http://www./schema/context/spring-context-2.5.xsd">  
  9.                  
  10.      <context:component-scan base-package="org.example"/>  
  11.        
  12. </beans>  

此外,在使用組件掃描元素時,AutowiredAnnotationBeanPostProcessor 和CommonAnnotationBeanPostProcessor會隱式地被包括進來。 也就是說,連個組件都會被自動檢測并織入 - 所有這一切都不需要在XML中提供任何bean配置元數(shù)據(jù)。 

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

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多