Springboot 如何獲取上下文對象
Springboot上下文對象獲取
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上下文,通過上下文可以拿到配置文件中的配置參數(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; } }
或者更簡單的寫法:
定義一個(gè)AppUtil.java類:
public class AppUtil { public static ConfigurableApplicationContext CONTEXT; }
在啟動類中進(jìn)行賦值
public static void main(String[] args) { AppUtil.CONTEXT = SpringApplication.run(RedisFlinkApplication.class, args); }
spring boot獲取上下文 隨時(shí)取出被spring管理的bean對象
方法一:
實(shí)現(xiàn)ApplicationContextAware,重寫setApplicationContext方法。這個(gè)方式下,工具類也被注冊成了Bean,既然這樣,那就必須確保該類能被Spring自動掃描到。
@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(); } }
方式二:
我想要的工具類,往往會部署在公共jar中,一般情況下不能被SpringBoot程序掃描到。所有就有手動注入上下文的方式。
@Slf4j public class SpringUtils { private SpringUtils() { } private static ApplicationContext context = null; /** * 初始化Spring上下文 * * @param ctx 上下文對象 */ public static void initContext(ApplicationContext ctx) { if(ctx == null) { log.warn("ApplicationContext is null."); return; } context = ctx; } /** * 根據(jù)類型獲取Bean * * @param cls Bean類 * @param <T> Bean類型 * @return Bean對象 */ public static <T> T getBean(Class<T> cls) { return context == null ? null : context.getBean(cls); } /** * 根據(jù)名稱獲取Bean * * @param name Bean名稱 * @return Bean對象 */ public static Object getBean(String name) { return context == null ? null : context.getBean(name); } /** * 根據(jù)Bean名稱和類獲取Bean對象 * * @param name Bean名稱 * @param cls Bean類 * @param <T> Bean類型 * @return Bean對象 */ public static <T> T getBean(String name, Class<T> cls) { return context == null ? null : context.getBean(name, cls); } }
此種方式不用實(shí)現(xiàn)ApplicationContextAware接口,但是需要手動設(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)游戲相信大家都玩過,是一款非常有趣的小游戲,那么基于代碼是如何實(shí)現(xiàn)的呢?今天小編給大家?guī)硪黄坛處椭蠹覍W(xué)習(xí)java實(shí)現(xiàn)潛艇大戰(zhàn)游戲,感謝的朋友一起看看吧2021-06-06springboot 實(shí)現(xiàn)長鏈接轉(zhuǎn)短鏈接的示例代碼
短鏈接服務(wù)通過將長URL轉(zhuǎn)換成6位短碼,并存儲長短鏈接對應(yīng)關(guān)系到數(shù)據(jù)庫中,用戶訪問短鏈接時(shí),系統(tǒng)通過查詢數(shù)據(jù)庫并重定向到原始URL,實(shí)現(xiàn)快速訪問,本文就來介紹一下如何使用,感興趣的可以了解一下2024-09-09淺談關(guān)于Java正則和轉(zhuǎn)義中\(zhòng)\和\\\\的理解
這篇文章主要介紹了淺談關(guān)于Java正則和轉(zhuǎn)義中\(zhòng)\和\\\\的理解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08Springboot利用Aop捕捉注解實(shí)現(xiàn)業(yè)務(wù)異步執(zhí)行
在開發(fā)過程中,盡量會將比較耗時(shí)且并不會影響請求的響應(yīng)結(jié)果的業(yè)務(wù)放在異步線程池中進(jìn)行處理,那么到時(shí)什么任務(wù)在執(zhí)行的時(shí)候會創(chuàng)建單獨(dú)的線程進(jìn)行處理呢?這篇文章主要介紹了Springboot利用Aop捕捉注解實(shí)現(xiàn)業(yè)務(wù)異步執(zhí)行2023-04-04