ApplicationContextAware介面的作用

來源:互聯網
上載者:User

標籤:清除   thold   highlight   comment   lin   tar   util   產生   name   

在Web應用中,Spring容器通常採用聲明式方式配置產生:開發人員只要在web.xml中配置一個Listener,該Listener將會負責初始化Spring容器,MVC架構可以直接調用Spring容器中的Bean,無需訪問Spring容器本身。在這種情況下,容器中的Bean處於容器管理下,無需主動訪問容器,只需接受容器的依賴注入即可。

但在某些特殊的情況下,Bean需要實現某個功能,但該功能必須藉助於Spring容器才能實現,此時就必須讓該Bean先擷取Spring容器,然後藉助於Spring容器實現該功能。為了讓Bean擷取它所在的Spring容器,可以讓該Bean實現ApplicationContextAware介面。

下面樣本為實現ApplicationContextAware的工具類,可以通過其它類引用它以操作spring容器及其中的Bean執行個體。

 

[java] view plain copy 
  1. public class SpringContextHolder implements ApplicationContextAware {  
  2.     private static ApplicationContext applicationContext = null;  
  3.   
  4.     /** 
  5.      * 擷取靜態變數中的ApplicationContext. 
  6.      */  
  7.     public static ApplicationContext getApplicationContext() {  
  8.         assertContextInjected();  
  9.         return applicationContext;  
  10.     }  
  11.   
  12.     /** 
  13.      * 從靜態變數applicationContext中得到Bean, 自動轉型為所賦值對象的類型. 
  14.      */  
  15.     @SuppressWarnings("unchecked")  
  16.     public static <T> T getBean(String name) {  
  17.         assertContextInjected();  
  18.         return (T) applicationContext.getBean(name);  
  19.     }  
  20.   
  21.     /** 
  22.      * 從靜態變數applicationContext中得到Bean, 自動轉型為所賦值對象的類型. 
  23.      */  
  24.     public static <T> T getBean(Class<T> requiredType) {  
  25.         assertContextInjected();  
  26.         return applicationContext.getBean(requiredType);  
  27.     }  
  28.   
  29.     /** 
  30.      * 清除SpringContextHolder中的ApplicationContext為Null. 
  31.      */  
  32.     public static void clearHolder() {  
  33.         applicationContext = null;  
  34.     }  
  35.   
  36.     /** 
  37.      * 實現ApplicationContextAware介面, 注入Context到靜態變數中. 
  38.      */  
  39.     @Override  
  40.     public void setApplicationContext(ApplicationContext applicationContext) {  
  41.         SpringContextHolder.applicationContext = applicationContext;  
  42.     }  
  43.   
  44.     /** 
  45.      * 檢查ApplicationContext不為空白. 
  46.      */  
  47.     private static void assertContextInjected() {  
  48.         Validate.validState(applicationContext != null,  
  49.                 "applicaitonContext屬性未注入, 請在applicationContext.xml中定義SpringContextHolder.");  
  50.     }  
  51.   
  52. }  

 

Spring容器會檢測容器中的所有Bean,如果發現某個Bean實現了ApplicationContextAware介面,Spring容器會在建立該Bean之後,自動調用該Bean的setApplicationContextAware()方法,調用該方法時,會將容器本身作為參數傳給該方法——該方法中的實現部分將Spring傳入的參數(容器本身)賦給該類對象的applicationContext執行個體變數,因此接下來可以通過該applicationContext執行個體變數來訪問容器本身。

 

例如:

在CacheUtil中通過spring容器建立CacheManager執行個體

[java] view plain copy 
  1. public class CacheUtils {  
  2.     private static CacheManager cacheManager = ((CacheManager) SpringContextHolder.getBean("cacheManager"));  
  3. }  

ApplicationContextAware介面的作用

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.