I just announced the new Spring 5 modules in REST With Spring:


1. Introduction

While working with an entity, the REST exporter handles operations for creating, saving, and deleting events. We can use an ApplicationListener to listen to these events and execute a function when the particular action is performed.

Alternatively, we can use annotated handler which filters events based on domain type.

2. Writing an Annotated Handler

The ApplicationListener doesn’t distinguish between entity types; but with the annotated handler, we can filter events based on domain type.

We can declare an annotation based event handler by adding @RepositoryEventHandler annotation on a POJO. As a result, this informs the BeanPostProcessor that the POJO needs to be inspected for handler methods.

In the example below, we annotate the class with RepositoryEventHandler corresponding to the entity Author – and declare methods pertaining to different before and after events corresponding to the Author entity in the AuthorEventHandler class:

public class AuthorEventHandler {
    Logger logger = Logger.getLogger("Class AuthorEventHandler");
    public void handleAuthorBeforeCreate(Author author){
        logger.info("Inside Author Before Create....");
        String name = author.getName();

    public void handleAuthorAfterCreate(Author author){
        logger.info("Inside Author After Create ....");
        String name = author.getName();

    public void handleAuthorBeforeDelete(Author author){
        logger.info("Inside Author Before Delete ....");
        String name = author.getName();

    public void handleAuthorAfterDelete(Author author){
        logger.info("Inside Author After Delete ....");
        String name = author.getName();

Here, different methods of the AuthorEventHandler class will be invoked based on the operation performed on Author entity.

On finding the class with @RepositoryEventHandler annotation, Spring iterates over the methods in the class to find annotations corresponding to the before and after events mentioned below:

Before* Event Annotations – associated with before annotations are called before the event is called.

  • BeforeCreateEvent
  • BeforeDeleteEvent
  • BeforeSaveEvent
  • BeforeLinkSaveEvent

After* Event Annotations – associated with after annotations are called after the event is called.

  • AfterLinkSaveEvent
  • AfterSaveEvent
  • AfterCreateEvent
  • AfterDeleteEvent

We can also declare methods with different entity type corresponding to the same event type in a class:

public class BookEventHandler {

    public void handleBookBeforeCreate(Book book){
        // code for before create book event

    public void handleAuthorBeforeCreate(Author author){
        // code for before create author event

Here, the BookEventHandler class deals with more than one entity. On finding the class with @RepositoryEventHandler annotation, it iterates over the methods and calls the respective entity before the respective create event.

Also, we need to declare the event handlers in the @Configuration class which will inspect the bean for handlers and matches them with the right events:

public class RepositoryConfiguration{
    public RepositoryConfiguration(){

    AuthorEventHandler authorEventHandler() {
        return new AuthorEventHandler();

    BookEventHandler bookEventHandler(){
        return new BookEventHandler();

3. Conclusion

In conclusion, this serves as an introduction to implementing and understanding @RepositoryEventHandler.

In this quick tutorial, we learned how to implement @RepositoryEventHandler annotation to handle various events corresponding to entity type.

And, as always, find the complete code samples over on Github.

I just announced the new Spring 5 modules in REST With Spring:


Leave a Reply

Notify of