【发布时间】:2014-10-20 01:21:51
【问题描述】:
我正在尝试将Spring Data REST documentation 中所述的 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
【问题讨论】:
-
如果你宁愿扩展
AbstractRepositoryEventListener,它是否有效? -
@OliverGierke(感谢您的介入)不,没有区别 [公共类 AgentEventHandler 扩展 AbstractRepositoryEventListener] 也 [公共类 AgentEventHandler 扩展 AbstractRepositoryEventListener
] -
你确定
RepositoryConfiguration类被 Boot 选中了吗?如果是这样,您是否有机会创建一个失败的小示例项目并在我们的JIRA 中创建票证? -
@OliverGierke 我认为是(我在 agentEvenHandler 方法中添加了一个 System.out.println("registering AgentEventHandler");...如果有更好的方法来检查这个,请告诉我) . Jira:我试试看!
-
@OliverGierke 不确定这是否真的是一个错误或我配置错误的东西。所以我在github 上推送了一个示例项目,而不是创建票证。
标签: java spring spring-data spring-data-rest