Skip to main content

@EventListener Methods Should Have One Parameter at Most

High
bugerror-prone

What is it?

This practice is triggered when methods annotated with @EventListener have more than one parameter, which violates the expected method signature for event handlers.

Why apply it?

Having more than one parameter in @EventListener methods can lead to runtime errors because the Spring framework expects these methods to only handle one event at a time.

How to fix it?

Modify the method to handle multiple types of events using the classes attribute of the @EventListener annotation, and ensure the method only has a single parameter representing the event.

Examples

Example 1:

Negative

This example mistakenly uses multiple parameters, which will cause a runtime error as @EventListener expects a single argument.

import org.springframework.context.event.EventListener;

public class EventProcessor {

@EventListener
public void handleEvent(CustomerEvent customerEvent, ExceptionalEvent exceptionalEvent) { /* Noncompliant */
System.out.println("Handling multiple events.");
}
}

Example 2:

Positive

This example demonstrates how to use a single parameter with @EventListener and leverage the classes attribute to handle multiple event types.

import org.springframework.context.event.EventListener;
import org.springframework.context.ApplicationEvent;

public class EventProcessor {

@EventListener(classes = {CustomerEvent.class, ExceptionalEvent.class})
public void handleEvent(ApplicationEvent event) { /* Compliant */
if (event instanceof CustomerEvent) {
System.out.println("Handling CustomerEvent...");
} else if (event instanceof ExceptionalEvent) {
System.out.println("Handling ExceptionalEvent...");
}
}
}

Negative

This negative example attempts to use two parameters for two different events, which violates the expected usage of @EventListener.

import org.springframework.context.event.EventListener;

public class SpecialEventHandler {

@EventListener
public void processEvent(CustomEvent customEvent, OtherEvent otherEvent) { /* Noncompliant */
System.out.println("Processing events: " + customEvent.getDescription() + " and " + otherEvent.getInfo());
}
}

Example 3:

Positive

This positive example uses a subclass of ApplicationEvent with a single parameter to handle specific events.

import org.springframework.context.event.EventListener;
import org.springframework.context.ApplicationEvent;

public class SpecialEventHandler {

@EventListener
public void processEvent(CustomEvent customEvent) { /* Compliant */
System.out.println("Processing custom event: " + customEvent.getDescription());
}
}