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

Listening repository events in spring-boot

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

这是监听器中的代码

@SuppressWarnings("rawtypes")
public class BeforeSaveEventListener extends AbstractRepositoryEventListener {  

    @Override
    public void onBeforeSave(Object customer) {
        throw new RuntimeException("++++ BEFORE SAVE EVENT ++++");
    }
}

这是我的申请class

@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- 引导机制可能不同?

如此处所述Spring-Data-Rest Validator

" ...似乎 before/after "save" 事件仅在 PUT 和 PATCH 上触发。在 POST 时,before/after "create" 事件触发。"。 =11=]