Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

Java Automation Job Top
We’re looking for a Backend Java/Spring Developer with Integration Experience: Read More

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.

REST bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course :

Persistence bottom

Get started with Spring Data JPA through the reference Learn Spring Data JPA course:

REST footer banner
Comments are closed on this article!