- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在从事 spring boot 应用程序的事件驱动设计。
代码由以下文件组成:
Spring boot:ApplicationEvent 文件,即 MyBusinessEvent.{java
@Data
@AllArgsConstructor
public class MyBusinessEvent {
private String data;
}
事件发布者文件:MyBusinessService.java
@Slf4j
@Service
public class MyBusinessService {
private final ApplicationEventPublisher applicationEventPublisher;
@Autowired
public MyBusinessService(
ApplicationEventPublisher applicationEventPublisher) {
this.applicationEventPublisher = applicationEventPublisher;
}
@Override
public void save() {
String data = "Testing event data";
MyBusinessEvent event = new MyBusinessEvent(data);
applicationEventPublisher.publishEvent(event);
}
}
事件监听器:MyBusinessEventListener.java
@Slf4j
@Component
public class MyBusinessEventListener {
@EventListener
public void handleEvent(MyBusinessEvent myBusinessEvent) {
log.info("[MyBusinessEventListener] New event received with following data: {}", myBusinessEvent);
}
}
重点是当监听器文件包含 @EventListener
时,应用程序会按预期工作,但当我执行 @TransactionEventListener
时。例如:
事务事件监听器:MyBusinessEventListener.java
@Slf4j
@Component
public class MyBusinessEventListener {
@TransactionalEventListener
public void handleEvent(MyBusinessEvent myBusinessEvent) {
log.info("[MyBusinessEventListener] New event received with following data: {}", myBusinessEvent);
}
}
使用@TransactionalEventListener
,它根本不起作用。
应用程序没有遇到任何异常(甚至在运行时也没有),但没有按预期进行日志记录。
是否缺少任何配置?
最佳答案
将 @Transactional
注释添加到 MyBusinessService.save
应该可以解决这个问题。
根据 java 文档,@TransactionalEventListener
在 @Transactional
边界内工作。
这是文档所说的,
If the event is not published within the boundaries of a managed transaction, the
* event is discarded unless the {@link #fallbackExecution} flag is explicitly set.
如果您不想在事务边界中使用您的服务方法,请使用 @TransactionalEventListener(fallbackExecution = true)
编辑:对于上述问题,存在三种可能的解决方案。
我。将 class
标记为 @Transactional
以被视为事务性
@Transactional
@Service
public class MyBusinessService {
private final ApplicationEventPublisher applicationEventPublisher;
@Autowired
public MyBusinessService(
ApplicationEventPublisher applicationEventPublisher) {
this.applicationEventPublisher = applicationEventPublisher;
}
@Override
public void save() {
String data = "Testing event data";
MyBusinessEvent event = new MyBusinessEvent(data);
applicationEventPublisher.publishEvent(event);
}
}
二。将service的具体方法做成@Transactional
@Override
@Transactional
public void save() {
String data = "Testing event data";
MyBusinessEvent event = new MyBusinessEvent(data);
applicationEventPublisher.publishEvent(event);
}
三。如果您不想在事务边界中使用您的服务方法,请使用
@Slf4j
@Component
public class MyBusinessEventListener {
@TransactionalEventListener(fallbackExecution = true)
public void handleEvent(MyBusinessEvent myBusinessEvent) {
log.info("[MyBusinessEventListener] New event received with following data: {}", myBusinessEvent);
}
}
关于java - @TransactionalEventListener 在@EventListener 像相同上下文的魅力一样工作的地方不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51097916/
我通过将个人电脑连接到智能手机 ARCHOS Diamond 测试了一个 Android 应用程序。使用 Eclipse,从包括 JavaFX 在内的 Java 程序,我可以通过执行安装 Gradle
我是一名优秀的程序员,十分优秀!