本例介绍SpringBoot的事件监听机制及使用
主要有ApplicationContextInitializer(上下文初始化器)、SpringApplicationRunListener(Spring应用运行监听器)、ApplicationRunner(启动加载类)、CommandLineRunner(启动加载类)
关于调用流程参考【SpringBoot】SpringBoot 启动原理(十五)
ApplicationContextInitializer(上下文初始化器)
1、新建一个SpringBoot Web项目
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>com.test</groupId> 8 <artifactId>test-springboot-runner</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 11 <parent> 12 <groupId>org.springframework.boot</groupId> 13 <artifactId>spring-boot-starter-parent</artifactId> 14 <version>2.1.8.RELEASE</version> 15 </parent> 16 17 <properties> 18 19 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 20 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 21 <java.version>1.8</java.version> 22 </properties> 23 24 <dependencies> 25 26 <dependency> 27 <groupId>org.springframework.boot</groupId> 28 <artifactId>spring-boot-starter-web</artifactId> 29 </dependency> 30 31 32 <dependency> 33 <groupId>org.springframework.boot</groupId> 34 <artifactId>spring-boot-starter-test</artifactId> 35 <scope>test</scope> 36 </dependency> 37 38 </dependencies> 39 40 41 <!-- SpringBoot打包插件,可以将代码打包成一个可执行的jar包 --> 42 <build> 43 <plugins> 44 <plugin> 45 <groupId>org.springframework.boot</groupId> 46 <artifactId>spring-boot-maven-plugin</artifactId> 47 </plugin> 48 </plugins> 49 </build> 50 51 </project>
2、新建HelloApplicationContextInitializer类,实现ApplicationContextInitializer接口,如下:
1 package com.test.springboot.listener; 2 3 import org.springframework.context.ApplicationContextInitializer; 4 import org.springframework.context.ConfigurableApplicationContext; 5 6 public class HelloApplicationContextInitializer implements ApplicationContextInitializer { 7 @Override 8 public void initialize(ConfigurableApplicationContext applicationContext) { 9 System.out.println("====HelloApplicationContextInitializer======initialize:" + applicationContext); 10 } 11 }
3、将HelloApplicationContextInitializer加入SpringBoot中,在resources目录下新建文件META-INF/spring.factories,添加一下内容
1 # Initializers 2 org.springframework.context.ApplicationContextInitializer=\ 3 com.test.springboot.listener.HelloApplicationContextInitializer
4、启动SpringBoot程序,查看控制台
SpringApplicationRunListener(Spring应用运行监听器)
1、在SpringBoot Web项目中,新建HelloSpringApplicationRunListener类,实现SpringApplicationRunListener接口
1 package com.test.springboot.listener; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.SpringApplicationRunListener; 5 import org.springframework.context.ConfigurableApplicationContext; 6 import org.springframework.core.env.ConfigurableEnvironment; 7 8 public class HelloSpringApplicationRunListener implements SpringApplicationRunListener { 9 10 public HelloSpringApplicationRunListener(SpringApplication application, String[] args) { 11 12 } 13 14 @Override 15 public void starting() { 16 System.out.println("===HelloSpringApplicationRunListener===starting"); 17 } 18 19 @Override 20 public void environmentPrepared(ConfigurableEnvironment environment) { 21 System.out.println("===os.name:" + environment.getSystemProperties().get("os.name") ); 22 System.out.println("===HelloSpringApplicationRunListener===environmentPrepared"); 23 } 24 25 @Override 26 public void contextPrepared(ConfigurableApplicationContext context) { 27 System.out.println("===HelloSpringApplicationRunListener===contextPrepared"); 28 } 29 30 @Override 31 public void contextLoaded(ConfigurableApplicationContext context) { 32 System.out.println("===HelloSpringApplicationRunListener===contextLoaded"); 33 } 34 35 @Override 36 public void started(ConfigurableApplicationContext context) { 37 System.out.println("===HelloSpringApplicationRunListener===started"); 38 } 39 40 @Override 41 public void running(ConfigurableApplicationContext context) { 42 System.out.println("===HelloSpringApplicationRunListener===running"); 43 } 44 45 @Override 46 public void failed(ConfigurableApplicationContext context, Throwable exception) { 47 System.out.println("===HelloSpringApplicationRunListener===failed"); 48 } 49 }
注意:HelloSpringApplicationRunListener必须添加构造方法,否则会包如下错误
HelloSpringApplicationRunListener的构造方法编写,可以参考SpringApplicationRunListener的其他实现类,如:EventPublishingRunListener
1 public class EventPublishingRunListener implements SpringApplicationRunListener, Ordered { 2 3 private final SpringApplication application; 4 5 private final String[] args; 6 7 private final SimpleApplicationEventMulticaster initialMulticaster; 8 9 public EventPublishingRunListener(SpringApplication application, String[] args) { 10 this.application = application; 11 this.args = args; 12 this.initialMulticaster = new SimpleApplicationEventMulticaster(); 13 for (ApplicationListener<?> listener : application.getListeners()) { 14 this.initialMulticaster.addApplicationListener(listener); 15 } 16 } 17 18 ... 19 20 }
2、将HelloSpringApplicationRunListener加入SpringBoot中,在resources目录下新建文件META-INF/spring.factories,添加一下内容
1 # Run Listeners 2 org.springframework.boot.SpringApplicationRunListener=\ 3 com.test.springboot.listener.HelloSpringApplicationRunListener
3、测试启动SpringBoot程序,查看控制台
ApplicationRunner(启动加载类)
1、在SpringBoot Web项目中,新建HelloApplicationRunner类,实现接口 ApplicationRunner
1 @Component 2 public class HelloApplicationRunner implements ApplicationRunner { 3 @Override 4 public void run(ApplicationArguments args) throws Exception { 5 System.out.println("===HelloApplicationRunner===run"); 6 } 7 }
2、需要在HelloApplicationRunner类上加上注解@Component
3、启动SpringBoot项目,查看是否在启动时,调用了run方法
CommandLineRunner(启动加载类)
1、在SpringBoot Web项目中,新建HelloCommandLineRunner类,实现接口 CommandLineRunner
1 @Component 2 public class HelloCommandLineRunner implements CommandLineRunner { 3 @Override 4 public void run(String... args) throws Exception { 5 System.out.println("===HelloCommandLineRunner===run:" + Arrays.asList(args)); 6 } 7 }
2、需要在HelloCommandLineRunner类上加上注解@Component
3、启动SpringBoot项目,查看是否在启动时,调用了run方法
4、同时注入ApplicationRunner和CommandLineRunner,ApplicationRunner先回调,CommandLineRunner再回调