spring-data-keyvalue
spring-data-keyvalue copied to clipboard
ApplicationEvents with generic type argument do not get published to listener using Spring 4.2 [DATAKV-116]
Christoph Strobl opened DATAKV-116 and commented
Type reolution fails for ApplicationEvents
using generic types. Using Spring 4.2 it is required to either implement ResolvableTypeProvider
or extend PayloadApplicationEvent
. In order to support 4.2 from a 4.1 based source we need to provide means to deal with this by providing base handling the generics.
class KeyValueEventListener <E> implements ApplicationListener<KeyValueEvent<?>> {
private final Class<?> domainClass;
public KeyValueEventListener() {
Class<?> typeArgument = GenericTypeResolver.resolveTypeArgument(this.getClass(), NeoEventListener.class);
this.domainClass = typeArgument == null ? Object.class : typeArgument;
}
@SuppressWarnings({ "unchecked" })
public void onApplicationEvent(KeyValueEvent<?> event) {
if (event instanceof BeforeDeleteEvent<?>) {
BeforeDeleteEvent<?> beforeDeleteEvent = (BeforeDeleteEvent<?>) event;
// inspect for matching types and ignore others
if (domainClass.isAssignableFrom(beforeDeleteEvent.getType())) {
onBeforeDelete((BeforeDeleteEvent <E>) event);
}
// other event types...
}
}
protected void onBeforeDelete(BeforeDeleteEvent<E> event) {
}
}
Affects: 1.0 GA (Gosling)