Spring中的事件讲解(Application Event)


1 Spring事件简介

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

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

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

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();
    }
}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM