Understanding Spring's Event Publishing and Listening Mechanism: Source Code Analysis and Practical Implementation
This article explains how Spring implements broadcast and listener functionality through ApplicationEventPublisher and ApplicationListener, analyzes the underlying bean post‑processor workflow, and provides complete source‑code examples for custom events, publishers, listeners, and a REST client to demonstrate end‑to‑end event handling.
Spring provides two key functional interfaces— ApplicationEventPublisher and ApplicationListener —to realize broadcast and listening capabilities. The former’s publishEvent() method sends events, while the latter’s onApplicationEvent() method processes them.
During bean initialization, Spring invokes applyBeanPostProcessorsBeforeInitialization and applyBeanPostProcessorsAfterInitialization , which iterate over all BeanPostProcessor implementations. Notably, invokeAwareInterfaces checks whether a bean implements an Aware interface (e.g., ApplicationEventPublisherAware ) and injects the corresponding object, enabling the bean to publish events.
When an event is published, the AbstractApplicationContext delegates to an ApplicationEventMulticaster (by default SimpleApplicationEventMulticaster ). The multicaster retrieves the list of registered ApplicationListener instances from a cache ( ConcurrentHashMap ) via retrieveApplicationListeners(event, type) and invokes each listener’s onApplicationEvent method.
Custom Event Example
public class MyEvent extends ApplicationContextEvent {
public MyEvent(ApplicationContext source) {
super(source);
}
}Custom Publisher Example
@Component
public class MyPublisher implements ApplicationEventPublisherAware, ApplicationContextAware {
private ApplicationEventPublisher applicationEventPublisher;
private ApplicationContext applicationContext;
@Override
public void setApplicationEventPublisher(ApplicationEventPublisher publisher) {
this.applicationEventPublisher = publisher;
}
@Override
public void setApplicationContext(ApplicationContext ctx) throws BeansException {
this.applicationContext = ctx;
}
// Send broadcast message
public void publishEvent() {
System.out.println("我要开始发送消息了。。。");
MyEvent myEvent = new MyEvent(applicationContext);
applicationEventPublisher.publishEvent(myEvent);
}
}Custom Listener Example
@Component
public class MyEventListener implements ApplicationListener
{
@Override
public void onApplicationEvent(MyEvent event) {
System.out.println("我监听到你的消息了");
}
}REST Client to Trigger Event
@RestController
@RequestMapping("/demo")
public class DemoTest {
@Autowired
private MyPublisher myPublisher;
@RequestMapping("/test")
public void test() {
myPublisher.publishEvent();
}
}Accessing http://127.0.0.1:8008/demo/test triggers the publisher, which emits MyEvent . The multicaster then delivers the event to MyEventListener , resulting in the console output:
我要开始发送消息了。。。
我监听到你的消息了Through this step‑by‑step analysis and hands‑on code, readers can fully grasp the inner workings of Spring’s event broadcasting and listening mechanisms.
IT Services Circle
Delivering cutting-edge internet insights and practical learning resources. We're a passionate and principled IT media platform.
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.