可以用来做一个简单的spring的 观察者通知 的 类似于MQ的实现
// pojo
@Data
public class GoodsApplicationEvent extends ApplicationEvent {
private static final long serialVersionUID = -6206752641309458207L;
private String id;
private String tag;
public GoodsApplicationEvent(Object source, String tag, String id) {
super(source);
this.tag = tag;
this.id = id;
}
}
@Autowired
private ApplicationEventPublisher applicationEventPublisher;
// 发送事件
GoodsApplicationEvent goodsApplicationEvent = new GoodsApplicationEvent("测试",message,"1");
applicationEventPublisher.publishEvent(goodsApplicationEvent);
@Component
public class GoodsChangeListener {
// @EventListener
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT) // warning:此注解专门监听@Transcation 事务提交的信息, 不然不会进行监听
public void generatorEsGoodsIndex(GoodsApplicationEvent goodsApplicationEvent) {
System.out.println(goodsApplicationEvent.getTag()+"====收到");
}
}