Spring的事件-代码记录

首先有一个具体的事件

package com.alibaba.demo.event;

import org.springframework.context.ApplicationEvent;

public class HelloEvent extends ApplicationEvent {
 
    private String name;
 
    public HelloEvent(Object source, String name) {
        super(source);
        this.name = name;
    }
 
    public String getName() {
        return name;
    }
}

第二步,记录一下由谁来接手这个事件

package com.alibaba.demo.event;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

@Component
public class HelloEventListener implements ApplicationListener<HelloEvent> {
 
    private static final Logger logger = LoggerFactory.getLogger(HelloEventListener.class);
 
    @Override
    public void onApplicationEvent(HelloEvent event) {
        logger.info("receive {} say hello!",event.getName());
    }
}

第三步,把事件发送出去

package com.alibaba.demo.event;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * @program: parent_pro
 * @description:
 * @author: 渭水
 * @create: 2021/06/23
 */

public class Application {
    public static void main(String[] args) {
        ApplicationContext ctx = new AnnotationConfigApplicationContext(BeanConfig.class);

        HelloEvent event = new HelloEvent("abc","weishui");
        ctx.publishEvent(event);


    }
}

以上为Spring4.2之前的事件分发处理过程,这里面有一些问题要注意

  1. 具体的事件必须继承自ApplicationEvent
  2. 事件的处理类必须继承ApplicationListener<T>,每一种事件处理逻辑都得有一个类。

在Spring4.2之后上面两点有了改变

以下,一个新的具体事件,没有继承任何类

package com.alibaba.demo.event;

public class EmailEvent  {

    private String name;
    private String address;

    public EmailEvent(Object source, String name,String address) {
        this.name = name;
        this.address= address;
    }

    public String getAddress() {
        return address;
    }

    public String getName() {
        return name;
    }
}

事件的处理逻辑如下:

package com.alibaba.demo.event;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

/**
 * @program: parent_pro
 * @description:
 * @author: 渭水
 * @create: 2021/06/23
 */
@Component
public class AnnotationEventListener {

    private static final Logger logger = LoggerFactory.getLogger(AnnotationEventListener.class);

    @EventListener
    public void on(EmailEvent emailEvent){
        logger.info("finished emailevent {}",emailEvent.getName());
    }
}

在具体的处理逻辑方法上加上@EventListener注解即可,方法的参数指明了这个方法具体处理的是那种事件。

上面的事件 监听都是同步的

如果想要变成异步的,可以参考

Spring 中的事件机制 - rickiyang - 博客园

关于SimpleApplicationEventMulticaster

Spring5源码 - 12 Spring事件监听机制_异步事件监听应用及源码解析【附源码】_小小工匠_51CTO博客

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值