微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – Spring Data Rest:RepositoryEventHandler方法未被调用

我试图将一个RepositoryEventHandler添加到如下所示的REST存储库中:
@RepositoryRestResource(collectionResourceRel = "agents",path = "/agents")
public interface AgentRepository extends CrudRepository<Agent,Long> {
    // no implementation required; Spring Data will create a concrete Repository
}

我创建了一个AgentEventHandler:

@Component
@RepositoryEventHandler(Agent.class)
public class AgentEventHandler {

    /**
     * Called before {@link Agent} is persisted 
     * 
     * @param agent
     */
    @HandleBeforeSave
    public void handleBeforeSave(Agent agent) {

        System.out.println("Saving Agent " + agent.toString());

    }
}

并在@Configuration组件中声明它:

@Configuration
public class RepositoryConfiguration {

    /**
     * Declare an instance of the {@link AgentEventHandler}
     *
     * @return
     */
    @Bean
    AgentEventHandler agentEvenHandler() {

        return new AgentEventHandler();
    }
}

当我发布到REST资源时,实体被持久化,但是方法handleBeforeSave从未被调用.我失踪了什么

我使用的是:Spring Boot 1.1.5.RELEASE

解决方法

有时候明显的错误不会被忽视.

发布一个Spring Data REST资源时,会发出一个BeforeCreateEvent.要捕获此事件,必须使用@HandleBeforeCreate而不是@HandleBeforeSave注入方法handleBeforeSave(后者在PUT和PATCH HTTP调用中被调用).

测试成功通过我的(清理)demo app.

原文地址:https://www.jb51.cc/java/125856.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐