我正在尝试RepositoryEventListener在Spring Boot应用程序中工作,但我想我做错了什么…
这是侦听器中的代码
@SuppressWarnings("rawtypes") public class BeforeSaveEventListener extends AbstractRepositoryEventListener { @Override public void onBeforeSave(Object customer) { throw new RuntimeException("++++ BEFORE SAVE EVENT ++++"); } }
这是我的应用程序类
@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验证程序
“……看来,“保存”之前/之后的事件仅在PUT和PATCH上触发。发布时,“创建”之前/之后的事件才触发。”