Spring Data Rest:未调用RepositoryEventHandler方法

我正在尝试将Spring Data REST文档中描述的RepositoryEventHandler添加到下面显示的REST存储库:

@RepositoryRestResource(collectionResourceRel = "agents", path = "/agents") public interface AgentRepository extends CrudRepository { // 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

有时明显的错误会被忽视。

POST一个Spring Data REST资源,发出一个BeforeCreateEvent 。 要捕获此事件,方法handleBeforeSave必须使用@HandleBeforeCreate而不是@HandleBeforeSave进行注释(后者在PUT和PATCH HTTP调用时调用)。

测试现在已成功通过我(已清理)的演示应用程序 。

您的主要Application类如何? 是否导入了https://spring.io/guides/gs/accessing-data-rest/中描述的RepositoryRestMvcConfiguration?