Spring Data Rest: RepositoryEventHandler 方法未被调用
2022-09-03 00:45:39
我正在尝试将Spring Data REST文档中所述的RepositorsEventHandler添加到如下所示的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 资源 POS 化时,实体被持久化,但方法 handleBeforeSave 永远不会被调用。我错过了什么?
我正在使用: 弹簧靴 1.1.5.发布