Spring监听器

https://blog.csdn.net/JokerLJG/article/details/121541262
应用程序事件允许我们发送和接收特定事件,我们可以根据需要处理这些事件。事件用于在松散耦合的组件之间交换信息。由于发布者和订阅者之间没有直接耦合,因此可以在不影响发布者的情况下修改订阅者,反之亦然。

1. 事件监听三要素

  1. 事件源:事件对象的产生者,任何一个事件都有一个来源
  2. 事件监听器:事件框架或组件收到一个事件后,需要通知所有相关的事件监听器来进行处理。这些监听器统一存储在事件监听器注册表中。
  3. 发布事件:ApplicationContext(spring 容器)

2. JDK 事件

Java 中,通过 java. util. EventObject 来描述事件,通过 java. util. EventListener 来描述事件监听器。

3. Spring 事件

  1. 事件源:ApplicationEvent
  2. 事件监听器:ApplicationListener(编码式事件监听器)、@EventListener(注解式事件监听器)
  3. 发布事件:ApplicationContext(spring容器)

ApplicationContext 事件机制是观察者设计模式的实现,通过 ApplicationEvent 类和 ApplicationListener 接口,可以实现 ApplicationContext 事件处理。
如果容器中有一个 ApplicationListener Bean,每当 ApplicationContext 发布 ApplicationEvent 时,ApplicationListener Bean 将自动被触发。这种事件机制都必须需要程序显示的触发。
spring 有一些内置的事件,当完成某种操作时会发出某些事件动作。比如监听 ContextRefreshedEvent 事件,当所有的 bean 都初始化完成并被成功装载后会触发该事件,实现 ApplicationListener 接口可以收到监听动作,然后可以写自己的逻辑。

1. 事件:ApplicationEvent

ApplicationEvent 是 Spring 事件的顶层抽象类,代表事件本身,继承自 JDK 的 EventObject。Spring 提供了一些内置事件,如 ContextClosedEvent、ContextStartedEvent、ContextRefreshedEvent、ContextStopedEvent 等
Pasted image 20230322183324.png

2. 事件监听器:ApplicationListener

ApplicationListener 是 Spring 事件监听器顶层接口,所有的监听器都实现该接口,继承自 JDK 的 EventListener。Spring 提供了一些易扩展的接口,如 SmartApplicationListener、GenericApplicationListener

3. 事件发布:ApplicationEventPublisher

ApplicationEventPublisher 是 Spring 的事件发布接口,事件源通过该接口的 publishEvent 方法发布事件,所有的应用上下文都具备事件发布能力,因为 ApplicationContext 继承了该接口

4. 事件广播器:ApplicationEventMulticaster

ApplicationEventMulticaster 是 Spring 事件机制中的事件广播器,它默认提供一个 SimpleApplicationEventMulticaster 实现,如果用户没有自定义广播器,则使用默认的(初始化逻辑见 AbstractApplicationContext 的 refresh 方法)。它通过父类 AbstractApplicationEventMulticaster 的 getApplicationListeners 方法从事件注册表中获取事件监听器,并且通过 invokeListener 方法执行监听器的具体逻辑。
默认的广播器是同步调用监听器的执行逻辑,但是可以通过为广播器配置 Executor 实现监听器的异步执行。
在 Spring 中通常是 ApplicationContext 本身担任监听器注册表的角色,在其子类 AbstractApplicationContext 中就聚合了事件广播器 ApplicationEventMulticaster 和事件监听器 ApplicationListnener,并且提供注册监听器的 addApplicationListnener 方法。
当一个事件源产生事件时,它通过事件发布器 ApplicationEventPublisher 发布事件,然后事件广播器 ApplicationEventMulticaster 会去事件注册表 ApplicationContext 中找到事件监听器 ApplicationListnener,并且逐个执行监听器的 onApplicationEvent 方法,从而完成事件监听器的逻辑。

4. SpringBoot 事件

1. 事件:SpringApplicationEvent

SpringApplicationEvent 是 SpringBoot 事件的顶层抽象类,且 SpringBoot 内置了 7 个事件:

2. 自定义事件监听

自定义事件监听一般包括事件源和、事件监听器

1.自定义事件源(Event):

import org.springframework.context.ApplicationEvent;

public class TestEvent extends ApplicationEvent {

    public TestEvent(Object obj) {
        super(obj);
    }
}


2. 自定义事件监听器(Listener)

1.自定义监听器类+@Component

@Component
public class TestListener implements ApplicationListener<TestEvent> {
    @Override
    public void onApplicationEvent(TestEvent event) {
	    System.out.println("监听到事件发生:"+event.getClass());	
    }
}

2. 自定义监听器类+addListeners ()

public class TestListener implements ApplicationListener<TestEvent> {
    @Override
    public void onApplicationEvent(TestEvent event) {
	    System.out.println("监听到事件发生:"+event.getClass());
    }
}

@SpringBootApplication
public class ScheduleApplication {
    public static void main(String[] args) {
        SpringApplication springApplication = new SpringApplication(ScheduleApplication.class);
        springApplication.addListeners(new TestListener());
        springApplication.run(args);
    }
}

3. 自定义监听器类+配置文件

public class TestListener implements ApplicationListener<TestEvent> {
    @Override
    public void onApplicationEvent(TestEvent event) {
	    System.out.println("监听到事件发生:"+event.getClass());
    }
}

context:
  listener:
    classes: com.test.TestListener

4.@EventListener +@Component

@Component
public class TestListener {
    @EventListener
    public void eventListener(TestEvent event) {
        System.out.println("监听到事件发生:"+event.getClass());
    }
}

3. 发布事件测试

@SpringBootApplication
public class ScheduleApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(ScheduleApplication.class, args);
        context.publishEvent(new TestEvent(1));
    }
}


3. ApplicationListener 接口和 SmartApplicationListener 接口区别

  • 实现 ApplicationListener 接口针对单一事件监听
  • 实现 SmartApplicationListener 接口针对多种事件监听

SmartApplicationListener 用法举例:

@Component
public class TestListener implements SmartApplicationListener {
    @Override
    public boolean supportsEventType(Class<? extends ApplicationEvent> eventType) {
        boolean b = TestEvent.class.isAssignableFrom(eventType) || Test2Event.class.isAssignableFrom(eventType);
        return b;
    }

    @Override
    public void onApplicationEvent(ApplicationEvent event) {
        System.out.println("监听到事件发生:"+event.getClass());

    }
}


测试用例

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class ListenerTest {

    @Autowired
    private ApplicationEventPublisher applicationEventPublisher;

    @Test
    public void test() {
        TestEvent testEvent = new TestEvent("hello word");
        applicationEventPublisher.publishEvent(testEvent);
    }
}


4. 常用监听器 接口

1. ServletContextListener

监听servletContext对象的创建以及销毁
执行时机:

  1. contextInitialized (ServletContextEvent arg0) – 创建时执行
  2. contextDestroyed (ServletContextEvent arg0) – 销毁时执行

2. ServletRequestListener

监听 request 对象的创建以及销毁
执行时机:

  1. requestInitialized(ServletRequestEvent sre) – 创建时执行
  2. requestDestroyed (ServletRequestEvent sre) – 销毁时执行

3. HttpSessionListener

监听 session 对象的创建以及销毁
执行时机:

  1. sessionCreated(HttpSessionEvent se) – 创建时执行
  2. sessionDestroyed (HttpSessionEvent se) – 销毁时执行

4. ServletContextAttributeListener

监听 servletContext 对象中属性的改变
执行时机:

  1. attributeAdded (ServletContextAttributeEvent event) – 添加属性时执行
  2. attributeReplaced (ServletContextAttributeEvent event) – 修改属性时执行
  3. attributeRemoved (ServletContextAttributeEvent event) – 删除属性时执行

5. ServletRequestAttributeListener

监听 request 对象中属性的改变

  1. attributeAdded (ServletRequestAttributeEvent srae) – 添加属性时执行
  2. attributeReplaced (ServletRequestAttributeEvent srae) – 修改属性时执行
  3. attributeRemoved (ServletRequestAttributeEvent srae) – 删除属性时执行

6. HttpSessionAttributeListener

监听 session 对象中属性的改变

  1. attributeAdded (HttpSessionBindingEvent event) – 添加属性时执行
  2. attributeReplaced (HttpSessionBindingEvent event) – 修改属性时执行
  3. attributeRemoved (HttpSessionBindingEvent event) – 删除属性时执行

5. 事件监听模型

Pasted image 20230322230858.png


Spring监听器
http://localhost:8080/archives/36fc202c-bab9-4aa0-b9d9-bd442e1ee92f
作者
Administrator
发布于
2024年06月11日
许可协议