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.
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.MyListener3Method 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 .
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
