Spring Boot Application Events and Listeners

In Spring Boot, Application Events and Listeners allow your application components to communicate in a loosely coupled, event-driven way. It’s a powerful technique for triggering actions like logging, monitoring, auditing, or asynchronous workflows when something meaningful happens in the app.

This post covers:

  • What are Application Events?
  • Built-in Spring Boot Events
  • How to publish custom events
  • How to register event listeners (synchronous & async)
  • Real-world examples

Let’s explore how you can use Spring Boot Application Events to build reactive and maintainable applications.

Spring Boot Application Events and Listeners

πŸ”§ What Are Application Events?

Spring provides an event publishing mechanism where one component can publish an event, and other components can listen for and respond to it.

Think of it like this:

β€œHey, something just happened β€” does anyone care?”

πŸš€ Step 1: Use Built-in Spring Boot Events

Spring Boot provides several lifecycle events, including:

Event ClassDescription
ApplicationStartedEventApp has started but not ready
ApplicationReadyEventApp is fully ready to serve requests
ContextRefreshedEventApplicationContext refreshed
ContextClosedEventApp context closed
ApplicationFailedEventApp startup failed

Example Listener


package com.kscodes.springboot.listener;

import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

@Component
public class AppReadyListener implements ApplicationListener<ApplicationReadyEvent> {
    @Override
    public void onApplicationEvent(ApplicationReadyEvent event) {
        System.out.println("βœ… Application is fully ready!");
    }
}

πŸ§ͺ Step 2: Create a Custom Application Event

You can define your own event by extending ApplicationEvent or simply creating a POJO (recommended since Spring 4.2).

βœ… POJO-Based Custom Event


package com.kscodes.springboot.event;

public class UserCreatedEvent {
    private final String email;

    public UserCreatedEvent(String email) {
        this.email = email;
    }

    public String getEmail() {
        return email;
    }
}

πŸ“£ Step 3: Publish the Event

Inject ApplicationEventPublisher in any Spring bean to publish your custom event.


package com.kscodes.springboot.service;

import com.kscodes.springboot.event.UserCreatedEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;

@Service
public class UserService {

    @Autowired
    private ApplicationEventPublisher publisher;

    public void registerUser(String email) {
        // logic to save user
        System.out.println("πŸ‘€ User registered: " + email);

        publisher.publishEvent(new UserCreatedEvent(email));
    }
}

🎧 Step 4: Create an Event Listener

🧍 Using @EventListener


package com.kscodes.springboot.listener;

import com.kscodes.springboot.event.UserCreatedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

@Component
public class UserCreatedEventListener {

    @EventListener
    public void handleUserCreated(UserCreatedEvent event) {
        System.out.println("πŸ“§ Sending welcome email to: " + event.getEmail());
    }
}

⚑ Optional: Make Listener Asynchronous

You can run listeners on a separate thread using @Async.

βœ… Enable async support:


@SpringBootApplication
@EnableAsync
public class MyApp {}

✨ Then mark the listener async:


@Async
@EventListener
public void handleUserCreatedAsync(UserCreatedEvent event) {
    // time-consuming logic
}

πŸ“š Real-World Use Cases

  • Audit Logging: Track changes and activities across services.
  • Email Notifications: Send emails without blocking user requests.
  • Monitoring: Notify dashboards or external systems.
  • Workflow Triggers: Kick off next steps after a process is complete.

πŸ” Bonus: Conditional Listeners

Use @EventListener(condition = "...") to listen selectively.


@EventListener(condition = "#event.email.endsWith('@admin.com')")
public void handleAdminUser(UserCreatedEvent event) {
    System.out.println("⚠️ Admin user detected: " + event.getEmail());
}

🧹 Cleaning Up with ContextClosedEvent

Listen for shutdown signals (graceful exit, cleanup, notifications).


@Component
public class ShutdownListener implements ApplicationListener<ContextClosedEvent> {
    @Override
    public void onApplicationEvent(ContextClosedEvent event) {
        System.out.println("πŸ›‘ Application is shutting down.");
    }
}

🧡 Summary

Spring Boot Application Events enable you to write decoupled, maintainable, and reactive code by letting components listen for lifecycle or custom business events.

βœ… In this guide, you learned:

  • How to use built-in and custom events
  • How to create and listen to events
  • How to use @EventListener and @Async
  • When to use conditional listeners

Start small β€” then evolve your monolith or microservice into an event-driven architecture!

πŸ”— References