spring 事件监听使用@TransactionalEventListener,解决同一个线程中拿不到另一个事务的数据(让之前的事务提交)

定义事件

package com.amarsoft.lease.mail.event;

import org.springframework.context.ApplicationEvent;
public class TestEvent extends ApplicationEvent {
    private String flowId;

    public TestEvent (Object source, String flowId) {
        super(source);
        this.flowId = flowId;
    }

    public String getFlowId() {
        return flowId;
    }

    public void setFlowId(String flowId) {
        this.flowId = flowId;
    }
}

发布事件

applicationContext.publishEvent(new TestEvent(flowId, flowId));

监听事件


@Slf4j
@Component
public class BillSendFlowMailListener {

    @TransactionalEventListener
    public void sendBillSendFlowMail(BillSendFlowMailEvent event) {
        String flowId = event.getSource().toString();
        try {
            //业务逻辑
        }catch (Exception e) {
            log.error("事件测试,flowId" + flowId, e);
        }
    }
}

你可能感兴趣的:(java,spring,java,后端)