在spring-boot中侦听存储库事件 [英] Listening repository events in spring-boot
本文介绍了在spring-boot中侦听存储库事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试RepositoryEventListener在spring-boot应用程序中工作,但我想我做错了...
I am trying RepositoryEventListener to work in spring-boot application but i guess im doing something wrong ...
这是监听器中的代码
@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);
}
}
在保存操作中,我可以看到这些事件被触发:
On save operation i can see these events fired:
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上不赞成的东西机制可能有所不同?
So no "BeforeSave" event seen ... maybe is something deprecated on documentation, or spring-boot mechanisms may be different?
推荐答案
如此处所述 Spring-Data-Rest Validator
......似乎只有之前/之后的保存事件在PUT和PATCH上触发。当发布时,前面/后面的创建事件触发。
" ... it appears that the before/after "save" events only fire on PUT and PATCH. When POSTing, the before/after "create" events fire."
这篇关于在spring-boot中侦听存储库事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文