Master Spring Boot Event Listeners: 4 Ways to Implement Custom Events

This article explains four distinct approaches to register Spring Boot event listeners, shows how to create custom events and listeners, provides complete code examples for each method, and demonstrates their execution order through a test run.

Java Interview Crash Guide
Java Interview Crash Guide
Java Interview Crash Guide
Master Spring Boot Event Listeners: 4 Ways to Implement Custom Events

Last week a friend was asked about Spring Boot event listeners in an interview, so we dive into the topic.

Spring Boot provides four ways to register event listeners:

Manually add a listener to ApplicationContext.

Load the listener into the Spring container.

Configure the listener in application.properties.

Use the @EventListener annotation.

Custom events are created by extending ApplicationEvent and defining a constructor; custom listeners implement ApplicationListener<T> and override onApplicationEvent.

Method 1

Create a listener class that implements ApplicationListener<MyEvent> and register it manually via ConfigurableApplicationContext.

public class MyListener1 implements ApplicationListener<MyEvent> {
    Logger logger = Logger.getLogger(MyListener1.class);
    public void onApplicationEvent(MyEvent event) {
        logger.info(String.format("%s listened to source: %s.", MyListener1.class.getName(), event.getSource()));
    }
}

In the Spring Boot main class, obtain the context and add the listener:

@SpringBootApplication
public class ListenerApplication {
    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(ListenerApplication.class, args);
        // register listener
        context.addApplicationListener(new MyListener1());
    }
}

Method 2

Annotate the listener class with @Component so that Spring registers it automatically.

@Component
public class MyListener2 implements ApplicationListener<MyEvent> {
    Logger logger = Logger.getLogger(MyListener2.class);
    public void onApplicationEvent(MyEvent event) {
        logger.info(String.format("%s listened to source: %s.", MyListener2.class.getName(), event.getSource()));
    }
}

Method 3

Define the listener class and configure its fully‑qualified name in application.properties using context.listener.classes.

public class MyListener3 implements ApplicationListener<MyEvent> {
    Logger logger = Logger.getLogger(MyListener3.class);
    public void onApplicationEvent(MyEvent event) {
        logger.info(String.format("%s listened to source: %s.", MyListener3.class.getName(), event.getSource()));
    }
}
context.listener.classes=com.listener.MyListener3

Method 4

Use the @EventListener annotation on a method; the class does not need to implement ApplicationListener.

@Component
public class MyListener4 {
    Logger logger = Logger.getLogger(MyListener4.class);
    @EventListener
    public void listener(MyEvent event) {
        logger.info(String.format("%s listened to source: %s.", MyListener4.class.getName(), event.getSource()));
    }
}

Custom event definition:

@SuppressWarnings("serial")
public class MyEvent extends ApplicationEvent {
    public MyEvent(Object source) {
        super(source);
    }
}

Test by publishing the event in the main class:

@SpringBootApplication
public class ListenerApplication {
    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(ListenerApplication.class, args);
        // register listener
        context.addApplicationListener(new MyListener1());
        // publish event
        context.publishEvent(new MyEvent("Test event."));
    }
}

Running the application prints ordered log messages from each listener, demonstrating that the four registration methods work and are invoked in order.

Source code: https://github.com/ingorewho/springboot-develope/tree/master/springboot-listener
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaBackend DevelopmentSpring Bootevent-listenerApplicationEvent
Java Interview Crash Guide
Written by

Java Interview Crash Guide

Dedicated to sharing Java interview Q&A; follow and reply "java" to receive a free premium Java interview guide.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.