spring-projects / spring-projects/spring-data-keyvalue

ApplicationEvents with generic type argument do not get published to listener using Spring 4.2 [DATAKV-116]

Open
#94 0 comments 0 reactions 1 assignee View on GitHub

@christophstrobl is already working on this.

Since Dec 31, 2020.

in: core type: bug
Dominant language
Java
Stars
157
Forks
85
PR merge metrics
No merged PRs in 30d

Description

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)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.