springboot 實(shí)用接口

HttpServletRequestWapper 修改請求參數(shù) 裝飾類

ApplicationRuner spring 啟動(dòng)后立即執(zhí)行可用類

Environment 可直接注入 讀取propertites 文件 或激活 相應(yīng)的profile 及一組bean

ApplicationContextAware Spring容器會檢測容器中的所有Bean,如果發(fā)現(xiàn)某個(gè)Bean實(shí)現(xiàn)了ApplicationContextAware接口,Spring容器會在創(chuàng)建該Bean之后,自動(dòng)調(diào)用該Bean的setApplicationContextAware()方法,調(diào)用該方法時(shí),會將容器本身作為參數(shù)傳給該方法——該方法中的實(shí)現(xiàn)部分將Spring傳入的參數(shù)(容器本身)賦給該類對象的applicationContext實(shí)例變量,因此接下來可以通過該applicationContext實(shí)例變量來訪問容器本身。

在Web應(yīng)用中,Spring容器通常采用聲明式方式配置產(chǎn)生:開發(fā)者只要在web.xml中配置一個(gè)Listener,該Listener將會負(fù)責(zé)初始化Spring容器,MVC框架可以直接調(diào)用Spring容器中的Bean,無需訪問Spring容器本身。在這種情況下,容器中的Bean處于容器管理下,無需主動(dòng)訪問容器,只需接受容器的依賴注入即可。

但在某些特殊的情況下,Bean需要實(shí)現(xiàn)某個(gè)功能,但該功能必須借助于Spring容器才能實(shí)現(xiàn),此時(shí)就必須讓該Bean先獲取Spring容器,然后借助于Spring容器實(shí)現(xiàn)該功能。為了讓Bean獲取它所在的Spring容器,可以讓該Bean實(shí)現(xiàn)ApplicationContextAware接口。

下面示例為實(shí)現(xiàn)ApplicationContextAware的工具類,可以通過其它類引用它以操作spring容器及其中的Bean實(shí)例。

public class SpringContextHolder implements ApplicationContextAware {

private static ApplicationContext applicationContext = null;



/**

* 獲取靜態(tài)變量中的ApplicationContext.

*/

public static ApplicationContext getApplicationContext() {

assertContextInjected();

return applicationContext;

}



/**

* 從靜態(tài)變量applicationContext中得到Bean, 自動(dòng)轉(zhuǎn)型為所賦值對象的類型.

*/

@SuppressWarnings("unchecked")

public static <T> T getBean(String name) {

assertContextInjected();

return (T) applicationContext.getBean(name);

}



/**

* 從靜態(tài)變量applicationContext中得到Bean, 自動(dòng)轉(zhuǎn)型為所賦值對象的類型.

*/

public static <T> T getBean(Class<T> requiredType) {

assertContextInjected();

return applicationContext.getBean(requiredType);

}



/**

* 清除SpringContextHolder中的ApplicationContext為Null.

*/

public static void clearHolder() {

applicationContext = null;

}



/**

* 實(shí)現(xiàn)ApplicationContextAware接口, 注入Context到靜態(tài)變量中.

*/

@Override

public void setApplicationContext(ApplicationContext applicationContext) {

SpringContextHolder.applicationContext = applicationContext;

}



/**

* 檢查ApplicationContext不為空.

*/

private static void assertContextInjected() {

Validate.validState(applicationContext != null,

"applicaitonContext屬性未注入, 請?jiān)赼pplicationContext.xml中定義SpringContextHolder.");

}



}

Spring容器會檢測容器中的所有Bean,如果發(fā)現(xiàn)某個(gè)Bean實(shí)現(xiàn)了ApplicationContextAware接口,Spring容器會在創(chuàng)建該Bean之后,自動(dòng)調(diào)用該Bean的setApplicationContextAware()方法,調(diào)用該方法時(shí),會將容器本身作為參數(shù)傳給該方法——該方法中的實(shí)現(xiàn)部分將Spring傳入的參數(shù)(容器本身)賦給該類對象的applicationContext實(shí)例變量,因此接下來可以通過該applicationContext實(shí)例變量來訪問容器本身。

例如:

在CacheUtil中通過spring容器創(chuàng)建CacheManager實(shí)例

public class CacheUtils {

private static CacheManager cacheManager = ((CacheManager) SpringContextHolder.getBean("cacheManager"));

}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容