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 !!!!!!!");
        }
    }
    
    原创:做时间的朋友
  • 相关阅读:
    Java--Filter(过滤器)
    TP5.1验证Token和Electron-vue头部携带Token
    TP5.1让验证码在另外的项目(Electron-vue)里面使用
    Electron-vue请求携带cookie跨域问题
    Electron-vue在发送请求时携带cookie
    TP5.1解决跨域
    Electron-vue解决跨域
    Electron-vue运行之后出现了文件浏览器
    Electron-vue取消代码检查Eslint
    使用Composer安装TP5.1出现zsh: no matches found: 5.1.*
  • 原文地址:https://www.cnblogs.com/PythonOrg/p/15428727.html
Copyright © 2011-2022 走看看