Spring Application Events

By | September 20, 2016

In this post I will show how to use Spring events, which are events that can be published and listened to within a Spring application context running in one JVM.


I was looking for a way to decouple different parts of an application I am working on. While I have read about event sourcing, I do not need to store the events and I also do not want to introduce an entire framework or programming paradigm to solve this problem. The good old observer design pattern came to my rescue!
It turns out that the Spring framework version which I am already using in my application, has implemented the observer design pattern with event listeners and publishers within a Spring application context.

Being true to my habit, I like to try out unfamiliar technologies etc in a small example application before I apply it to the problem I have at hand.

Example Application

The example application presented in this article is a Spring Boot application with no additional dependencies running on Spring 4.3.2 and Java 8.
The complete project can be found on GitHub and so I will omit parts that are not closely related to Spring events.
Note that the annotation-based configuration etc for Spring application events are not available in versions prior to version 4.2 of the Spring framework.

Main Application Starter Class

The main application starter class is implemented as follows:

The main part of this class was generated by Spring Boot, I have added two things:

  • The @EnableScheduling annotation.
    Enables scheduling using the @Scheduled Spring annotation on methods that are to be invoked periodically.
    I use Spring scheduling to publish events in the example application.
  • Thread.sleep
    If the application would be to exit immediately, no events would be published.

Event Classes

Event listeners can select which type(s) of event(s) to listen to by specifying the event parameter type of a listener method or by using the classes attribute of the @EventListener annotation on a listener method. In the example I have created three different event classes, but I can imagine that an application can contain many event classes that are crafted with a level of attention similar to that given the domain model.
Instances of all the event classes in this example are immutable.


The BarEvent class just contain a message string.


The FooEvent class is identical to the BarEvent class, except for some of the names:


The MultiLevelEvent class contains a message string and additionally also contain a severity level raining from 1 to 10.

Event Publisher

There is just one single event publisher in this example but it publishes all three types of events according to different schedules:

Note that:

  • The event publishing class implements the ApplicationEventPublisherAware interface.
    This interface allows for the event publishing class to obtain an ApplicationEventPublisher object which it can use to publish application events with.
    The method setApplicationEventPublisher method is from the ApplicationEventPublisherAware interface.
  • Foo events are published each 4th second from the publishFooEvents method.
    The message enclosed with an event is given a timestamp.
  • Bar events are published each 3rd second from the publishBarEvents method.
  • MultiLevel events are published each other second from the publishMultiLevelEvents method.
    In addition to a message string with a time stamp, each event is also given a random severity level from 1 to 10 (inclusive).
  • Each of the event publishing methods prints a message before and after the publishing of an event.
    This will be used to verify that Spring application events are published and processed synchronously.

Event Listeners

The example program contains four event listener classes. They show different aspects of event listening, such as filtering, listening to multiple event types, multiple event listeners receiving one and the same event etc.


The AllEventsListener class listens to all events within the Spring application. This includes, as we will see, some events generated by the Spring framework itself.
The main purpose of this class is to show that one single event listener can listen to many (all) types of events.

Note that:

  • The event listener class is annotated with the @Component annotation.
    Each event listener is to be a Spring bean, in order for Spring to be able to pick up and register the listener methods in the listener.
    Using Java or XML Spring configuration to define listener beans work equally well.
  • The method handleEvent is annotated with the @EventListener annotation.
    This annotation tells Spring that this is a method that is to be registered as an event listener.
  • The type of the inEvent parameter of the handleEvent method is Object.
    This will match all event types and filter out no event types.
  • Handling of the event consists of printing out a message.


The FooBarEventListener contain two event listener methods; one that handles Foo events and another that handles Bar events.

Note that:

  • The event listener class is annotated with the @Component annotation.
    The motivation is the same as for the AllEventsListener event listener class discussed earlier.
  • The method handleFooEvents is annotated with the @EventListener annotation.
    This registers the method with Spring as an event listener method.
  • The parameter-type of the inFooEvent parameter of the handleFooEvents method is FooEvent.
    This will cause Spring to deliver only events of the type FooEvent to the event listener method.
    If there is an event type that, directly or indirectly, inherits from FooEvent then events of this type will also be delivered to this listener method.
  • The method handleBarEvents is annotated with the @EventListener annotation.
    As before, this registers the method with Spring as an event listener method.
  • The @EventListener annotation on the handleBarEvents method contain BarEvent.class.
    This is another way to filter events that are to be delivered to the event listener method, which allows one and the same event listener method to handle multiple types of events that are not related to each other in the class hierarchy.
  • Event processing in the different listener method contains of printing a message with the message string from the event being processed appended.


The FooEventListener class was created to show that multiple listeners can receive one and the same event, provided that the event matches the conditions of the listeners.

Nothing new in this class, so on to the next.


The MultiLevelEventListener event listener shows that it is possible to filter events in a more fine-grained fashion.

As before, the even listener class is annotated with @Component and all the event listener methods are annotated with @EventListener.

Note that:

  • The parameter type of all the three event listener methods are the same – MultiLevelEvent.
    Thus only MultiLevelEvent events will be considered for these three listener methods.
  • Each of the three event listener methods contain a condition attribute on the @EventListener annotation.
    This attribute contain an expression in the Spring Expression Language that will be used to further filter the events considered for the event listener methods.
    Thus the handleLowSeverityEvents method will only handle events of the type MultiLevelEvent which has a severityLevel less than, or equal to, three.
    The handleMidSeverityEvents method handles MultiLevelEvent events with a severityLevel in the range from 3 to 7 (non-inclusive).
    Finally, the handleHighSeverityEvents method handles events of the type MultiLevelEvent with a severityLevel larger than six.

Running the Example Program

If we now run the example program, we will see output similar to this in the console:

Note that:

  • On row 13, the AllEventsListener receives a ContextRefreshedEvent.
    This event is sent by the Spring framework when the Spring application context is initialized or refreshed.
  • On rows 15-19 we can follow the publishing and handling of a FooEvent.
  • The FooEvent is handled by three different event listeners; the AllEventsListener, the FooBarEventListener and the FooEventListener.
  • The event publishing and handling indeed seem synchronous, since the message “After publishing FooEvent” is printed only after all the event listeners have finished handling the event.
  • On rows 28, 32, 45 and 53 we can see that MultiLevelEvent-s with different severity levels are indeed handled by different event listener methods.

This concludes this very brief introduction to Spring application events.

Happy coding!


Leave a Reply

Your email address will not be published. Required fields are marked *