【Spring boot随时获取ApplicationContex】追风赶月莫停留,平芜尽处是春山。这篇文章主要讲述Spring boot随时获取ApplicationContex相关的知识,希望能为你提供帮助。
[java] view plain copy print?
- @Service
- public class SpringManager implements ApplicationListener< ContextRefreshedEvent> {
- private static ApplicationContext applicationContext = null;
- @Override
- public void onApplicationEvent(ContextRefreshedEvent event) {
- if(applicationContext == null){
- applicationContext = event.getApplicationContext();
- }
- }
- /*ApplicationContext context= ContextLoader.getCurrentWebApplicationContext(); //尝试下这个方法*/
- public static ApplicationContext getApplicationContext() {
- return applicationContext;
- }
- }
@Servicepublic class SpringManager implements ApplicationListener< ContextRefreshedEvent> {private static ApplicationContext applicationContext = null; @Overridepublic void onApplicationEvent(ContextRefreshedEvent event) {if(applicationContext == null){applicationContext = event.getApplicationContext(); }}/*ApplicationContext context= ContextLoader.getCurrentWebApplicationContext(); //尝试下这个方法*/public static ApplicationContext getApplicationContext() {return applicationContext; }}
[java] view plain copy print?
- 或者
- @Service
- public class SpringTool implements ApplicationContextAware {
- private static ApplicationContext context = null;
- @Override
- public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
- context = applicationContext;
- }
- public static ApplicationContext getApplicationContext() {
- return context;
- }
- }
或者@Servicepublic class SpringTool implements ApplicationContextAware {private static ApplicationContext context = null; @Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {context = applicationContext; }public static ApplicationContext getApplicationContext() {return context; }}
[java] view plain copy print?
- 或者直接
- @Autowired
- ApplicationContext context;
或者直接@AutowiredApplicationContext context;
推荐阅读
- Java Service Wrapper简介与使用(转)
- 搭建android开发环境
- Java简单模拟Android中Handler-Message机制
- cordova+vue 项目打包成Android(apk)应用
- Android音视频深入八小视频录制(附源码下载)
- Android 音视频深入 九FFmpeg解码视频生成yuv文件(附源码下载)
- Android 音视频深入六使用FFmpeg播放视频(附源码下载)
- Android 音视频深入十FFmpeg给视频加特效(附源码下载)
- Android7.0调用系统相机拍照读取系统相册照片+CropImageView剪裁照片