Spring自定义发布ApplicationEventPublisher和监听ApplicationEvent事件

Spring提供了事件的发布和订阅,可以使核心业务与子业务进行解耦,也便于后期的业务的扩展。比如用户登录动作,用户登录需要记录登录信息,可增加一个监听事件处理即可,后续可能会增加异地登录通知功能,新增一个监听事件来做就好,不需要修改登录业务。

(1)自定义Event类,继成ApplicationEvent定义事件

(2)调用ApplicationEventPublisher的publishEvent方法发布事件

(3)自定义监听类的方法上加注解@EventListener监听发布的事件

1、 定义事件

import com.xiyunerp.yzbizcenter.k12.app.common.form.cmb.AgreementSuccessForm;
import org.springframework.context.ApplicationEvent;

public class SignSucessEvent extends ApplicationEvent {
    private static final long serialVersionUID = -2681986913481417971L;
    
    /**
     * AgreementSuccessForm类自定义,一般为发布时传递的参数。发布事件调用进行赋值
     * @param source
     */
    public SignSucessEvent(AgreementSuccessForm source) {
        super(source);//
    }
    
    /**
     * 重写父类getSource方法,用于监听事件的取值
     */
    @Override
    public SignSucessEvent getSource() {
        return (SignSucessEvent) super.getSource();
    }
}
import lombok.Data;
import lombok.EqualsAndHashCode;

@EqualsAndHashCode(callSuper = false)
@Data
public class AgreementSuccessForm{
    /**
     * 调用入口,传调用方service名字
     */
    String publishSource;
    /**
     * 账户id
     */
    String accountId;
    /**
     * 会员id
     */
    Long memberId;
    /**
     * 关联账户
     */
    String relationAccount;
    /**
     * 关联类型
     */
    Integer relationType;
    /**
     * 商户id
     */
    Integer merchantId;
}

 

2、事件发布器

import com.xiyunerp.yzbizcenter.k12.app.common.form.cmb.AgreementSuccessForm;

import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

@Service
public class AppAlipayUserService {
    @Resource
    private ApplicationEventPublisher publisher;
    
    public void publishEvent() {
        AgreementSuccessForm form = new AgreementSuccessForm();
        form.setPublishSource(this.getClass().toString());
        form.setAccountId("123");
        form.setMemberId(19235546478221L);
        form.setMerchantId(3456);
        form.setRelationType(AppAccountRelationEntity.RELATION_TYPE_CMB_BANK_UID);
        // 调用事件,发布器进行事件发布
        publisher.publishEvent(new SignSucessEvent(form));
    }
}

3、事件监听器

import com.alibaba.fastjson.JSONObject;
import com.xiyunerp.yzbizcenter.k12.app.common.form.cmb.AgreementSuccessForm;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;


@Component
@Slf4j
public class SignSucessListener {
    
    @Resource
    private AccountService accountService;//业务类,自定义

    /**
     * 事件监听器,监听发布器发布的事件SignSucessEvent
     * @param event
     */
    @EventListener
    public void onSignEvent(SignSucessEvent event) {
        AgreementSuccessForm form = event.getSource();
        log.info("进入签约成功监听,source:{}",JSONObject.toJSONString(form));
        // 自己定义的业务类,可以调用业务类处理自己的业务
        accountService.bindAccountRelation(form.getAccountId(),
                form.getMemberId(),
                form.getMerchantId(),
                form.getRelationAccount(),
                form.getRelationType());
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值