SpringBoot 监听机制
SpringBoot 在项目启动时,会对几个监听器进行回调,我们可以实现这些监听器接口,在项目启动时完成 一些操作
- ApplicationContextInitializer
- SpringApplicationRunListener
- CommandLineRunner
- ApplicationRunner
新建项目工程
创建类
MyApplicationContextInitializer
package com.xiang.listener;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Component;
/**
* Created by IntelliJ IDEA.
* User: xiang
* Date: 2021/10/25 21:05
*/
@Component
public class MyApplicationContextInitializer implements ApplicationContextInitializer {
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
System.out.println("ApplicationContextInitializer.............. initialize()");
}
}
MyApplicationRunner
package com.xiang.listener;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import java.util.Arrays;
/**
* Created by IntelliJ IDEA.
* User: xiang
* Date: 2021/10/25 21:08
*/
@Component
public class MyApplicationRunner implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println("ApplicationRunner.........run");
System.out.println("Arrays.asList(args.getSourceArgs())------->"+Arrays.asList(args.getSourceArgs()));
}
}
MyCommandLineRunner
package com.xiang.listener;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import java.util.Arrays;
/**
* Created by IntelliJ IDEA.
* User: xiang
* Date: 2021/10/25 21:08
*/
@Component
public class MyCommandLineRunner implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println("CommandLineRunner....run");
System.out.println("Arrays.asList(args)--------->"+Arrays.asList(args));
}
}
MySpringApplicationRunListener
package com.xiang.listener;
import org.springframework.boot.ConfigurableBootstrapContext;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringApplicationRunListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.stereotype.Component;
/**
* Created by IntelliJ IDEA.
* User: xiang
* Date: 2021/10/25 21:06
*/
//@Component
public class MySpringApplicationRunListener implements SpringApplicationRunListener {
public MySpringApplicationRunListener(SpringApplication application,String[] args) {
}
/**
* springBoot 启动时会触发不同的事件;
*
* @param bootstrapContext
*/
@Override
public void starting(ConfigurableBootstrapContext bootstrapContext) {
SpringApplicationRunListener.super.starting(bootstrapContext);
System.out.println("starting..... 项目启动中");
}
@Override
public void environmentPrepared(ConfigurableBootstrapContext bootstrapContext, ConfigurableEnvironment environment) {
SpringApplicationRunListener.super.environmentPrepared(bootstrapContext, environment);
System.out.println("environmentPrepared..... 环境对象开始准备");
}
@Override
public void environmentPrepared(ConfigurableEnvironment environment) {
SpringApplicationRunListener.super.environmentPrepared(environment);
System.out.println("environmentPrepared..... 环境对象开始准备.........");
}
@Override
public void contextPrepared(ConfigurableApplicationContext context) {
SpringApplicationRunListener.super.contextPrepared(context);
System.out.println("contextPrepared..... 上下文对象开始准备");
}
@Override
public void contextLoaded(ConfigurableApplicationContext context) {
SpringApplicationRunListener.super.contextLoaded(context);
System.out.println("contextLoaded..... 上下文对象开始加载");
}
@Override
public void started(ConfigurableApplicationContext context) {
SpringApplicationRunListener.super.started(context);
System.out.println("started..... 上下文对象加载完成");
}
@Override
public void running(ConfigurableApplicationContext context) {
SpringApplicationRunListener.super.running(context);
System.out.println("running..... 项目启动完成,开始运行");
}
@Override
public void failed(ConfigurableApplicationContext context, Throwable exception) {
SpringApplicationRunListener.super.failed(context, exception);
System.out.println("failed..... 项目启动失败");
}
}
新建META-INF/spring.factories
org.springframework.context.ApplicationContextInitializer=
com.xiang.listener.MyApplicationContextInitializer
org.springframework.boot.SpringApplicationRunListener=
com.xiang.listener.MySpringApplicationRunListener
运行主程序 测试 SpringBootListenerApplication
package com.xiang;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootListenerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootListenerApplication.class, args);
/**
* 控制台只走了 两个
* ApplicationRunner.........run
* CommandLineRunner....run
*/
}
}
运行结果
starting..... 项目启动中
environmentPrepared..... 环境对象开始准备.........
environmentPrepared..... 环境对象开始准备
. ____ _ __ _ _
/\ / ___'_ __ _ _(_)_ __ __ _
( ( )\___ | '_ | '_| | '_ / _` |
\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.5.6)
ApplicationContextInitializer.............. initialize()
contextPrepared..... 上下文对象开始准备
2021-10-25 22:39:56.188 INFO 15652 --- [ main] com.xiang.SpringBootListenerApplication : Starting SpringBootListenerApplication using Java 1.8.0_281 on DESKTOP-6K7C1EJ with PID 15652 (D:ComprehensiveTraining rainingspringBootDemospring-boot-listener argetclasses started by Xiang in D:ComprehensiveTraining rainingspringBootDemo)
2021-10-25 22:39:56.193 INFO 15652 --- [ main] com.xiang.SpringBootListenerApplication : No active profile set, falling back to default profiles: default
contextLoaded..... 上下文对象开始加载
2021-10-25 22:39:57.209 INFO 15652 --- [ main] com.xiang.SpringBootListenerApplication : Started SpringBootListenerApplication in 1.842 seconds (JVM running for 3.343)
started..... 上下文对象加载完成
ApplicationRunner.........run
Arrays.asList(args.getSourceArgs())------->[]
CommandLineRunner....run
Arrays.asList(args)--------->[]
running..... 项目启动完成,开始运行
Process finished with exit code 0