0
0
Spring Bootframework~5 mins

Event-driven architecture pattern in Spring Boot

Choose your learning style9 modes available
Introduction

Event-driven architecture helps your app react to things happening, like messages or actions, without waiting. It makes your app more flexible and faster.

When you want different parts of your app to work independently and communicate by sending messages.
When your app needs to handle many tasks at the same time without slowing down.
When you want to build apps that can easily grow or change without breaking everything.
When you want to respond quickly to user actions or system events, like new orders or notifications.
Syntax
Spring Boot
1. Define an event class (a simple Java class).
2. Create an event publisher using ApplicationEventPublisher.
3. Create an event listener using @EventListener annotation.
4. Publish events from your service or controller.

Example:

public class MyEvent {
    private final String message;
    public MyEvent(String message) { this.message = message; }
    public String getMessage() { return message; }
}

@Component
public class MyEventPublisher {
    private final ApplicationEventPublisher publisher;
    public MyEventPublisher(ApplicationEventPublisher publisher) {
        this.publisher = publisher;
    }
    public void publish(String message) {
        publisher.publishEvent(new MyEvent(message));
    }
}

@Component
public class MyEventListener {
    @EventListener
    public void handleMyEvent(MyEvent event) {
        System.out.println("Received event: " + event.getMessage());
    }
}

Use @EventListener on methods to listen for events.

Events are simple Java objects carrying data.

Examples
This defines a simple event carrying a username.
Spring Boot
public class UserCreatedEvent {
    private final String username;
    public UserCreatedEvent(String username) { this.username = username; }
    public String getUsername() { return username; }
}
This listens for UserCreatedEvent and prints the username.
Spring Boot
@Component
public class UserEventListener {
    @EventListener
    public void onUserCreated(UserCreatedEvent event) {
        System.out.println("User created: " + event.getUsername());
    }
}
This publishes the event after creating a user.
Spring Boot
@Component
public class UserService {
    private final ApplicationEventPublisher publisher;
    public UserService(ApplicationEventPublisher publisher) {
        this.publisher = publisher;
    }
    public void createUser(String username) {
        // logic to create user
        publisher.publishEvent(new UserCreatedEvent(username));
    }
}
Sample Program

This Spring Boot app creates a user and publishes an event. The listener prints a message when it receives the event.

Spring Boot
package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        ApplicationContext context = SpringApplication.run(DemoApplication.class, args);
        var userService = context.getBean(UserService.class);
        userService.createUser("alice");
    }
}

class UserCreatedEvent {
    private final String username;
    public UserCreatedEvent(String username) { this.username = username; }
    public String getUsername() { return username; }
}

@Component
class UserService {
    private final ApplicationEventPublisher publisher;
    public UserService(ApplicationEventPublisher publisher) {
        this.publisher = publisher;
    }
    public void createUser(String username) {
        System.out.println("Creating user: " + username);
        publisher.publishEvent(new UserCreatedEvent(username));
    }
}

@Component
class UserEventListener {
    @EventListener
    public void onUserCreated(UserCreatedEvent event) {
        System.out.println("User created event received for: " + event.getUsername());
    }
}
OutputSuccess
Important Notes

Events help separate concerns: one part sends info, another reacts.

Listeners run in the same thread by default; use async if needed.

Spring Boot makes event handling easy with annotations.

Summary

Event-driven architecture lets parts of your app talk by sending and listening to events.

Spring Boot uses simple Java classes and annotations to handle events.

This pattern helps build apps that are flexible and easy to maintain.