springboot应用,启动spring容器大致有如下几个过程:
- 容器开始启动
- 初始化环境变量
- 初始化上下文
- 加载上下文
- 完成
对应的Spring应用的启动器的监听器可以监听以上的过程,接口如下:
1 public interface SpringApplicationRunListener { 2 3 /** 4 * Called immediately when the run method has first started. Can be used for very 5 * early initialization. 6 */ 7 void started(); 8 9 /** 10 * Called once the environment has been prepared, but before the 11 * {@link ApplicationContext} has been created. 12 * @param environment the environment 13 */ 14 void environmentPrepared(ConfigurableEnvironment environment); 15 16 /** 17 * Called once the {@link ApplicationContext} has been created and prepared, but 18 * before sources have been loaded. 19 * @param context the application context 20 */ 21 void contextPrepared(ConfigurableApplicationContext context); 22 23 /** 24 * Called once the application context has been loaded but before it has been 25 * refreshed. 26 * @param context the application context 27 */ 28 void contextLoaded(ConfigurableApplicationContext context); 29 30 /** 31 * Called immediately before the run method finishes. 32 * @param context the application context or null if a failure occurred before the 33 * context was created 34 * @param exception any run exception or null if run completed successfully. 35 */ 36 void finished(ConfigurableApplicationContext context, Throwable exception); 37 38 }