合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
我们可以实现如下四个接口来观察SpringBoot的启动过程。 ``` ApplicationContextInitializer SpringApplicationRunListener ApplicationRunner CommandLineRunner ``` 演示SpringBoot启动过程。 **1. 实现上面的4个接口** (1)*`com.exa.process.listener.MyApplicationRunner`* ```java package com.exa.process.listener; import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; import org.springframework.stereotype.Component; @Component public class MyApplicationRunner implements ApplicationRunner { @Override public void run(ApplicationArguments args) throws Exception { System.out.println("ApplicationRunner——>run()"); } } ``` (2)*`com.exa.process.listener.MyCommandLineRunner`* ```java package com.exa.process.listener; import org.springframework.boot.CommandLineRunner; import org.springframework.stereotype.Component; @Component public class MyCommandLineRunner implements CommandLineRunner { @Override public void run(String... args) throws Exception { System.out.println("CommandLineRunner——>run()"); } } ``` (3)*`com.exa.process.listener.MyApplicationContextInitializer`* ```java package com.exa.process.listener; import org.springframework.context.ApplicationContextInitializer; import org.springframework.context.ConfigurableApplicationContext; /** * 监听ConfigurableApplicationContext的启动 */ public class MyApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> { @Override public void initialize(ConfigurableApplicationContext configurableApplicationContext) { System.out.println("ApplicationContextInitializer——>initialize()"); } } ``` (4)*`com.exa.process.listener.MySpringApplicationRunListener.java`* ```java package com.exa.process.listener; import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplicationRunListener; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.core.env.ConfigurableEnvironment; public class MySpringApplicationRunListener implements SpringApplicationRunListener { public MySpringApplicationRunListener(SpringApplication application, String[] args) { } @Override public void starting() { System.out.println("SpringApplicationRunListener——>starting()"); } @Override public void environmentPrepared(ConfigurableEnvironment environment) { // 可以获取当前机器的操作系统等信息 Object mes = environment.getSystemProperties().get("os.name"); System.out.println("SpringApplicationRunListener——>environmentPrepared()---操作系统:"+mes); } @Override public void contextPrepared(ConfigurableApplicationContext context) { System.out.println("SpringApplicationRunListener——>contextPrepared()"); } @Override public void contextLoaded(ConfigurableApplicationContext context) { System.out.println("SpringApplicationRunListener——>contextLoaded()"); } @Override public void started(ConfigurableApplicationContext context) { System.out.println("SpringApplicationRunListener——>started()"); } @Override public void running(ConfigurableApplicationContext context) { System.out.println("SpringApplicationRunListener——>running()"); } @Override public void failed(ConfigurableApplicationContext context, Throwable exception) { System.out.println("SpringApplicationRunListener——>failed()"); } } ``` **2. 编写`spring.factories`文件** 将MyApplicationContextInitializer、MySpringApplicationRunListener通过`spring.factories`注入到IoC容器中。 *`resources/META-INF/spring.factories`* ```xml # Initializers org.springframework.context.ApplicationContextInitializer=\ com.exa.process.listener.MyApplicationContextInitializer # Application Listeners org.springframework.boot.SpringApplicationRunListener=\ com.exa.process.listener.MySpringApplicationRunListener ``` **3. 运行项目后日志输出顺序如下** ``` SpringApplicationRunListener——>starting() SpringApplicationRunListener——>environmentPrepared()---操作系统:Windows 10 ApplicationContextInitializer——>initialize() SpringApplicationRunListener——>contextPrepared() SpringApplicationRunListener——>contextLoaded() SpringApplicationRunListener——>started() ApplicationRunner——>run() CommandLineRunner——>run() SpringApplicationRunListener——>running() // 由于项目并没有运行失败所以SpringApplicationRunListener——>failed()并没有运行 ```