SpringBoot的多种事件监听机制
前置配置
# META-INF/spring.factories文件配置 # ApplicationContextInitializer org.springframework.context.ApplicationContextInitializer=com.test.springbootdemo.eventListeners.MyApplicationContextInitializer # ApplicationListener org.springframework.context.ApplicationListener=com.test.springbootdemo.eventListeners.MyApplicationListener # SpringApplicationRunListener org.springframework.boot.SpringApplicationRunListener=com.test.springbootdemo.eventListeners.MySpringApplicationRunListener
ApplicationContextInitializer接口
代码示例
public class MyApplicationContextInitializer implements ApplicationContextInitializer { //run方法this.prepareContext触发 //进一步则是this.applyInitializers //在进一步则是initializer.initialize(context); @Override public void initialize(ConfigurableApplicationContext applicationContext) { System.out.println("ApplicationContextInitializer.....initialize"); } }
说明
1.由于触发阶段在于准备容器的时候,所以添加@Component注解是不会起作用的,应为@Component注解需要在容器刷新时候才会起到作用。
ApplicationListener接口
代码示例
public class MyApplicationListener implements ApplicationListener { //run方法listeners.starting,触发第一次 //run方法this.prepareEnvironment,触发第二次 //即进一步,listeners.environmentPrepared //run方法this.prepareContext,触发第三次与触发第四次 //进一步则是listeners.contextPrepared(context);触发第三次 // listeners.contextLoaded(context);触发第四次 @Override public void onApplicationEvent(ApplicationEvent event) { if (event instanceof ContextStartedEvent){ System.out.println("ContextStartedEvent1.....run()"); } System.out.println("Event1.....run()"); } } @Component public class MyApplicationListener2 implements ApplicationListener { //容器refresh()的时候触发 //进一步finishRefresh(); //再进一步getLifecycleProcessor().onRefresh(); @Override public void onApplicationEvent(ApplicationEvent event) { if (event instanceof ContextStartedEvent){ System.out.println("ContextStartedEvent2.....run()"); } System.out.println("Event2.....run()"); } }
说明
1.存在两种注册方式:
1)@Component注解注册,这种需要在容器刷新后的finishRefresh()方法里面触发,然后监听到全部事件
2)配置文件注册,这种会在SpringBoot中开启listeners,让listeners准备环境,准备容器等都会触发。包括容器刷新后的finishRefresh()方法里面都会触发【包括了注解修饰的部分】
ApplicationRunner接口
代码示例
@Component public class MyApplicationRunner implements ApplicationRunner { //run方法this.callRunners,第一次触发 //进一步,this.callRunner //在进一步,runner.run(args); @Override public void run(ApplicationArguments args) throws Exception { System.out.println("ApplicationRunner.....run()"); } }
说明
1.这种只能在容器启动后进行触发
2.与CommandLineRunner接口其实并没有很大差别,都在同一个方法内被调用,调用同一个触发方法。但是会优于CommandLineRunner被调用。
CommandLineRunner接口
代码示例
@Component public class MyCommandLineRunner implements CommandLineRunner { //run方法this.callRunners,第一次触发 //进一步,this.callRunner //在进一步,runner.run(args); @Override public void run(String... args) throws Exception { System.out.println("CommandLineRunner.....run()"); } }
说明
1.这种只能在容器启动后进行触发
2.与ApplicationRunner接口其实并没有很大差别,都在同一个方法内被调用,调用同一个触发方法。但是会晚于ApplicationRunner被调用。
SpringApplicationRunListener接口
代码示例
public class MySpringApplicationRunListener implements SpringApplicationRunListener { public MySpringApplicationRunListener(SpringApplication springApplication, String[] arg) { } //run方法listeners.starting触发 @Override public void starting(ConfigurableBootstrapContext bootstrapContext) { System.out.println("SpringApplicationRunListener.....starting()"); } //run方法this.prepareEnvironment触发 //进一步listeners.environmentPrepared @Override public void environmentPrepared(ConfigurableBootstrapContext bootstrapContext, ConfigurableEnvironment environment) { System.out.println("SpringApplicationRunListener.....environmentPrepared()"); } //run方法this.prepareContext触发 //进一步listeners.contextPrepared(context); @Override public void contextPrepared(ConfigurableApplicationContext context) { System.out.println("SpringApplicationRunListener.....contextPrepared()"); } //run方法this.prepareContext触发 //进一步listeners.contextLoaded(context); @Override public void contextLoaded(ConfigurableApplicationContext context) { System.out.println("SpringApplicationRunListener.....contextLoaded()"); } //run方法listeners.started(context, timeTakenToStartup);触发 @Override public void started(ConfigurableApplicationContext context, Duration timeTaken) { System.out.println("SpringApplicationRunListener.....started()"); } @Override public void started(ConfigurableApplicationContext context) { System.out.println("SpringApplicationRunListener.....started()"); } //run方法listeners.ready(context, timeTakenToReady);触发 @Override public void ready(ConfigurableApplicationContext context, Duration timeTaken) { System.out.println("SpringApplicationRunListener.....ready()"); } @Override public void running(ConfigurableApplicationContext context) { System.out.println("SpringApplicationRunListener.....running()"); } @Override public void failed(ConfigurableApplicationContext context, Throwable exception) { System.out.println("SpringApplicationRunListener.....failed()"); } }
说明
1.这个比较有意思在容器Refresh()前后都会触发,包括各种处理环境,准备容器等步骤。