首页 > 技术文章 > Spring中的事件讲解(Application Event)

jingzh 2020-12-28 09:44 原文

1 Spring事件

1.1 简介

Spring的事件(Application Event)为BeanBean之间的消息同步提供了支持。当一个Bean处理完成一个任务之后,希望另外一个Bean知道并能做相应的处理,这时我们就需要让另外一个Bean监听当前Bean所发生的事件

Spring的事件需要遵循如下流程:

  • 自定义事件,继承ApplicationEvent
  • 定义事件监听器,实现ApplicationListener
  • 使用容器发布事件

1.2 Spring框架中事件

Spring提供了以下5种标准的事件:

  • 上下文更新事件(ContextRefreshedEvent):在调用ConfigurableApplicationContext 接口中的refresh()方法时被触发。
  • 上下文开始事件(ContextStartedEvent):当容器调用ConfigurableApplicationContextStart()方法开始/重新开始容器时触发该事件。
  • 上下文停止事件(ContextStoppedEvent):当容器调用ConfigurableApplicationContextStop()方法停止容器时触发该事件。
  • 上下文关闭事件(ContextClosedEvent):当ApplicationContext被关闭时触发该事件。容器被关闭时,其管理的所有单例Bean都被销毁。
  • 请求处理事件(RequestHandledEvent):在Web应用中,当一个http请求(request)结束触发该事件。

如果一个bean实现了ApplicationListener接口,当一个ApplicationEvent被发布以后,bean会自动被通知

2 Demo示例

2.1 pom.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>cn.jzh</groupId>
    <artifactId>TestDemo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.9.RELEASE</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>
</project>

2.2 自定义事件

package cn.jzh.event;

import org.springframework.context.ApplicationEvent;

/**
 * 自定义的spring事件
 */

public class DemoEvent extends ApplicationEvent {

    private String msg;

    public DemoEvent(Object source,String msg) {
        super(source);
        this.msg = msg;
    }

    public String getMsg() {
        return msg;
    }
    public void setMsg(String msg) {
        this.msg = msg;
    }
}

2.3 事件监听器

package cn.jzh.event;

import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

/**
 * 监听事件的实现类
 */
@Component
public class DemoListener implements ApplicationListener<DemoEvent> {//实现ApplicationListener接口,并指定监听的事件类型
    @Override
    public void onApplicationEvent(DemoEvent event) {//使用onApplicationEvent方法对消息进行接受处理
        String msg = event.getMsg();
        System.out.println("DemoListener获取到了监听消息:"+msg);

    }
}

2.4 事件发布类

package cn.jzh.event;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

/**
 * 发布事件
 */
@Component
public class DemoPublisher  {
    @Autowired
    private ApplicationContext applicationContext;//注入ApplicationContext用来发布事件

    public void publish(String msg){
        applicationContext.publishEvent(new DemoEvent(this,msg));//使用ApplicationContext对象的publishEvent发布事件
    }
}

2.5 配置类

配置类中没有具体的代码逻辑注意作用是为了能扫描到相应的使用注解的类

package cn.jzh.event;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

@Configuration
@ComponentScan("cn.jzh.event")
public class EventConfig {

}

2.6 启动测试

package cn.jzh.event;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class App {
    public static void main(String[] args) {
        //使用AnnotationConfigApplicationContext读取配置EventConfig类,EventConfig类读取了使用注解的地方
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(EventConfig.class);

        DemoPublisher publish = context.getBean(DemoPublisher.class);
        publish.publish("你好");
        context.close();
    }
}

推荐阅读