
Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:
Once the early-adopter seats are all used, the price will go up and stay at $33/year.
Last updated: March 14, 2025
In this tutorial, we’ll be discussing how to use events in Spring.
Events are one of the most overlooked functionalities in the framework, although they’re also among the most useful. And like many other things in Spring, event publishing is one of the capabilities provided by ApplicationContext.
There are a few simple guidelines to follow:
Spring allows us to create and publish custom events that by default are synchronous. This has a few advantages, such as the listener being able to participate in the publisher’s transaction context.
Let’s create a simple event class — just a placeholder to store the event data.
In this case, the event class holds a String message:
public class CustomSpringEvent extends ApplicationEvent {
private String message;
public CustomSpringEvent(Object source, String message) {
super(source);
this.message = message;
}
public String getMessage() {
return message;
}
}
Now let’s create a publisher of that event. The publisher constructs the event object and publishes it to anyone who’s listening.
To publish the event, the publisher can simply inject the ApplicationEventPublisher and use the publishEvent() API:
@Component
public class CustomSpringEventPublisher {
@Autowired
private ApplicationEventPublisher applicationEventPublisher;
public void publishCustomEvent(final String message) {
System.out.println("Publishing custom event. ");
CustomSpringEvent customSpringEvent = new CustomSpringEvent(this, message);
applicationEventPublisher.publishEvent(customSpringEvent);
}
}
Alternatively, the publisher class can implement the ApplicationEventPublisherAware interface, and this will also inject the event publisher on the application startup. Usually, it’s simpler to just inject the publisher with @Autowire.
As of Spring Framework 4.2, the ApplicationEventPublisher interface provides a new overload for the publishEvent(Object event) method that accepts any object as the event. Therefore, Spring events no longer need to extend the ApplicationEvent class.
Finally, let’s create the listener.
The only requirement for the listener is to be a bean and implement ApplicationListener interface:
@Component
public class CustomSpringEventListener implements ApplicationListener<CustomSpringEvent> {
@Override
public void onApplicationEvent(CustomSpringEvent event) {
System.out.println("Received spring custom event - " + event.getMessage());
}
}
Notice how our custom listener is parametrized with the generic type of custom event, which makes the onApplicationEvent() method type-safe. This also avoids having to check if the object is an instance of a specific event class and casting it.
And, as already discussed (by default Spring events are synchronous), the doStuffAndPublishAnEvent() method blocks until all listeners finish processing the event.
In some cases, publishing events synchronously isn’t really what we’re looking for — we may need async handling of our events.
We can turn asynchronous event handling on in the configuration by creating an ApplicationEventMulticaster bean with an executor.
For our purposes here, SimpleAsyncTaskExecutor works well:
@Configuration
public class AsynchronousSpringEventsConfig {
@Bean(name = "applicationEventMulticaster")
public ApplicationEventMulticaster simpleApplicationEventMulticaster() {
SimpleApplicationEventMulticaster eventMulticaster =
new SimpleApplicationEventMulticaster();
eventMulticaster.setTaskExecutor(new SimpleAsyncTaskExecutor());
return eventMulticaster;
}
}
The event, publisher, and listener implementations remain the same as before, but now the listener will asynchronously deal with the event in a separate thread.
However, there are times when we can’t use the multicaster or otherwise prefer to have some events operate asynchronously and not others.
To this end, we can add Spring’s @Async annotation to identify and annotate individual listeners that should process events asynchronously:
@EventListener
@Async
public void handleAsyncEvent(CustomSpringEvent event) {
System.out.println("Handle event asynchronously: " + event.getMessage());
}
This processes an event in a separate thread. Furthermore, we can use the value attribute of the @Async annotation to indicate that an executor other than the default should be used, for example:
@Async("nonDefaultExecutor")
void handleAsyncEvent(CustomSpringEvent event) {
// run asynchronously by "nonDefaultExecutor"
}
To enable support for @Async annotations, we can add @EnableAsync to a @Configuration or @SpringBootApplication class:
@Configuration
@EnableAsync
public class AppConfig {
}
The @EnableAsync annotation switches on Spring’s ability to run @Async methods in a background thread pool. It also customizes the used Executor. Spring searches for an associated thread pool definition. It looks for either:
If it doesn’t find either, a SimpleAsyncTaskExecutor will be used to invoke event listeners asynchronously.
Spring itself publishes a variety of events out of the box. For example, the ApplicationContext will fire various framework events: ContextRefreshedEvent, ContextStartedEvent, RequestHandledEvent etc.
These events provide application developers an option to hook into the life cycle of the application and the context and add in their own custom logic where needed.
Here’s a quick example of a listener listening for context refreshes:
public class ContextRefreshedListener
implements ApplicationListener<ContextRefreshedEvent> {
@Override
public void onApplicationEvent(ContextRefreshedEvent cse) {
System.out.println("Handling context re-freshed event. ");
}
}
To learn more about existing framework events, have a look at our next tutorial here.
Starting with Spring 4.2, an event listener is not required to be a bean implementing the ApplicationListener interface — it can be registered on any public method of a managed bean via the @EventListener annotation:
@Component
public class AnnotationDrivenEventListener {
@EventListener
public void handleContextStart(ContextStartedEvent cse) {
System.out.println("Handling context started event.");
}
}
As before, the method signature declares the event type it consumes.
By default, the listener is invoked synchronously. However, we can easily make it asynchronous by adding an @Async annotation. We just need to remember to EnableAsync support in the application.
It is also possible to dispatch events with generics information in the event type.
Let’s create a generic event type.
In our example, the event class holds any content and a success status indicator:
public class GenericSpringEvent<T> {
private T what;
protected boolean success;
public GenericSpringEvent(T what, boolean success) {
this.what = what;
this.success = success;
}
// ... standard getters
}
Notice the difference between GenericSpringEvent and CustomSpringEvent. We now have the flexibility to publish any arbitrary event and it’s not required to extend from ApplicationEvent anymore.
Now let’s create a listener of that event.
We could define the listener by implementing the ApplicationListener interface like before:
@Component
public class GenericSpringEventListener
implements ApplicationListener<GenericSpringEvent<String>> {
@Override
public void onApplicationEvent(@NonNull GenericSpringEvent<String> event) {
System.out.println("Received spring generic event - " + event.getWhat());
}
}
But this definition unfortunately requires us to inherit GenericSpringEvent from the ApplicationEvent class. So for this tutorial, let’s make use of an annotation-driven event listener discussed previously.
It is also possible to make the event listener conditional by defining a boolean SpEL expression on the @EventListener annotation.
In this case, the event handler will only be invoked for a successful GenericSpringEvent of String:
@Component
public class AnnotationDrivenEventListener {
@EventListener(condition = "#event.success")
public void handleSuccessful(GenericSpringEvent<String> event) {
System.out.println("Handling generic event (conditional).");
}
}
The Spring Expression Language (SpEL) is a powerful expression language that’s covered in detail in another tutorial.
The event publisher is similar to the one described above. But due to type erasure, we need to publish an event that resolves the generics parameter we would filter on, for example, class GenericStringSpringEvent extends GenericSpringEvent<String>.
Also, there’s an alternative way of publishing events. If we return a non-null value from a method annotated with @EventListener as the result, Spring Framework will send that result as a new event for us. Moreover, we can publish multiple new events by returning them in a collection as the result of event processing.
This section is about using the @TransactionalEventListener annotation. To learn more about transaction management, check out Transactions With Spring and JPA.
Since Spring 4.2, the framework provides a new @TransactionalEventListener annotation, which is an extension of @EventListener, that allows binding the listener of an event to a phase of the transaction.
Binding is possible to one of four transaction phases:
Here’s a quick example of a transactional event listener:
@TransactionalEventListener(phase = TransactionPhase.BEFORE_COMMIT)
public void handleCustom(CustomSpringEvent event) {
System.out.println("Handling event inside a transaction BEFORE COMMIT.");
}
This listener will be invoked only if there’s a transaction in which the event producer is running and it’s about to be committed. And if no transaction is running, the event isn’t sent at all unless we override this by setting the fallbackExecution attribute to true.
To handle these asynchronously, we need a slightly different approach than using a multicaster. This is because Spring Framework has designed TransactionalEventListeners to work with thread-bound transactions, whereas the ApplicationEventMulticaster handles events in a separate thread. Therefore, using the two together would break the default functioning of a TransactionalEventListener.
To address this, we can create an async, transactional event listener running in a transaction itself. To do this, we annotate it with @Transactional in turn:
@Async
@Transactional(propagation = Propagation.REQUIRES_NEW)
@TransactionalEventListener
void handleCustom(CustomSpringEvent event) {
//...
}
As before, let’s remember to add @EnableAsync support.
In this quick article, we went over the basics of dealing with events in Spring, including creating a simple custom event, publishing it and then handling it in a listener. We also had a brief look at how to enable the asynchronous processing of events in the configuration.
Then we learned about improvements introduced in Spring 4.2, such as annotation-driven listeners, better generics support, and events binding to transaction phases.
Follow the Spring Category