【SpringBoot教程 事件】3.SpringBoot自定义事件和发布订阅机制

前言

前面讲到了SpringBoot 七个生命周期回调事件函数,SpringBoot底层是在jdk中的java.util.EventObject类和java.util.EventListener之上封装实现的发布订阅机制。同时也提供了自定义事件定义,该种方式是一种设计模式–观察者设计模式,优势在于解耦,模块和模块耦合度降低方便扩展。

如下实现一个需求:注册用户,然后插入到数据库,另外还需要给用户发邮件和短信。

编码

1.创建用户注册事件基类

import org.springframework.context.ApplicationEvent;

/**
 * 用户注册事件
 * @author terry
 * @version 1.0
 * @date 2022/6/8 15:37
 */
public class UserRegisterEvent extends ApplicationEvent {

    /**
     * 用户名
     */
    private String name;

    public UserRegisterEvent(Object source) {
        super(source);
    }


    public UserRegisterEvent(Object source, String name) {
        super(source);
        this.name = name;
    }

    public String getUserName() {
        return name;
    }
}

2.用户注册,发布事件例子。

import lombok.extern.java.Log;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.stereotype.Service;

@Service
@Log
public class UserService implements ApplicationEventPublisherAware {

    private ApplicationEventPublisher applicationEventPublisher;

    @Override
    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
        this.applicationEventPublisher = applicationEventPublisher;
    }

    public void register(String userName) {
        log.info("用户注册" + userName + ", 数据插入到数据库!");

        // 发布用户注册事件
        applicationEventPublisher.publishEvent(new UserRegisterEvent(this, userName));
    }
}

3.发送短信,订阅事件例子。

import lombok.extern.java.Log;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Service;

/**
 * 短信service
 * @author terry
 * @version 1.0
 * @date 2022/6/8 15:43
 */
@Service
@Log
public class SmsService implements ApplicationListener<UserRegisterEvent> {

    @Override
    public void onApplicationEvent(UserRegisterEvent event) {
        log.info("发送短信给用户:" + event.getUserName());
    }
}

4.发送邮件,订阅事件例子。

import lombok.extern.java.Log;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Service;

/**
 * 邮件service
 * @author terry
 * @version 1.0
 * @date 2022/6/8 15:43
 */
@Service
@Log
public class EmailService implements ApplicationListener<UserRegisterEvent> {

    @Override
    public void onApplicationEvent(UserRegisterEvent event) {
        log.info("发送邮件给用户:" + event.getUserName());
    }
}

5.测试类,模拟用户注册。

import org.springframework.context.ApplicationEvent;

/**
 * 用户注册事件
 * @author terry
 * @version 1.0
 * @date 2022/6/8 15:37
 */
public class UserRegisterEvent  extends ApplicationEvent {

    /**
     * 用户名
     */
    private String name;

    public UserRegisterEvent(Object source) {
        super(source);
    }


    public UserRegisterEvent(Object source, String name) {
        super(source);
        this.name = name;
    }

    public String getUserName() {
        return name;
    }
}

打印如下:

2022-06-08 15:49:48.196  INFO 27560 --- [           main] com.terry.event.event.UserService        : 用户注册terry, 数据插入到数据库!
2022-06-08 15:49:48.197  INFO 27560 --- [           main] com.terry.event.event.EmailService       : 发送邮件给用户:terry
2022-06-08 15:49:48.198  INFO 27560 --- [           main] com.terry.event.event.SmsService         : 发送短信给用户:terry
  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

terrybg

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值