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上下文,通过上下文可以拿到配置文件中的配置参数 */ @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); }
spring boot获取上下文 随时取出被spring管理的bean对象
方法一:
实现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
以上为个人经验,希望能给大家一个参考,也希望大家多多支持自学编程网。
- 本文固定链接: https://www.zxbcw.cn/post/219625/
- 转载请注明:必须在正文中标注并保留原文链接
- QQ群: PHP高手阵营官方总群(344148542)
- QQ群: Yii2.0开发(304864863)