Skip to content

Commit bd82acb

Browse files
committed
Support ResolvableTypeProvider on simple event pojo
Previously, the generic type of a simple pojo event implementing ResolvableTypeProvider wasn't detected properly. This commit fixes the logic when the generic type is not provided to reuse what PayloadApplicationEvent is already doing anyway. Issue: SPR-14029
1 parent f76b38c commit bd82acb

File tree

3 files changed

+90
-2
lines changed

3 files changed

+90
-2
lines changed

spring-context/src/main/java/org/springframework/context/support/AbstractApplicationContext.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ protected void publishEvent(Object event, ResolvableType eventType) {
369369
else {
370370
applicationEvent = new PayloadApplicationEvent<Object>(this, event);
371371
if (eventType == null) {
372-
eventType = ResolvableType.forClassWithGenerics(PayloadApplicationEvent.class, event.getClass());
372+
eventType = ((PayloadApplicationEvent)applicationEvent).getResolvableType();
373373
}
374374
}
375375

spring-context/src/test/java/org/springframework/context/event/AnnotationDrivenEventListenerTests.java

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -50,6 +50,7 @@
5050
import org.springframework.context.event.test.AbstractIdentifiable;
5151
import org.springframework.context.event.test.AnotherTestEvent;
5252
import org.springframework.context.event.test.EventCollector;
53+
import org.springframework.context.event.test.GenericEventPojo;
5354
import org.springframework.context.event.test.Identifiable;
5455
import org.springframework.context.event.test.TestEvent;
5556
import org.springframework.context.support.ClassPathXmlApplicationContext;
@@ -443,6 +444,30 @@ public void listenerWithGenericApplicationEvent() {
443444
this.eventCollector.assertTotalEventsCount(1);
444445
}
445446

447+
@Test
448+
public void listenerWithResolvableTypeEvent() {
449+
load(ResolvableTypeEventListener.class);
450+
ResolvableTypeEventListener listener = this.context.getBean(ResolvableTypeEventListener.class);
451+
452+
this.eventCollector.assertNoEventReceived(listener);
453+
GenericEventPojo<String> event = new GenericEventPojo<>("TEST");
454+
this.context.publishEvent(event);
455+
this.eventCollector.assertEvent(listener, event);
456+
this.eventCollector.assertTotalEventsCount(1);
457+
}
458+
459+
@Test
460+
public void listenerWithResolvableTypeEventWrongGeneric() {
461+
load(ResolvableTypeEventListener.class);
462+
ResolvableTypeEventListener listener = this.context.getBean(ResolvableTypeEventListener.class);
463+
464+
this.eventCollector.assertNoEventReceived(listener);
465+
GenericEventPojo<Long> event = new GenericEventPojo<>(123L);
466+
this.context.publishEvent(event);
467+
this.eventCollector.assertNoEventReceived(listener);
468+
this.eventCollector.assertTotalEventsCount(0);
469+
}
470+
446471
@Test
447472
public void conditionMatch() {
448473
long timestamp = System.currentTimeMillis();
@@ -791,6 +816,16 @@ public void handleString(PayloadApplicationEvent<String> event) {
791816
}
792817

793818

819+
@Component
820+
static class ResolvableTypeEventListener extends AbstractTestEventListener {
821+
822+
@EventListener
823+
public void handleString(GenericEventPojo<String> value) {
824+
collectEvent(value);
825+
}
826+
}
827+
828+
794829
@Component
795830
static class ConditionalEventListener extends TestEventListener {
796831

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2002-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.context.event.test;
18+
19+
import org.springframework.core.ResolvableType;
20+
import org.springframework.core.ResolvableTypeProvider;
21+
22+
/**
23+
* A simple POJO that implements {@link ResolvableTypeProvider}.
24+
*
25+
* @author Stephane Nicoll
26+
*/
27+
public class GenericEventPojo<T> implements ResolvableTypeProvider {
28+
private final T value;
29+
30+
public GenericEventPojo(T value) {
31+
this.value = value;
32+
}
33+
34+
@Override
35+
public ResolvableType getResolvableType() {
36+
return ResolvableType.forClassWithGenerics(getClass(), ResolvableType.forInstance(this.value));
37+
}
38+
39+
@Override
40+
public boolean equals(Object o) {
41+
if (this == o) return true;
42+
if (o == null || getClass() != o.getClass()) return false;
43+
44+
GenericEventPojo<?> that = (GenericEventPojo<?>) o;
45+
46+
return this.value.equals(that.value);
47+
}
48+
49+
@Override
50+
public int hashCode() {
51+
return this.value.hashCode();
52+
}
53+
}

0 commit comments

Comments
 (0)