Springboot 如何獲取上下文對(duì)象
Springboot上下文對(duì)象獲取
package it.benjamin.aspirinweb.mem; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; /** * 獲取Springboot上下文,通過(guò)上下文可以拿到配置文件中的配置參數(shù) */ @Component public class AppContextTool implements ApplicationContextAware { private static ApplicationContext context; public String getConfig(String key) { return context.getEnvironment().getProperty(key); } @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.context = applicationContext; } }
或者更簡(jiǎn)單的寫(xiě)法:
定義一個(gè)AppUtil.java類(lèi):
public class AppUtil { public static ConfigurableApplicationContext CONTEXT; }
在啟動(dòng)類(lèi)中進(jìn)行賦值
public static void main(String[] args) { AppUtil.CONTEXT = SpringApplication.run(RedisFlinkApplication.class, args); }
spring boot獲取上下文 隨時(shí)取出被spring管理的bean對(duì)象
方法一:
實(shí)現(xiàn)ApplicationContextAware,重寫(xiě)setApplicationContext方法。這個(gè)方式下,工具類(lèi)也被注冊(cè)成了Bean,既然這樣,那就必須確保該類(lèi)能被Spring自動(dòng)掃描到。
@Component public class SpringContextUtils implements ApplicationContextAware { private static ApplicationContext applicationContext; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { SpringContextUtils.applicationContext = applicationContext; } public static <T> T getBean(Class<T> cls) { if (applicationContext == null) { throw new RuntimeException("applicationContext注入失敗"); } return applicationContext.getBean(cls); } public static Object getBean(String name) { if (applicationContext == null) { throw new RuntimeException("applicationContext注入失敗"); } return applicationContext.getBean(name); } public static <T> T getBean(String name, Class<T> cls) { if (applicationContext == null) { throw new RuntimeException("applicationContext注入失敗"); } return applicationContext.getBean(name, cls); } public static HttpServletRequest getRequest() { ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder .getRequestAttributes(); return requestAttributes == null ? null : requestAttributes.getRequest(); } }
方式二:
我想要的工具類(lèi),往往會(huì)部署在公共jar中,一般情況下不能被SpringBoot程序掃描到。所有就有手動(dòng)注入上下文的方式。
@Slf4j public class SpringUtils { private SpringUtils() { } private static ApplicationContext context = null; /** * 初始化Spring上下文 * * @param ctx 上下文對(duì)象 */ public static void initContext(ApplicationContext ctx) { if(ctx == null) { log.warn("ApplicationContext is null."); return; } context = ctx; } /** * 根據(jù)類(lèi)型獲取Bean * * @param cls Bean類(lèi) * @param <T> Bean類(lèi)型 * @return Bean對(duì)象 */ public static <T> T getBean(Class<T> cls) { return context == null ? null : context.getBean(cls); } /** * 根據(jù)名稱(chēng)獲取Bean * * @param name Bean名稱(chēng) * @return Bean對(duì)象 */ public static Object getBean(String name) { return context == null ? null : context.getBean(name); } /** * 根據(jù)Bean名稱(chēng)和類(lèi)獲取Bean對(duì)象 * * @param name Bean名稱(chēng) * @param cls Bean類(lèi) * @param <T> Bean類(lèi)型 * @return Bean對(duì)象 */ public static <T> T getBean(String name, Class<T> cls) { return context == null ? null : context.getBean(name, cls); } }
此種方式不用實(shí)現(xiàn)ApplicationContextAware接口,但是需要手動(dòng)設(shè)置上下文。所以在程序入口處,需要調(diào)用initContext方法,完成上下文的初始化。
public static void main(String[] args) { ApplicationContext context = SpringApplication.run(YCloudsServiceBApplication.class, args); SpringUtils.initContext(context); }
GitHub地址:https://github.com/ye17186/spring-boot-learn
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring學(xué)習(xí)筆記3之消息隊(duì)列(rabbitmq)發(fā)送郵件功能
這篇文章主要介紹了Spring學(xué)習(xí)筆記3之消息隊(duì)列(rabbitmq)發(fā)送郵件功能的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07java實(shí)現(xiàn)潛艇大戰(zhàn)游戲源碼
潛艇大戰(zhàn)游戲相信大家都玩過(guò),是一款非常有趣的小游戲,那么基于代碼是如何實(shí)現(xiàn)的呢?今天小編給大家?guī)?lái)一篇教程幫助大家學(xué)習(xí)java實(shí)現(xiàn)潛艇大戰(zhàn)游戲,感謝的朋友一起看看吧2021-06-06springboot 實(shí)現(xiàn)長(zhǎng)鏈接轉(zhuǎn)短鏈接的示例代碼
短鏈接服務(wù)通過(guò)將長(zhǎng)URL轉(zhuǎn)換成6位短碼,并存儲(chǔ)長(zhǎng)短鏈接對(duì)應(yīng)關(guān)系到數(shù)據(jù)庫(kù)中,用戶(hù)訪問(wèn)短鏈接時(shí),系統(tǒng)通過(guò)查詢(xún)數(shù)據(jù)庫(kù)并重定向到原始URL,實(shí)現(xiàn)快速訪問(wèn),本文就來(lái)介紹一下如何使用,感興趣的可以了解一下2024-09-09淺談關(guān)于Java正則和轉(zhuǎn)義中\(zhòng)\和\\\\的理解
這篇文章主要介紹了淺談關(guān)于Java正則和轉(zhuǎn)義中\(zhòng)\和\\\\的理解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08Springboot利用Aop捕捉注解實(shí)現(xiàn)業(yè)務(wù)異步執(zhí)行
在開(kāi)發(fā)過(guò)程中,盡量會(huì)將比較耗時(shí)且并不會(huì)影響請(qǐng)求的響應(yīng)結(jié)果的業(yè)務(wù)放在異步線程池中進(jìn)行處理,那么到時(shí)什么任務(wù)在執(zhí)行的時(shí)候會(huì)創(chuàng)建單獨(dú)的線程進(jìn)行處理呢?這篇文章主要介紹了Springboot利用Aop捕捉注解實(shí)現(xiàn)業(yè)務(wù)異步執(zhí)行2023-04-04