I just announced the new Spring Boot 2 material, coming in REST With Spring:

>> CHECK OUT THE COURSE

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:

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

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

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

    @HandleAfterDelete
    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:

@RepositoryEventHandler
public class BookEventHandler {

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

    @HandleBeforeCreate
    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:

@Configuration
public class RepositoryConfiguration{
    
    public RepositoryConfiguration(){
        super();
    }

    @Bean
    AuthorEventHandler authorEventHandler() {
        return new AuthorEventHandler();
    }

    @Bean
    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 Boot 2 material, coming in REST With Spring:

>> CHECK OUT THE LESSONS