This example describes the event listener mechanism and the use of SpringBoot
There are ApplicationContextInitializer (context initialization device), SpringApplicationRunListener (Spring applications running listener), ApplicationRunner (boot loader class), CommandLineRunner (boot loader class)
About call flow reference [] SpringBoot start SpringBoot principle (xv)
ApplicationContextInitializer (context initializer)
1, a new SpringBoot Web project
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 is </ Dependencies > 39 40 41 is <-! SpringBoot packing plug, the code can be packaged into an executable jar package -> 42 is < Build > 43 is < plugins > 44 is < plugin > 45 < the groupId > org.springframework.boot </ the groupId > 46 is < the artifactId > Spring-Boot-Maven-plugin </ the artifactId > 47 </ plugin > 48 </plugins> 49 </build> 50 51 </project>
2, the new class HelloApplicationContextInitializer achieve ApplicationContextInitializer interfaces, as follows:
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 added SpringBoot, the new file in the resources directory META-INF / spring.factories, add about content
1 # Initializers 2 org.springframework.context.ApplicationContextInitializer=\ 3 com.test.springboot.listener.HelloApplicationContextInitializer
4, start SpringBoot program, view the console
SpringApplicationRunListener (Spring applications running listener)
1, in SpringBoot Web project, the new HelloSpringApplicationRunListener class, implement the interface 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 }
Note: HelloSpringApplicationRunListener must add constructor, otherwise it will package the following error
Prepared HelloSpringApplicationRunListener construction method, reference may be implemented SpringApplicationRunListener other classes, such as: 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 added SpringBoot, the new file in the resources directory META-INF / spring.factories, add about content
1 # Run Listeners 2 org.springframework.boot.SpringApplicationRunListener=\ 3 com.test.springboot.listener.HelloSpringApplicationRunListener
3, start the test program SpringBoot, you view the console
ApplicationRunner (boot loader class)
1, in SpringBoot Web project, the new HelloApplicationRunner class that implements the interface 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, need to annotate @Component on HelloApplicationRunner class
3, start SpringBoot project to see if at startup, called the run method
CommandLineRunner (boot loader class)
1, in SpringBoot Web project, the new HelloCommandLineRunner class that implements the interface 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再回调