码迷,mamicode.com
首页 > 移动开发 > 详细

springboot获取上下文ApplicationContext

时间:2019-08-07 14:45:03      阅读:1128      评论:0      收藏:0      [点我收藏+]

标签:htm   ==   nbsp   上下   dev   vat   code   org   blog   

 

在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

springboot获取上下文ApplicationContext

标签:htm   ==   nbsp   上下   dev   vat   code   org   blog   

原文地址:https://www.cnblogs.com/xiaostudy/p/11314963.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!