小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

Spring Annotation 筆記——IOC篇

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

或者使用隱式注冊(cè)(隱式注冊(cè) 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默認(rèn)按照類型匹配的方式進(jìn)行注入 
3、@Autowired注解可以用于成員變量、setter方法、構(gòu)造器函數(shù)等 
4、使用@Autowired注解須有且僅有一個(gè)與之匹配的Bean,當(dāng)找不到匹配的 Bean 或者存在多個(gè)匹配的Bean時(shí),Spring 容器將拋出 異常 
5、Spring 允許我們通過 @Qualifier 注釋指定注入 Bean 的名稱。@Autowired 和 @Qualifier 結(jié)合使用時(shí),自動(dòng)注入的策略就從 byType 轉(zhuǎn)變成 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 的作用相當(dāng)于 @Autowired,只不過 @Autowired 按 byType 自動(dòng)注入,@Resource 默認(rèn)按 byName 自動(dòng)注入罷了。 
2、要讓 JSR-250 的注釋生效,除了在 Bean 類中標(biāo)注這些注釋外,還需要在 Spring 容器中注冊(cè)一個(gè)負(fù)責(zé)處理這些注釋的 BeanPostProcessor 
Java代碼  收藏代碼
  1. <bean  class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/>   

3、@Resource 有兩個(gè)屬性是比較重要的,分別是 name 和 type,Spring 將 @Resource 注釋的 name 屬性解析為 Bean 的名字,而 type 屬性則解析為 Bean 的類型。所以如果使用 name 屬性,則使用 byName 的自動(dòng)注入策略,而使用 type 屬性時(shí)則使用 byType 自動(dòng)注入策略。如果既不指定 name 也不指定 type 屬性,這時(shí)將通過反射機(jī)制使用 byName 自動(dòng)注入策略。 
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 
標(biāo)注了 @PostConstruct 注釋的方法將在類實(shí)例化后調(diào)用,而標(biāo)注了 @PreDestroy 的方法將在類銷毀之前調(diào)用。 
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定義。但是若兩種定義同時(shí)存在,xml中的定義會(huì)覆蓋類中注解的Bean定義。 
2、@Component 有一個(gè)可選的入?yún)ⅲ糜谥付?Bean 的名稱。 
Java代碼  收藏代碼
  1. @Component  
  2. public class ActionMovieCatalog implements MovieCatalog {  
  3.     // ...  
  4. }  

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

下面這個(gè)XML配置會(huì)忽略所有的@Repository注解并用“stub”儲(chǔ)存庫(kù)代替。 
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、默認(rèn)情況下通過 @Component 定義的 Bean 都是 singleton 的,如果需要使用其它作用范圍的 Bean,可以通過 @Scope 注釋來達(dá)到目標(biāo) 
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的細(xì)化, 用來表示更具體的用例(例如,分別對(duì)應(yīng)了持久化層、服務(wù)層和表現(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、要檢測(cè)這些類并注冊(cè)相應(yīng)的bean,需要在XML中包含以下元素,其中'basePackage'是兩個(gè)類的公共父包 (或者可以用逗號(hào)分隔的列表來分別指定包含各個(gè)類的包)。 
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>  

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

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

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多