加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-14 16:17:47 所属栏目:Java 来源:网络整理
导读:我试图将一个RepositoryEventHandler添加到如下所示的REST存储库中: @RepositoryRestResource(collectionResourceRel = "agents",path = "/agents")public interface AgentRepository extends CrudRepositoryAgent,Long { // no implementation required; S
我试图将一个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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读