這篇文章主要講解了“Springboot怎么獲取上下文對象”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“Springboot怎么獲取上下文對象”吧!
Springboot上下文對象獲取
或者更簡單的寫法:
spring boot獲取上下文 隨時取出被spring管理的bean對象
方法一:
方式二:
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上下文,通過上下文可以拿到配置文件中的配置參數
*/
@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;
}
}定義一個AppUtil.java類:
public class AppUtil {
public static ConfigurableApplicationContext CONTEXT;
}在啟動類中進行賦值
public static void main(String[] args) {
AppUtil.CONTEXT = SpringApplication.run(RedisFlinkApplication.class, args);
}實現ApplicationContextAware,重寫setApplicationContext方法。這個方式下,工具類也被注冊成了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;
}
/**
* 根據類型獲取Bean
*
* @param cls Bean類
* @param <T> Bean類型
* @return Bean對象
*/
public static <T> T getBean(Class<T> cls) {
return context == null ? null : context.getBean(cls);
}
/**
* 根據名稱獲取Bean
*
* @param name Bean名稱
* @return Bean對象
*/
public static Object getBean(String name) {
return context == null ? null : context.getBean(name);
}
/**
* 根據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);
}
}此種方式不用實現ApplicationContextAware接口,但是需要手動設置上下文。所以在程序入口處,需要調用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
感謝各位的閱讀,以上就是“Springboot怎么獲取上下文對象”的內容了,經過本文的學習后,相信大家對Springboot怎么獲取上下文對象這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。