JDK的监听 Spring的事件监听机制

一。概述
使用场景:用户注册完成时,需要给该用户发送邮件、发送优惠劵等等操作,实现业务的解耦(MQ的异步 销峰  解耦)

大体步骤
 1.UserService 在完成自身的用户注册逻辑之后,仅仅只需要发布一个 UserRegisterEvent 事件,而无需关注其它拓展逻辑。
 2.其它 Service 可以自己订阅 UserRegisterEvent 事件,实现自定义的拓展逻辑

友情提示:很多时候,我们会把观察者模式和发布订阅模式放在一起对比
	简单来说,发布订阅模式属于广义上的观察者模式,在观察者模式的 Subject 和 Observer 的基础上,引入 Event Channel 这个中介,进一步解耦


二。Spring 事件机制
	Spring 基于观察者模式,实现了自身的事件机制,由三部分组成      
		a.事件发布者                      实现ApplicationEventPublisherAware            对应上面的UserService 
		b.事件                           继承ApplicationEvent                          对应上面的UserRegisterEvent 
		c.事件监听器 ApplicationListener  实现ApplicationListener<UserRegisterEvent>    对应上面的其他Service 
	
 友情提示:JDK 也内置了事件机制的实现,考虑到通用性,Spring 的事件机制是基于它之上进行拓展。 		
		ApplicationEvent    继承自 java.util.EventObject
		ApplicationListener 继承自 java.util.EventListener
		
	 我们写的代码是继承spring的 ApplicationEventPublisherAware  
	 						 ApplicationEvent    
	 					     ApplicationListener 

三。代码案例
 1.事件类
 2.事件发布者
 3.事件监听者
1.事件类
/**
 * @author LuHuanCheng
 * 类说明:事件类
 */
public class UserRegisterEvent extends ApplicationEvent {
    /**
     * 用户名
     */
    private String username;

    /**
     * 构造函数1
     *
     * @param source 参数1
     */
    public UserRegisterEvent(Object source) {
        super(source);
    }

    /**
     * 构造函数2
     *
     * @param source   参数1
     * @param username 参数2
     */
    public UserRegisterEvent(Object source, String username) {
        super(source);
        this.username = username;
    }

    /**
     * get方法
     *
     * @return 用户名
     */
    public String getUsername() {
        return username;
    }

    /**
     * set方法
     *
     * @param username 用户名
     */
    public void setUsername(String username) {
        this.username = username;
    }
}
2.事件发布者
@Service
public class UserService implements ApplicationEventPublisherAware {

    private ApplicationEventPublisher applicationEventPublisher;

    public void registerUser(String username) {
        //1.注册用户正常逻辑
        System.out.println("用户表添加一个用户");

        //2.发布事件
        applicationEventPublisher.publishEvent(new UserRegisterEvent(this, username));
    }

    @Override
    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
        this.applicationEventPublisher = applicationEventPublisher;
    }
}
3.事件监听器

/**
 * @author LuHuanCheng
 * 类说明:优惠券监听类
 */
@Service
public class CouponService implements ApplicationListener<UserRegisterEvent> {
    @Override
    public void onApplicationEvent(UserRegisterEvent userRegisterEvent) {
        System.out.println("优惠券监听到用户注册事件");
    }  
}

/**
 * @author LuHuanCheng
 * 类说明:邮箱监听类
 */
@Service
public class EmailService implements ApplicationListener<UserRegisterEvent> {
    @Override
    public void onApplicationEvent(UserRegisterEvent userRegisterEvent) {
        System.out.println("邮箱监听到用户注册事件");
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

飘然生

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

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

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

打赏作者

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

抵扣说明:

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

余额充值