@EventListener监听注解的使用(ApplicationEventPublisher)

controller,可以使用ApplicationContext 注入因为继承了ApplicationEventPublisher,也可以直接使用ApplicationEventPublisher注入

package com.example.poi.controller;

import com.example.poi.entity.RegisterSuccessEvent;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Author xu
 * @create 2023/7/5 01
 */

@RestController
@RequestMapping("/customDemo")
public class DemoTwoController {
    
    @Resource
    private ApplicationContext applicationContext;

//    @Resource
//    private ApplicationEventPublisher applicationEventPublisher;;

    @GetMapping("/publishEvent")
    public void publishEvent() {
        applicationContext.publishEvent(new RegisterSuccessEvent("张三"));
        System.out.println("******");
    }
}

封装事件相关的对象,不继承ApplicationEvent

package com.example.poi.entity;

import lombok.Data;

/**
 * @Author xu
 * @create 2023/7/19 16
 */
@Data
public class RegisterSuccessEvent {

    private String userName;

    public RegisterSuccessEvent(String userName) {
        this.userName = userName;
    }
}

封装事件相关的对象,继承ApplicationEvent

package com.example.poi.entity;

import lombok.Data;

/**
 * @Author xu
 * @create 2023/7/19 16
 */
@Data
public class RegisterSuccessEvent extends ApplicationEvent{

    private String userName;

    public RegisterSuccessEvent(String source) {
        super(source);
        this.userName = source;
    }
}

继承 ApplicationEvent 的情况:
如果 EsSaveEvent 类继承自 ApplicationEvent 类,那么它就是一个标准的 Spring 应用程序事件。这意味着它可以被 Spring 应用程序上下文中的事件监听器监听到,并且可以通过 Spring 的事件发布器进行发布。这样做的好处是,你可以利用 Spring 框架提供的事件机制来实现模块之间的解耦,并且可以方便地在不同模块之间传递事件相关的数据。
不继承 ApplicationEvent 的情况:
如果 EsSaveEvent 类不继承自 ApplicationEvent 类,那么它并不是一个标准的 Spring 应用程序事件。在这种情况下,你仍然可以通过自定义的方式实现事件的发布和监听,但需要注意的是,这种方式不会与 Spring 框架的事件机制直接相关。你可能需要自己实现事件的发布和监听逻辑,或者利用其他框架或工具来实现类似的功能。
总的来说,继承 ApplicationEvent 的方式更符合 Spring 框架的设计理念,可以更好地利用 Spring 提供的事件机制来实现模块之间的解耦。而不继承 ApplicationEvent 的方式则需要自己实现事件的发布和监听逻辑,可能会增加一些额外的工作量

事件的监听逻辑使用注解@EventListener

package com.example.poi.config;

import com.example.poi.entity.RegisterSuccessEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

/**
 * @Author xu
 * @create 2023/7/19 16
 */
@Slf4j
@Component
public class RegisterEventListener {

    @EventListener
    public void handleNotifyEvent(RegisterSuccessEvent event) {
        log.info("监听到用户:"+ event.getUserName()+"注册成功事件");
    }

}

事件的监听逻辑使用实现接口ApplicationListener

package com.example.poi.config;

import com.example.poi.entity.RegisterSuccessEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.context.ApplicationListener;

/**
 * @Author xu
 * @create 2023/7/19 16
 */
@Slf4j
@Component
public class RegisterEventListener implements ApplicationListener<RegisterSuccessEvent >{

    public void handleNotifyEvent(RegisterSuccessEvent event) {
        log.info("监听到用户:"+ event.getUserName()+"注册成功事件");
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值