springboot获取上下文ApplicationContext
2024-09-03 05:32:35
在springboot主程序里改成
public static void main(String[] args) {
// SpringApplication.run(SpringbootAPP.class, args);换成下面
SpringApplication sa = new SpringApplication(SpringbootAPP.class);
sa.addListeners(new MainBusiListeners());
sa.run(args);
}
package com.xxx.xxxx; import com.xxx.utils.SpringContextUtils;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent; /**
* 启动监听
*/
public class MainBusiListeners implements ApplicationListener<ContextRefreshedEvent> {
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
SpringContextUtils.setApplicationContextByMain(event.getApplicationContext());
}
}
@Component
public class SpringContextUtils { private static ApplicationContext applicationContext; public static void setApplicationContextByMain(ApplicationContext applicationContext) {
if (SpringContextUtils.applicationContext == null) {
SpringContextUtils.applicationContext = applicationContext;
}
}
参考文章:https://www.cnblogs.com/qq931399960/p/10184151.html
最新文章
- ndk学习16: unix domain socket
- bc.34.B.Building Blocks(贪心)
- 仿php的日期函数,asp时间处理函数
- char*与char[]
- 从Chrome源码看浏览器的事件机制
- react-router 离开路由前确认
- springboot~Money类型在序列化时遇到的问题与解决
- Linux下利用文件描述符恢复的成功失败实验
- LeetCode算法题-Implement Stack Using Queues
- Individual Project 1 总结
- Java过滤器Filter的使用详解
- 转 c#中stringbuilder的使用
- JS 判断object是否包含某个键
- elast alert
- spfa 单源最短路究极算法
- ASP.NET MVC4企业级实战目录
- js在一个可编辑的div光标处插入图片或者文本(兼容ie,火狐等浏览器)
- (转)Memcached 之 .NET(C#)实例分析
- malloc与free函数用法
- java算法 蓝桥杯算法训练 Fibonacci数列