个人笔记-自定义事件监听与注解开发

本文详细介绍了如何在Spring框架中实现自定义事件监听,包括创建自定义事件、监听器以及在业务层发布事件的过程。同时,还探讨了使用注解简化事件监听的方法,以及如何通过@Async注解实现异步事件处理。

自定义事件监听与注解开发

前提条件

若要实现自定义事件监听,则要满足以下三个条件:
1. Event
2. Listener
3. 发布事件

  • 自定义事件则需要实现ApplicationEvent,代码如下:
package com.weixiaoyi.event;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationEvent;

/**
 * @author :yuanLong Wei
 * @date :Created in 2019-5-16 15:28:42
 * @description:自定义事件
 * @modified By:
 * @version: 1.0
 */
@Slf4j
public class RestEvent extends ApplicationEvent {

    /**
     * Create a new ApplicationEvent.
     *
     * @param source the object on which the event initially occurred (never {@code null})
     */
    public RestEvent(Object source) {
        super(source);
        log.info("RestEvent执行了。。。");
    }

}
  • 自定义的监听器则需要实现ApplicationListener,这里传入的泛型则是自定义的事件类型,代码如下:
package com.weixiaoyi.listener;

import com.weixiaoyi.event.RestEvent;
import com.weixiaoyi.vo.RestVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

/**
 * @author :yuanLong Wei
 * @date :Created in 2019-5-16 15:28:21
 * @description:
 * @modified By:
 * @version: 1.0
 */
@Component
@Slf4j
public class RestListener implements ApplicationListener<RestEvent> {

    @Override
    public void onApplicationEvent(RestEvent event) {
        log.info("自定义  RestListener执行了。。。。 参数为:{}",source);
    }

}
  • service层作为业务处理层,会发布事件:
package com.weixiaoyi.service.impl;

import com.weixiaoyi.dto.SendEventDto;
import com.weixiaoyi.event.RestEvent;
import com.weixiaoyi.service.MyEventService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

/**
 * @author :yuanLong Wei
 * @date :Created in 2019/5/16 15:35
 * @description:发布事件service层实现类
 * @modified By:
 * @version: 1.0
 */
@Service
@Slf4j
public class MyEventServiceImpl implements MyEventService {

    @Resource
    private ApplicationContext applicationContext;

    /**
     * @see com.weixiaoyi.service.MyEventService#sendEvent(String)
     *
     */
    @Override
    public String sendEvent(String sendName) {
        log.info("sendEvent  业务执行中.....");

        // 封装参数实体后 发布事件
        SendEventDto sendEventDto = new SendEventDto(sendName);
        applicationContext.publishEvent(new RestEvent(sendEventDto));

        log.info("sendEvent  业务执行完成.....");
        return "sendName";
    }

}

执行结果为;
在这里插入图片描述

  • 使用注解进行开发

代码如下:

package com.weixiaoyi.listener;

import com.weixiaoyi.event.RestEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

/**
 * @author :yuanLong Wei
 * @date :Created in 2019-5-16 16:09:25
 * @description:注解使用
 * @modified By:
 * @version: 1.0
 */
@Component
@Slf4j
public class SpringStartAnnoListener {

    @EventListener(classes = {RestEvent.class})
    public void listenAnno(RestEvent restVo) {
        log.info("listenAnno excute。。。,参数为:{}", restVo.getSource());
    }

}

执行效果如下:
在这里插入图片描述
至此,全部开发步骤已经完成了,这里采用观察者模式,上述情况为同步方式进行业务处理,想要进行异步方式进行业务处理可以在注释方法上添加@Async:
在这里插入图片描述
这样就不会影响主线程正常返回值。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值