添加Controller时SpringDataRest @RepositoryEventHandler未运行

ken*_*nyg 4 spring-data-rest

我有一个在存储库上运行完美的事件处理程序.然而,一旦我将控制器添加到混合中并直接调用存储库方法,似乎跳过了EventHandler.

有人遇到过这个"问题"吗?如果是这样,我该怎么做才能让事件处理程序再次开始运行?

Mat*_*nkt 6

因此,您希望在使用自定义控制器时调用事件处理程序.我认为这种期望是错误的.当弹簧数据RepositoryEntityController处于控制状态时,就会调用事件处理程序.它不是JPA级别的实体事件侦听器.

你可以做的是手动调用事件处理程序.spring-data-rest RepositoryEventHandler是一个使用普通的spring应用程序事件.因此,您的控制器可以实现ApplicationEventPublisherAware并发布其中一个spring-data-rest应用程序事件.这些都是.的子类org.springframework.data.rest.core.event.RepositoryEvent

applicationEventPublisher.publishEvent(new AfterCreateEvent(myEntity));
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请参阅spring文档.

  • 根据可用的文档,如果新用户的控制器用`@RepositoryRestController` 进行注释,我认为新用户做出这种假设是不合理的。毕竟,它被称为`@RepositoryEventHandler`,而不是`@RepositoryControllerEventHandler` (3认同)