Reliably detect event listener conditions on proxied beans
Issue: SPR-15678
This commit is contained in:
parent
a6d616d844
commit
eb928ce456
|
@ -38,6 +38,7 @@ import org.springframework.core.annotation.Order;
|
|||
import org.springframework.expression.EvaluationContext;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
@ -91,7 +92,9 @@ public class ApplicationListenerMethodAdapter implements GenericApplicationListe
|
|||
this.targetClass = targetClass;
|
||||
this.bridgedMethod = BridgeMethodResolver.findBridgedMethod(method);
|
||||
|
||||
EventListener ann = AnnotatedElementUtils.findMergedAnnotation(method, EventListener.class);
|
||||
Method targetMethod = ClassUtils.getMostSpecificMethod(method, targetClass);
|
||||
EventListener ann = AnnotatedElementUtils.findMergedAnnotation(targetMethod, EventListener.class);
|
||||
|
||||
this.declaredEventTypes = resolveDeclaredEventTypes(method, ann);
|
||||
this.condition = (ann != null ? ann.condition() : null);
|
||||
this.order = resolveOrder(method);
|
||||
|
|
|
@ -63,6 +63,8 @@ import org.springframework.scheduling.annotation.Async;
|
|||
import org.springframework.scheduling.annotation.EnableAsync;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.validation.beanvalidation.MethodValidationPostProcessor;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
@ -491,10 +493,20 @@ public class AnnotationDrivenEventListenerTests {
|
|||
|
||||
@Test
|
||||
public void conditionMatch() {
|
||||
validateConditionMatch(ConditionalEventListener.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void conditionMatchWithProxy() {
|
||||
validateConditionMatch(ConditionalEventListener.class, MethodValidationPostProcessor.class);
|
||||
}
|
||||
|
||||
private void validateConditionMatch(Class<?>... classes) {
|
||||
long timestamp = System.currentTimeMillis();
|
||||
load(ConditionalEventListener.class);
|
||||
load(classes);
|
||||
TestEvent event = new TestEvent(this, "OK");
|
||||
TestEventListener listener = this.context.getBean(ConditionalEventListener.class);
|
||||
|
||||
ConditionalEventInterface listener = this.context.getBean(ConditionalEventInterface.class);
|
||||
this.eventCollector.assertNoEventReceived(listener);
|
||||
|
||||
this.context.publishEvent(event);
|
||||
|
@ -505,6 +517,9 @@ public class AnnotationDrivenEventListenerTests {
|
|||
this.eventCollector.assertEvent(listener, event, "OK");
|
||||
this.eventCollector.assertTotalEventsCount(2);
|
||||
|
||||
this.context.publishEvent("NOT OK");
|
||||
this.eventCollector.assertTotalEventsCount(2);
|
||||
|
||||
this.context.publishEvent(timestamp);
|
||||
this.eventCollector.assertEvent(listener, event, "OK", timestamp);
|
||||
this.eventCollector.assertTotalEventsCount(3);
|
||||
|
@ -896,8 +911,21 @@ public class AnnotationDrivenEventListenerTests {
|
|||
}
|
||||
|
||||
|
||||
interface ConditionalEventInterface extends Identifiable {
|
||||
|
||||
void handle(TestEvent event);
|
||||
|
||||
void handleString(String payload);
|
||||
|
||||
void handleTimestamp(Long timestamp);
|
||||
|
||||
void handleRatio(Double ratio);
|
||||
}
|
||||
|
||||
|
||||
@Component
|
||||
static class ConditionalEventListener extends TestEventListener {
|
||||
@Validated
|
||||
static class ConditionalEventListener extends TestEventListener implements ConditionalEventInterface {
|
||||
|
||||
@EventListener(condition = "'OK'.equals(#root.event.msg)")
|
||||
@Override
|
||||
|
|
Loading…
Reference in New Issue