Python 官方文档:入门教程 => 点击学习
目录SpringBoot上下文对象获取或者更简单的写法:Spring Boot获取上下文 随时取出被spring管理的bean对象方法一:方式二: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;
@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;
public static void initContext(ApplicationContext ctx) {
if(ctx == null) {
log.warn("ApplicationContext is null.");
return;
}
context = ctx;
}
public static <T> T getBean(Class<T> cls) {
return context == null ? null : context.getBean(cls);
}
public static Object getBean(String name) {
return context == null ? null : context.getBean(name);
}
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
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。
--结束END--
本文标题: Springboot 如何获取上下文对象
本文链接: https://www.lsjlt.com/news/132573.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0