Event publishing lets parts of your app talk to each other without being tightly connected. It helps keep your code clean and organized.
0
0
Event publishing with ApplicationEventPublisher in Spring Boot
Introduction
When you want to notify other parts of your app about something that happened, like a user signing up.
When you want to separate concerns, so one part handles an action and another part reacts to it.
When you want to trigger background tasks after a main action, like sending an email after order placement.
When you want to build a flexible system where new features can listen to events without changing existing code.
Syntax
Spring Boot
public class MyEvent extends ApplicationEvent { public MyEvent(Object source) { super(source); } } @Component public class MyEventPublisher { private final ApplicationEventPublisher eventPublisher; public MyEventPublisher(ApplicationEventPublisher eventPublisher) { this.eventPublisher = eventPublisher; } public void publish() { MyEvent event = new MyEvent(this); eventPublisher.publishEvent(event); } }
The event class extends ApplicationEvent and usually has a constructor with a source object.
The publisher class uses ApplicationEventPublisher to send events.
Examples
This event carries extra data (username) about the event.
Spring Boot
public class UserRegisteredEvent extends ApplicationEvent { private final String username; public UserRegisteredEvent(Object source, String username) { super(source); this.username = username; } public String getUsername() { return username; } }
Publisher sends the event with extra data.
Spring Boot
@Component
public class UserEventPublisher {
private final ApplicationEventPublisher eventPublisher;
public UserEventPublisher(ApplicationEventPublisher eventPublisher) {
this.eventPublisher = eventPublisher;
}
public void publishUserRegistered(String username) {
UserRegisteredEvent event = new UserRegisteredEvent(this, username);
eventPublisher.publishEvent(event);
}
}Sample Program
This Spring Boot app publishes a user registered event and listens to it. When you run it, it prints messages before and after publishing the event.
Spring Boot
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.event.EventListener; import org.springframework.stereotype.Component; @SpringBootApplication public class EventDemoApplication { public static void main(String[] args) { var context = SpringApplication.run(EventDemoApplication.class, args); UserEventPublisher publisher = context.getBean(UserEventPublisher.class); System.out.println("Publishing user registered event..."); publisher.publishUserRegistered("alice"); } } class UserRegisteredEvent extends ApplicationEvent { private final String username; public UserRegisteredEvent(Object source, String username) { super(source); this.username = username; } public String getUsername() { return username; } } @Component class UserEventPublisher { private final ApplicationEventPublisher eventPublisher; public UserEventPublisher(ApplicationEventPublisher eventPublisher) { this.eventPublisher = eventPublisher; } public void publishUserRegistered(String username) { UserRegisteredEvent event = new UserRegisteredEvent(this, username); eventPublisher.publishEvent(event); } } @Component class UserEventListener { @EventListener public void handleUserRegistered(UserRegisteredEvent event) { System.out.println("User registered event received for user: " + event.getUsername()); } }
OutputSuccess
Important Notes
Publishing an event is fast and usually runs in the same thread by default.
Listeners can be synchronous or asynchronous if configured.
Common mistake: forgetting to annotate listener methods with @EventListener.
Summary
Use ApplicationEventPublisher to send events inside your app.
Create event classes by extending ApplicationEvent.
Listeners react to events using @EventListener annotation.