spring的事件

目录

简介

事件监听类图

 事件发布者

说明: 

接口简介:

源码实现

 结果

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

最后


简介

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值