Event-driven architecture with Spring Events

David Kumar Jan 2026
4 tabs
package com.example.demo.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.context.annotation.Bean;

import java.util.concurrent.Executor;

@Configuration
@EnableAsync
public class AsyncConfig {

    @Bean(name = "taskExecutor")
    public Executor taskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(5);
        executor.setMaxPoolSize(10);
        executor.setQueueCapacity(100);
        executor.setThreadNamePrefix("async-event-");
        executor.initialize();
        return executor;
    }
}
4 files · java Explain with highlit

Spring's event mechanism enables loose coupling between components. ApplicationEventPublisher publishes events. @EventListener handles events asynchronously or synchronously. Events extend ApplicationEvent or are POJOs. @TransactionalEventListener publishes after transaction commit, ensuring consistency. Event ordering uses @Order annotation. Async events with @Async improve responsiveness. Events decouple business logic—order creation triggers email, inventory update, analytics. Domain events model business occurrences. Event sourcing stores events as primary data source. Spring Cloud Stream extends events across microservices. Proper event design prevents tight coupling while enabling reactive, scalable architectures. Events are fundamental to modern application design.