zoukankan      html  css  js  c++  java
  • springboot获取上下文ApplicationContext

    在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

  • 相关阅读:
    正则表达式---用户验证
    最短路 Dijkstra
    最短路 Dijkstra+堆优化
    LCIS(最长公共上升子序列)
    最小生成树 Kruskal
    网络流 最大流
    网络流 最小费用最大流
    树链剖分
    树状数组
    双重Hash
  • 原文地址:https://www.cnblogs.com/xiaostudy/p/11314963.html
Copyright © 2011-2022 走看看