Improve generics matching of ApplicationEvent subtypes with generic payload parameter [SPR-13069] #17661
Labels
in: core
Issues in core modules (aop, beans, core, context, expression)
type: enhancement
A general enhancement
Milestone
Oliver Drotbohm opened SPR-13069 and commented
Spring 4.2 introduces better matching of generic types for
ApplicationEvent
instances. The matching now requires a complete generic match for an event being delivered to anApplicationListener
. Assume the following scenario:The main purpose of a generic type introduced in an
ApplicationEvent
is to allowApplicationListeners
to be declared to match that particular type:The delivery of instances of such an event now only works if a dedicated sub-type that fills the generic parameter is used (e.g. a
new EventWithPayload<Person>(person) {
} is thrown). However, if the generic type is used as is, delivery fails as the event type is basically interpreted asEventWithPayload<?>
and thus (understandably) doesn't match the generics matching.I suggest to introduce an annotation that can be used on
EventWithPayload
to let Spring Framework inspect the event's payload type and include that in the matching algorithm. It basically expresses: "in this event type the generic type is used to bind the payload".Alternatively, an idea might be to analyze the return type of
getSource()
and inspect the payload if that method has been overridden to return any of the generic parameters of the type.Affects: 4.2 RC1
Referenced from: commits 06a0dfa, b87816e
The text was updated successfully, but these errors were encountered: