这篇文章主要讲解了“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怎么获取上下文对象这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。