標籤:ram ons ade 工具 util 特殊 str code pre
1/==============
在Web應用中,Spring容器通常採用聲明式方式配置產生:開發人員只要在web.xml中配置一個Listener,該Listener將會負責初始化Spring容器,MVC架構可以直接調用Spring容器中的Bean,無需訪問Spring容器本身。在這種情況下,容器中的Bean處於容器管理下,無需主動訪問容器,只需接受容器的依賴注入即可。但在某些特殊的情況下,Bean需要實現某個功能,但該功能必須藉助於Spring容器才能實現,此時就必須讓該Bean先擷取Spring容器,然後藉助於Spring容器實現該功能。為了讓Bean擷取它所在的Spring容器,可以讓該Bean實現ApplicationContextAware介面。下面樣本為實現ApplicationContextAware的工具類,可以通過其它類引用它以操作spring容器及其中的Bean執行個體。 public class SpringContextHolder implements ApplicationContextAware { private static ApplicationContext applicationContext = null; /** * 擷取靜態變數中的ApplicationContext. */ public static ApplicationContext getApplicationContext() { assertContextInjected(); return applicationContext; } /** * 從靜態變數applicationContext中得到Bean, 自動轉型為所賦值對象的類型. */ @SuppressWarnings("unchecked") public static <T> T getBean(String name) { assertContextInjected(); return (T) applicationContext.getBean(name); } /** * 從靜態變數applicationContext中得到Bean, 自動轉型為所賦值對象的類型. */ public static <T> T getBean(Class<T> requiredType) { assertContextInjected(); return applicationContext.getBean(requiredType); } /** * 清除SpringContextHolder中的ApplicationContext為Null. */ public static void clearHolder() { applicationContext = null; } /** * 實現ApplicationContextAware介面, 注入Context到靜態變數中. */ @Override public void setApplicationContext(ApplicationContext applicationContext) { SpringContextHolder.applicationContext = applicationContext; } /** * 檢查ApplicationContext不為空白. */ private static void assertContextInjected() { Validate.validState(applicationContext != null, "applicaitonContext屬性未注入, 請在applicationContext.xml中定義SpringContextHolder."); } } Spring容器會檢測容器中的所有Bean,如果發現某個Bean實現了ApplicationContextAware介面,Spring容器會在建立該Bean之後,自動調用該Bean的setApplicationContextAware()方法,調用該方法時,會將容器本身作為參數傳給該方法——該方法中的實現部分將Spring傳入的參數(容器本身)賦給該類對象的applicationContext執行個體變數,因此接下來可以通過該applicationContext執行個體變數來訪問容器本身。 例如:在CacheUtil中通過spring容器建立CacheManager執行個體public class CacheUtils { private static CacheManager cacheManager = ((CacheManager) SpringContextHolder.getBean("cacheManager")); }
2/==========================
一、這個介面有什麼用?當一個類實現了這個介面(ApplicationContextAware)之後,這個類就可以方便獲得ApplicationContext中的所有bean。換句話說,就是這個類可以直接擷取spring設定檔中,所有有引用到的bean對象。二、怎麼用?舉個例子吧:例如我有一個方法類AppUtil,這個方法類中需要使用到的ApplicationContext中的某個bean(companyService)。1、因為spring要建立屬於自己的容器,就必須要載入自己的設定檔。 這個時候,需要註冊ContextLoaderListener或者這個類的子類。在web.xml加上以下的資訊:<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>當然,這樣子的話只會讀取預設路徑下的application.xml設定檔的。如果需要讀取特定路徑下的設定檔。需要在web.xml中添加如下資訊。可以參考我的樣本,指定設定檔,如下:<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:conf/app-context.xml</param-value> </context-param> 注意:<param-name>contextConfigLocation</param-name>是不能改變的。 2、方法類AppUtil的處理方法類AppUtil實現ApplicationContextAware介面:public class AppUtil implements ApplicationContextAware為方法類AppUtil增加一個靜態成員ApplicationContext類型的對象。以後方法類AppUtil擷取ApplicationContext,就是通過讀取這個成員變數的。具體如下所示:private static ApplicationContext appContext; 實現ApplicationContextAware介面的預設方法: public void setApplicationContext(ApplicationContext paramApplicationContext) throws BeansException { appContext = paramApplicationContext; } 3、在spring的設定檔中,註冊方法類AppUtil嚴格上來說,方法類AppUtil是一個bean,而且從步驟2中我們不難發現,之所以方法類AppUtil能夠靈活自如地擷取ApplicationContext就是因為spring能夠為我們自動地執行了setApplicationContext。但是,spring不會無緣無故地為某個類執行它的方法的,所以,就很有必要通過註冊方法類AppUtil的方式告知spring有這樣子一個類的存在。(載入Spring設定檔時,如果Spring設定檔中所定義的Bean類實現了ApplicationContextAware 介面,那麼在載入Spring設定檔時,會自動調用ApplicationContextAware 介面中的public void setApplicationContext(ApplicationContext context) throws BeansException方法,獲得ApplicationContext對象,ApplicationContext對象是由spring注入的。前提必須在Spring設定檔中指定該類)其實,方法很簡單,就是將方法類AppUtil作為一個普通的bean在spring的設定檔中進行註冊:<bean id="appUtil" class="com.htsoft.core.util.AppUtil"/>4、使用靜態成員ApplicationContext類型的對象,appContext,來調用其他bean。在方法類AppUtil中增加如下方法:public static Object getBean(String paramString) { return appContext.getBean(paramString); }那麼,在方法類AppUtil中就能夠靈活地調用其他任何一個bean了,例如:CompanyService localCompanyService = (CompanyService)getBean("companyService");註:設定檔中關於companyService的內容:<bean id="companyService" class="com.kaiwii.service.system.impl.CompanyServiceImpl"> <constructor-arg index="0" ref="companyDao"/> </bean>
ApplicationContextAware介面的作用