spring的事件

时间:2022-07-25
本文章向大家介绍spring的事件,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

简介

通过查看ApplicationContext继承了ApplicationEvent 而ApplicationEvent继续jdk的事件监听, 的实现分别不同的操作,而通过类图发现通过实现ApplicationEvent ,各个子类承担不同的监听工作。

事件监听类图

事件发布者

说明:

ContextRefreshedEvent

事件被发布当ApplicationContext被初始化或者刷新时,它可以由refresh()方法引起在ConfigurableApplicationtext接口中。

ContextStartedEvent

事件被发布当ApplicationContext通过ConfiugrableApplication()接口的start()方法启动时。你可以查询数据库或者你可以重新启动任何停止的应用在收到这个事件后。

ContextStoppedEvent

事件被发布当ApplicationContext通过ConfigurableApplication接口的stop()方法停止时。你可以做必须的看家工作在收到这个事件时。

ContextClosedEvent

事件被发布当ApplicationContext通过ConfigurableApplicationContext接口的close()方法关闭时。一个关闭的上下文到达它声明的终点,不能在被刷新或者重启。

RequestHandledEvent

这是一个web专有的事件,告诉所有beans有一个HTTP请求已经被处理。

接口简介:

ApplicationEvent : 事件,代表一个事情发生了,一个事件对象需要关联(事件源,事件关联的对象);

ApplicationEventPublisher : 发布消息对象,负责发布消息,调度消息的监听器;

ApplicationListener : 负责处理某一类消息;

源码实现

版本号

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.3.11.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.13</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>4.3.11.RELEASE</version>
    </dependency>
</dependencies>
/**
 * @Auther: csh
 * @Date: 2020/7/9 17:11
 * @Description:自定义事件
 */
public class UserEvent extends ApplicationEvent {

    /**
     * Create a new ApplicationEvent.
     *
     * @param source the object on which the event initially occurred (never {@code null})
     */
    public UserEvent(Object source) {
        super(source);
    }

    @Override
    public String toString() {
        return "this my event";
    }
}
/**
 * @Auther: csh
 * @Date: 2020/7/9 17:14
 * @Description:负责处理类UserEvent消息
 */
public class UserEventHandler implements ApplicationListener<UserEvent> {
    public void onApplicationEvent(UserEvent event) {
        System.out.println(event.toString());
    }
}
/**
 * @Auther: csh
 * @Date: 2020/7/9 17:12
 * @Description:负责发布消息
 */
public class UserEventPublisher implements ApplicationEventPublisherAware {
    private ApplicationEventPublisher publisher;
    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
        this.publisher = applicationEventPublisher;
    }

    public void publish(){
        UserEvent userEvent = new UserEvent(this);
        publisher.publishEvent(userEvent);
    }
}
/**
 * @Auther: csh
 * @Date: 2020/7/9 17:15
 * @Description:自定义事件
 */
public class UserEventTest {
    public static void main(String[] args) {
        ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("event/applicationfile-event.xml");
        UserEventPublisher userEventPublisher = context.getBean("userEventPublisher", UserEventPublisher.class);
        userEventPublisher.publish();
        userEventPublisher.publish();
    }
}
this my event
this my event

代码下载:https://gitee.com/hong99/spring/issues/I1N1DF

最后

spring event事件是通过观察模式实现的。参考:设计模式-观察者模式,详细了解的话可以去参考一下相关源码,后续文章再详细写出。