介绍
类名
org.springframework.context.ApplicationListener
监听器模式
参照 SpringBoot
监听器源码写的 监听天气功能,可跳过忽略
事件对象
// 事件 接口
public abstract class WeatherEvent {
// 事件本身不同的方法
abstract String getWeather();
}
// 两个事件实现
// 下雪事件
public class SnowEvent extends WeatherEvent{
@Override
String getWeather() {
return "下雪";
}
}
// 下雨事件
public class RainEvent extends WeatherEvent{
@Override
String getWeather() {
return "下雨";
}
}
事件监听器
// 监听器接口
public interface WeatherListener {
/**
* 监听到天气时,做出的行为
* @param event
*/
void onWeatherEvent(WeatherEvent event);
}
// 监听器两个实现
// 下雪事件监听器
public class SnowListener implements WeatherListener{
@Override
public void onWeatherEvent(WeatherEvent event) {
if (event instanceof SnowEvent){
System.out.println("执行事件的方法:" + event.getWeather());
}
}
}
// 下雨事件监听器
public class RainListener implements WeatherListener{
@Override
public void onWeatherEvent(WeatherEvent event) {
if (event instanceof RainEvent){
System.out.println("执行事件的方法" + event.getWeather());
}
}
}
事件源
// 事件源接口
public interface EventMulticaster {
void multicastEvent(WeatherEvent event);
void addListener(WeatherListener listener);
void removeListener(WeatherListener listener);
}
//事件源接口实现
public class WeatherEventMulticaster implements EventMulticaster{
@Autowired
private List<WeatherListener> listenerList;
@Override
public void multicastEvent(WeatherEvent event) {
System.out.println("事件开始");
listenerList.forEach(i -> i.onWeatherEvent(event));
System.out.println("事件结束");
}
@Override
public void addListener(WeatherListener listener) {
listenerList.add(listener);
}
@Override
public void removeListener(WeatherListener listener) {
listenerList.remove(listener);
}
}
WeatherRunListener
类比于 SpringApplicationRunListener
之后介绍
public class WeatherRunListener {
@Autowired
private WeatherEventMulticaster weatherEventMulticaster;
public void snow(){
weatherEventMulticaster.multicastEvent(new SnowEvent());
}
public void rain(){
weatherEventMulticaster.multicastEvent(new RainEvent());
}
}
测试类
@Autowired
WeatherRunListener weatherRunListener;
@Test
public void weatherEventMultica(){
weatherRunListener.snow();
weatherRunListener.rain();
}
运行结果
事件开始
调用事件:下雪
事件结束
事件开始
调用事件:下雨
事件结束
SpringBoot监听器
事件
常用事件类

事件加载顺序
项目启动事件加载顺序
### 事件监听器
#### 类名及继承关系

#### 事件源
```java
// 传入事件对象,触发不同的 Listener
public class SimpleApplicationEventMulticaster extends AbstractApplicationEventMulticaster {
public void multicastEvent(final ApplicationEvent event, @Nullable ResolvableType eventType) {
ResolvableType type = (eventType != null ? eventType : resolveDefaultEventType(event));
Executor executor = getTaskExecutor();
for (ApplicationListener<?> listener : getApplicationListeners(event, type)) {
if (executor != null) {
executor.execute(() -> invokeListener(listener, event));
}
else {
invokeListener(listener, event);
}
}
}
}
原理
加载机制
SpringFactoriesLoader
SpringFactoriesLoader
具体实现过程可参考前一篇系统初始化器
,逻辑一致,只是 SpringFactoriesLoader.loadFactoryNames(type, classLoader)
中的 type
由 ApplicationContextInitializer.class
变为ApplicationListener.class
自定义实现
有四种方式来实现自定义的监听器
方式一
1、// 实现 ApplicationListener 触发事件是 ApplicationStartedEvent
public class FirstListener implements ApplicationListener<ApplicationStartedEvent> {
@Override
public void onApplicationEvent(ApplicationStartedEvent event) {
System.out.println("hello first listener");
}
}
2、// spring.factories文件中指定
org.springframework.context.ApplicationListener=com.lanmi17.sb.code.listener.FirstListener
方式二
1、public class SecondListener implements ApplicationListener<ApplicationStartedEvent> {
@Override
public void onApplicationEvent(ApplicationStartedEvent event) {
System.out.println("hello second listener");
}
}
2、// SpringBoot主类中指定
public static void main(String[] args) {
SpringApplication application = new SpringApplication(SbCodeApplication.class);
application.addListeners(new SecondListener());
application.run(args);
}
方式三
1、public class ThirdListener implements ApplicationListener<ApplicationStartedEvent> {
@Override
public void onApplicationEvent(ApplicationStartedEvent event) {
System.out.println("hello third listener");
}
}
2、application.yml 中指定
context:
listener:
classes: com.lanmi17.sb.code.listener.ThirdListener,com.lanmi17.sb.code.listener.FourthListener
方式四
// 实现 SmartApplicationListener 类,可以自定义对哪些 event 感兴趣,触发事件
public class FourthListener implements SmartApplicationListener {
//对哪类事件感兴趣
@Override
public boolean supportsEventType(Class<? extends ApplicationEvent> eventType) {
return ApplicationStartedEvent.class.isAssignableFrom(eventType)
|| ApplicationPreparedEvent.class.isAssignableFrom(eventType);
}
@Override
public void onApplicationEvent(ApplicationEvent event) {
System.out.println("hello fourth lintener");
}
}
网友评论