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());
    }
}

 

你可能感兴趣的:(SpringBoot,SpringMVC)