Rafael
Rafael

Reputation: 2676

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?

Upvotes: 3

Views: 8970

Answers (1)

Rafael
Rafael

Reputation: 2676

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."

Upvotes: 6

Related Questions