zoukankan      html  css  js  c++  java
  • springboot启动后执行某些动作

    springboot启动后执行某些动作

    1.主要是springboot的启动run方法里调用了callRunners,会去调用实现了ApplicationRunner接口的类的方法

    public ConfigurableApplicationContext run(String... args) {
    		StopWatch stopWatch = new StopWatch();
    		stopWatch.start();
    		DefaultBootstrapContext bootstrapContext = createBootstrapContext();
    		ConfigurableApplicationContext context = null;
    		configureHeadlessProperty();
    		SpringApplicationRunListeners listeners = getRunListeners(args);
    		listeners.starting(bootstrapContext, this.mainApplicationClass);
    		try {
    			ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
    			ConfigurableEnvironment environment = prepareEnvironment(listeners, bootstrapContext, applicationArguments);
    			configureIgnoreBeanInfo(environment);
    			Banner printedBanner = printBanner(environment);
    			context = createApplicationContext();
    			context.setApplicationStartup(this.applicationStartup);
    			prepareContext(bootstrapContext, context, environment, listeners, applicationArguments, printedBanner);
    			refreshContext(context);
    			afterRefresh(context, applicationArguments);
    			stopWatch.stop();
    			if (this.logStartupInfo) {
    				new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);
    			}
    			listeners.started(context);
    			callRunners(context, applicationArguments);
    		}
    		catch (Throwable ex) {
    			handleRunFailure(context, ex, listeners);
    			throw new IllegalStateException(ex);
    		}
    
    		try {
    			listeners.running(context);
    		}
    		catch (Throwable ex) {
    			handleRunFailure(context, ex, null);
    			throw new IllegalStateException(ex);
    		}
    		return context;
    	}
    

    2.编写相关调用

    此处applicationContext加载完成后进行了输出DeltaConfigApplication start finished !!!!!!!

    @Component
    public class DeltaConfigApplicationRunner implements ApplicationRunner {
    
        private static final Logger logger = LoggerFactory.getLogger(DeltaConfigApplicationRunner.class);
    
        public void run(ApplicationArguments args) throws Exception {
            logger.info("DeltaConfigApplication start finished !!!!!!!");
        }
    }
    
    原创:做时间的朋友
  • 相关阅读:
    C#获取屏幕鼠标所指点的颜色
    C#连接SQLServer数据库基本实现
    论文摘要写法
    红黑树
    递归、迭代和分治法
    逻辑右/左移与算术右/左移
    C 中数字数据类型在不同机器上所占字节数
    十进制转十六进制
    c带头结点的单链表逆置
    求一维数组长度误区
  • 原文地址:https://www.cnblogs.com/PythonOrg/p/15428727.html
Copyright © 2011-2022 走看看