【问题标题】:Listening repository events in spring-boot在 spring-boot 中监听存储库事件
【发布时间】:2015-06-02 12:02:31
【问题描述】:

我正在尝试 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 机制可能不同?

【问题讨论】:

    标签: java spring-boot spring-data-jpa


    【解决方案1】:

    正如这里所解释的Spring-Data-Rest Validator

    " ... 之前/之后的“保存”事件似乎只在 PUT 和 PATCH 上触发。发布时,之前/之后的“创建”事件触发。”

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-28
      • 2019-03-10
      • 2015-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-22
      • 1970-01-01
      相关资源
      最近更新 更多