在springboot官网中,有一段关于Application Events and Listeners(应用中事件和监听器)的描述:
In addition to the usual Spring Framework events, such as ContextRefreshedEvent, a SpringApplication sends some additional application events.
(平时往Spring Framework events添加普通的事件,像ContextRefreshedEvent和SpringApplication也提供了一些额外的事件)
Some events are actually triggered before the ApplicationContext is created, so you cannot register a listener on those as a @Bean. You can register them with the SpringApplication.addListeners(…) method or the SpringApplicationBuilder.listeners(…) method.
(有一些事件时在ApplicationContext被创建之前就执行了,所以你不能像用@Bean的形式注册这些事件,但是呢,你可以通过SpringApplication.addListeners(…)和SpringApplicationBuilder.listeners(…)方法进行注册)
If you want those listeners to be registered automatically, regardless of the way the application is created, you can add a META-INF/spring.factories file to your project and reference your listener(s) by using the org.springframework.context.ApplicationListener key, as shown in the following example:
(如果你想你的监听自动注册,你可以添加META-INF/spring.factories这样一个文件,然后使用配置文件关联这些监听,例如:)
org.springframework.context.ApplicationListener=com.example.project.MyListener
以下是所支持的事件列表以及加载顺序:
Application events are sent in the following order, as your application runs:
应用事件遵循一下规律,按照应用启动顺序:
An ApplicationStartingEvent is sent at the start of a run but before any processing, except for the registration of listeners and initializers.
ApplicationStartingEvent 这个事件在应用启动,但未处理任何请求之前执行,除了监听器和初始化监听之外
An ApplicationEnvironmentPreparedEvent is sent when the Environment to be used in the context is known but before the context is created.
ApplicationEnvironmentPreparedEvent 这个事件是在已知当前上下文环境是,但是还没有创建上下文之前运行
An ApplicationContextInitializedEvent is sent when the ApplicationContext is prepared and ApplicationContextInitializers have been called but before any bean definitions are loaded.
ApplicationContextInitializedEvent 事件是在应用上下文准备好,并且应用上下文初始化,但是没有定义任何对象加载的时候执行
An ApplicationPreparedEvent is sent just before the refresh is started but after bean definitions have been loaded.
ApplicationPreparedEvent 事件是在定义bean加载后刷新前方法开始之前执行
An ApplicationStartedEvent is sent after the context has been refreshed but before any application and command-line runners have been called.
ApplicationStartedEvent 在上下文刷新之后但在调用任何应用程序和命令行运行程序之前执行
An AvailabilityChangeEvent is sent right after with LivenessState.CORRECT to indicate that the application is considered as live.
AvailabilityChangeEvent 在LivenessState.CORRECT之后被执行,用来表明应用的可用性
An ApplicationReadyEvent is sent after any application and command-line runners have been called.
ApplicationReadyEvent在任何应用程序和命令执行后被调用
An AvailabilityChangeEvent is sent right after with ReadinessState.ACCEPTING_TRAFFIC to indicate that the application is ready to service requests.
AvailabilityChangeEvent在ReadinessState.ACCEPTING_TRAFFIC之后,用来表明应用可以服务请求了
An ApplicationFailedEvent is sent if there is an exception on startup.
ApplicationFailedEvent 在启动中,有异常发生时被调用
随便拿了一个,试试看
创建类:MyApplicationStartingEventListener 继承了ApplicationStartingEvent
在构造方法中,添加了一个打印
import org.springframework.boot.ConfigurableBootstrapContext;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.event.ApplicationStartingEvent;
/**
* @author lixl
* @description
* @date 2022/1/29
*/
public class MyApplicationStartingEventListener extends ApplicationStartingEvent {
public MyApplicationStartingEventListener(SpringApplication application, String[] args) {
super(application, args);
System.out.println("123123");
}
public MyApplicationStartingEventListener(ConfigurableBootstrapContext bootstrapContext, SpringApplication application, String[] args) {
super(bootstrapContext, application, args);
}
}
启动执行
import com.lixl.webcontext.listener.MyApplicationStartingEventListener;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
/**
* @author lixl
* @description
* @date 2022/1/28
*/
@EnableAutoConfiguration
@ComponentScan(basePackages = {"com.lixl.webcontext","com.lixl.config"})
public class MyApplication {
public static void main(String[] args) {
SpringApplication application = new SpringApplication(MyApplication.class);
// 自定义的事件
MyApplicationStartingEventListener myApplicationStartingEventListener = new MyApplicationStartingEventListener(application, args);
//application.addListeners(myApplicationStartingEventListener);
application.run(args);
}
}
很早就打印出来了
再看一下监听器,创建监听器需要实现ApplicationListener 接口
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
/**
* @author lixl
* @description
* @date 2022/1/29
*/
public class MyListener implements ApplicationListener {
@Override
public void onApplicationEvent(ApplicationEvent event) {
System.out.println("这是我的监听器");
System.out.println("监听的事件是:"+event.getSource());
}
}
import com.lixl.webcontext.events.MyApplicationStartingEventListener;
import com.lixl.webcontext.listener.MyListener;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
/**
* @author lixl
* @description
* @date 2022/1/28
*/
@EnableAutoConfiguration
@ComponentScan(basePackages = {"com.lixl.webcontext","com.lixl.config"})
public class MyApplication {
public static void main(String[] args) {
SpringApplication application = new SpringApplication(MyApplication.class);
MyApplicationStartingEventListener myApplicationStartingEventListener = new MyApplicationStartingEventListener(application, args);
// 在spring中,添加我的监听器
application.addListeners(new MyListener());
application.run(args);
}
}
下边使用配置文件,指定监听器位置
在resources文件夹下,创建META-INF/spring.factories文件,并指定配置参数
org.springframework.context.ApplicationListener=com.lixl.webcontext.listener.Listener2,com.lixl.webcontext.listener.Listener3(多个监听器,用逗号隔开)
效果如下: