简体   繁体   中英

Listening repository events in spring-boot

I am trying RepositoryEventListener to work in spring-boot application but i guess im doing something wrong ...

This is the code in the Listener

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

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

This is my Application class

@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!

So no "BeforeSave" event seen ... maybe is something deprecated on documentation, or spring-boot mechanisms may be different?

As explained here Spring-Data-Rest Validator

" ... it appears that the before/after "save" events only fire on PUT and PATCH. When POSTing, the before/after "create" events fire."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM