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 !!!!!!!");
        }
    }
    
    原创:做时间的朋友
  • 相关阅读:
    openresty开发系列35--openresty执行流程之5内容content阶段
    openresty开发系列34--openresty执行流程之4访问阶段
    openresty开发系列33--openresty执行流程之3重写rewrite和重定向
    [转] Dangers of using dlsym() with RTLD_NEXT
    fork failed because of Out Of Memory
    gdb 拾穗
    原子变量的性能问题
    blktrace + blkparse + btt 分析IO
    [转] 利用BLKTRACE分析IO性能
    使用perf + FlameGraph生成进程火焰图
  • 原文地址:https://www.cnblogs.com/PythonOrg/p/15428727.html
Copyright © 2011-2022 走看看