Event

Event

In software development, an event is an action or occurrence that happens in a program, to which the program might respond.

  • These events are often generated by the user’s interaction with the application, but they can also be triggered by the system itself.
  • Common Examples of Events:
    • A user clicking a button.
    • A user pressing a key on the keyboard.
    • A web page finishing its loading process.
    • A window being resized.
    • Receiving data from a server.
  • Think of an event as a signal that something has happened.

Event Listener

Event

An event listener (also known as an event handler) is a function that ā€œlistensā€ for a specific event to occur on a specific element.

  • When the event happens, the listener is ā€œtriggered,ā€ and the code inside the function is executed.
  • @EventListener Annotation
    • marks a method as an event listener
    • When you start your application, Spring scans for all methods marked with @EventListener. When eventPublisher.publishEvent() is called, Spring checks the type of event being published (in our case, UserCreatedEvent). It then finds the matching listener method (sendWelcomeEmail) and executes it, passing the event object as an argument
    • creates a loosely coupled system ⇒ The UserService has no idea that an EmailService even exists

Sample diagram

Example

A common use case is sending a welcome email after a new user registers.

UserCreatedEvent

  • First, we define the event itself. This is a simple class that holds the data related to the event, like the new user’s email.
// This class represents the "what happened" signal.
public class UserCreatedEvent {
    private final String userEmail;
 
    public UserCreatedEvent(String userEmail) {
        this.userEmail = userEmail;
    }
 
    public String getUserEmail() {
        return userEmail;
    }
}

UserService

  • The publisher is the component that creates and sends out the event. Here, a UserService publishes a UserCreatedEvent after creating a new user.
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
 
@Service
public class UserService {
 
    // Spring injects an event publisher for us.
    private final ApplicationEventPublisher eventPublisher;
 
    public UserService(ApplicationEventPublisher eventPublisher) {
        this.eventPublisher = eventPublisher;
    }
 
    public void createUser(String email, String password) {
        // ... logic to save the user to the database ...
        System.out.println("User created with email: " + email);
 
        // Announce that a new user has been created.
        UserCreatedEvent event = new UserCreatedEvent(email);
        eventPublisher.publishEvent(event);
    }
}

EmailService

  • The listener waits for the event. The EmailService isn’t called directly by the UserService. Instead, it just listens for the UserCreatedEvent.
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;
 
@Service
public class EmailService {
 
    // This method will automatically be called when a UserCreatedEvent is published.
    @EventListener(UserCreatedEvent.class)
    public void sendWelcomeEmail(UserCreatedEvent event) {
        System.out.println("Sending welcome email to: " + event.getUserEmail());
        // ... logic to actually send the email ...
    }
}