前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >spring的事件

spring的事件

作者头像
逍遥壮士
发布2020-09-18 11:35:44
4160
发布2020-09-18 11:35:44
举报
文章被收录于专栏:技术趋势技术趋势

简介

通过查看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事件是通过观察模式实现的。参考:设计模式-观察者模式,详细了解的话可以去参考一下相关源码,后续文章再详细写出。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-07-11,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 技术趋势 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档