在spring-boot中侦听存储库事件

我正在尝试RepositoryEventListener在spring-boot应用程序中工作,但我想我做错了…

这是Listener中的代码

@SuppressWarnings("rawtypes") public class BeforeSaveEventListener extends AbstractRepositoryEventListener { @Override public void onBeforeSave(Object customer) { throw new RuntimeException("++++ BEFORE SAVE EVENT ++++"); } } 

这是我的Application类

 @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication springApplication = new SpringApplication(Application.class); springApplication.addListeners(new BeforeSaveEventListener()); springApplication.run(args); } } 

在保存操作中,我可以看到这些事件被触发:

 Current Event is org.springframework.data.rest.core.event.BeforeCreateEvent received! Current Event is org.springframework.data.rest.core.event.AfterCreateEvent received! Current Event is org.springframework.web.context.support.ServletRequestHandledEvent received! 

所以没有看到“BeforeSave”事件…可能是文档上不赞成的东西,或者spring-boot机制可能有所不同?

正如此处所解释的Spring-Data-Rest Validator

“……似乎前/后”保存“事件仅在PUT和PATCH上触发。当POSTing时,前面/后面的”创建“事件触发。