SpringBoot原理发现(四)
说明:
本系列基于SpringBoot 2.2.9.RELEASE 版本,对SpringBoot的原理进行分析,一共分为四节:
SpringBoot原理发现(一):创建Hello World,对pom依赖以及@SpringBootApplication注解进行分析
SpringBoot原理发现(二):分析SpringBoot自动配置原理
SpringBoot原理发现(三):通过主配置类main方法分析SpringBoot启动配置原理
SpringBoot原理发现(四):了解SpringBoot启动中的几个重要回调机制
SpringBoot回调机制
针对SpringBoot原理发现(三)第二步骤springApplication.run(args)应用启动中包含的回调机制,编写如下四个实现类,主要观察启动流程
1.ApplicationContextInitializer
@Component public class MyApplicationContextInitializer implements ApplicationContextInitializer { @Override public void initialize(ConfigurableApplicationContext applicationContext) { System.out.println("MyApplicationContextInitializer initialize ..."); } }
2.SpringApplicationRunListener
@Component public class MySpringApplicationRunListener implements SpringApplicationRunListener { public MySpringApplicationRunListener(){} public MySpringApplicationRunListener(SpringApplication application, String[] args){} public void starting() { System.out.println("MySpringApplicationRunListener starting..."); } public void environmentPrepared(ConfigurableEnvironment environment) { System.out.println("MySpringApplicationRunListener environmentPrepared..."); } public void contextPrepared(ConfigurableApplicationContext context) { System.out.println("MySpringApplicationRunListener contextPrepared..."); } public void contextLoaded(ConfigurableApplicationContext context) { System.out.println("MySpringApplicationRunListener contextLoaded..."); } public void started(ConfigurableApplicationContext context) { System.out.println("MySpringApplicationRunListener started..."); } public void running(ConfigurableApplicationContext context) { System.out.println("MySpringApplicationRunListener running..."); } }
通过SpringBoot原理发现(三)知道ApplicationContextInitializer和SpringApplicationRunListener是从
META-INF/spring.factories读取的,所以需要配置该文件。
META-INF/spring.factories配置:
# Initializers org.springframework.context.ApplicationContextInitializer=\ com.example.demo.listener.MyApplicationContextInitializer org.springframework.boot.SpringApplicationRunListener=\ com.example.demo.listener.MySpringApplicationRunListener
3.ApplicationRunner
@Component public class MyApplicationRunner implements ApplicationRunner { @Override public void run(ApplicationArguments args) throws Exception { System.out.println("MyApplicationRunner run..."); } }
4.CommandLineRunner
@Component
public class MyCommandLineRunner implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println("MyCommandLineRunner run");
}
}
输出
观察结果能验证出输出顺序和上一篇文章分析结果一样
MySpringApplicationRunListener starting... MySpringApplicationRunListener environmentPrepared... . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.2.9.RELEASE) MyApplicationContextInitializer initialize ... MySpringApplicationRunListener contextPrepared... 2020-10-30 17:46:09.280 INFO 5132 --- [ main] com.example.demo.DemoApplication : Starting DemoApplication on 2020-10-30 17:46:09.283 INFO 5132 --- [ main] com.example.demo.DemoApplication : No active profile set, falling back to default profiles: default MySpringApplicationRunListener contextLoaded... 2020-10-30 17:46:10.410 INFO 5132 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2020-10-30 17:46:10.425 INFO 5132 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2020-10-30 17:46:10.426 INFO 5132 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.37] 2020-10-30 17:46:10.574 INFO 5132 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2020-10-30 17:46:10.579 INFO 5132 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1256 ms 2020-10-30 17:46:10.766 INFO 5132 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor' 2020-10-30 17:46:10.966 INFO 5132 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2020-10-30 17:46:10.970 INFO 5132 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 3.292 seconds (JVM running for 3.959) MySpringApplicationRunListener started... MyApplicationRunner run... MyCommandLineRunner run MySpringApplicationRunListener running...