添加控制器时,SpringDataRest @RepositoryEventHandler 未运行 [英] SpringDataRest @RepositoryEventHandler not running when Controller is added

查看:23
本文介绍了添加控制器时,SpringDataRest @RepositoryEventHandler 未运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an event handler that runs perfectly fine on a repository. However once I add a controller into the mix and call the repository method directly, the EventHandler seems to be skipped over.

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

Has anyone encountered this "issue"? If so, what can I do to get the event handler to start running again?

推荐答案

因此,您希望在使用自定义控制器时调用事件处理程序.我认为这种期望是错误的.事件处理程序仅在 spring 数据静止 RepositoryEntityController 处于控制之下时被调用.它不是 JPA 级别的实体事件侦听器.

So you expect that your event handler is called when you use a custom controller. I think this expectation is false. The event handler is just called when spring data rests RepositoryEntityController is in control. It is not an entity event listener on JPA level.

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

What you could do is call the event handler manually. The spring-data-rest RepositoryEventHandler is a using normal spring application events. So your controller could implement ApplicationEventPublisherAware and publish one of the spring-data-rest application events. These are all subclasses of org.springframework.data.rest.core.event.RepositoryEvent

applicationEventPublisher.publishEvent(new AfterCreateEvent(myEntity));

请参阅 spring 文档 了解详情.

这篇关于添加控制器时,SpringDataRest @RepositoryEventHandler 未运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆