Skip to content

Allow event listener method declared with multiple event classes to take a single parameter that is assignable from all of those event classes #35506

@kalgon

Description

@kalgon

Currently, the javadoc of org.springframework.context.event.EventListener.classes() says the following:

The event classes that this listener handles.
If this attribute is specified with a single value, the annotated method may optionally accept a single parameter. However, if this attribute is specified with multiple values, the annotated method must not declare any parameters.

If I have a hierarchy of events or if some events implement a common interface, I would like to be able to declare which event classes this listener must listen to and have the method accept a single parameter which would be a common ancestor of my events or a common interface.

interface FileEvent {
  String fileName();
}

record FileUploadedEvent(String fileName, ...) implements FileEvent {}
record FileDownloadedEvent(String fileName, ...) implements FileEvent {}
record FileDeletedEvent(String fileName, ...) implements FileEvent {}

@EventListener(classes = {FileUploadedEvent.class, FileDownloadedEvent.class})
void onFileTransfered(FileEvent event) { ... }

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)type: documentationA documentation task

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions