Spring Boot – Publish and Consume Custom Application Events

This post demonstrates how to implement the Observer Design Pattern using the facilities made available by Spring Application Events.

We’ll use

  • Spring Boot

Observer Design Pattern

Here is an short video that describes the Observer Design Pattern

Spring Boot Application

Let’s create a new Spring Boot Application using IntelliJ (optional) and Spring Initialzr (or http://start.spring.io/).

Step 1: Create a new project

Choose Spring Initialzr. Then click Next.

Step 2: Provide project information

Step 3: Choose nothing

Step 4: Save the new project

Step 5: Create a custom event class

This is the event type that gets published and consume by our codes.

Step 6: Create a event publisher

Step 7: Create a event source class

Step 8: Create a event handler

This represents out observer that handlers the event. It basically waits for an event “sent” by the publisher.

It is possible to have multiple observers by creating similar class that implements the same interface.

The Main class


Download the codes