【问题标题】:SpringDataRest @RepositoryEventHandler not running when Controller is added添加控制器时 SpringDataRest @RepositoryEventHandler 未运行
【发布时间】:2016-03-16 14:53:33
【问题描述】:

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

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

【问题讨论】:

    标签: spring-data-rest


    【解决方案1】:

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

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

    applicationEventPublisher.publishEvent(new AfterCreateEvent(myEntity));
    

    详情请参阅spring documentation

    【讨论】:

    • 根据可用的文档,如果新用户的控制器带有@RepositoryRestController 注释,我认为新用户做出这种假设是不合理的。毕竟,它叫@RepositoryEventHandler,而不是@RepositoryControllerEventHandler
    • @Snekse 可能是这种情况 - 我可以告诉你,情况并非如此 - 但我详细说明了我的答案,其中包含有关如何仍然使用 RepositoryEventHandler 的信息您的自定义控制器。
    • 正如 Mathias 所说,您可以发布自己的活动。确保您处理所有这些(之前、之后、创建、保存、列出、删除)。这是另一个很好的例子。 stackoverflow.com/a/37616850/378151 在我们的项目中,我们有一个 MyEntityValidator,我们从控制器调用它,并且 Repo 事件处理程序也调用。
    猜你喜欢
    • 1970-01-01
    • 2017-03-17
    • 2011-09-21
    • 1970-01-01
    • 1970-01-01
    • 2017-06-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多