Four Ways to Implement Event Listeners in Spring Boot

This article explains four approaches for implementing Spring Boot event listeners—including manual registration, component scanning, properties configuration, and @EventListener annotation—while also covering how to create custom events and listener classes with complete code examples.

Selected Java Interview Questions
Selected Java Interview Questions
Selected Java Interview Questions
Four Ways to Implement Event Listeners in Spring Boot

Spring Boot provides four ways to listen for events:

Manually add a listener to the ApplicationContext.

Load the listener as a Spring bean.

Configure the listener in application.properties.

Use the @EventListener annotation.

Custom events are created by extending ApplicationEvent, and custom listeners implement ApplicationListener<T> or use @EventListener on a method.

Method 1: Manual registration

Create MyListener1 implementing ApplicationListener<MyEvent>:

public class MyListener1 implements ApplicationListener<MyEvent> {
    Logger logger = Logger.getLogger(MyListener1.class);
    public void onApplicationEvent(MyEvent event) {
        logger.info(String.format("%s监听到事件源:%s.", MyListener1.class.getName(), event.getSource()));
    }
}

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

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

Method 2: Component scanning

Annotate the listener with @Component so 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监听到事件源:%s.", MyListener2.class.getName(), event.getSource()));
    }
}

Method 3: Configuration file

Create MyListener3 (same as above) and add its fully‑qualified class name to application.properties:

context.listener.classes=com.listener.MyListener3

Method 4: @EventListener annotation

Define a bean without implementing ApplicationListener and annotate a method with @EventListener:

@Component
public class MyListener4 {
    Logger logger = Logger.getLogger(MyListener4.class);
    @EventListener
    public void listener(MyEvent event) {
        logger.info(String.format("%s监听到事件源:%s.", MyListener4.class.getName(), event.getSource()));
    }
}

Custom event class example:

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

Testing by publishing the event in the main class:

@SpringBootApplication
public class LisenterApplication {
    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(LisenterApplication.class, args);
        // Register listeners (if not using component scanning)
        context.addApplicationListener(new MyListener1());
        // Publish event
        context.publishEvent(new MyEvent("测试事件."));
    }
}

Running the application prints log messages from each listener, demonstrating that the four implementations are invoked in order.

Full source code is available at 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 BootApplicationListenerevent-listenerEventListener
Selected Java Interview Questions
Written by

Selected Java Interview Questions

A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!

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.