Reliably detect event listener conditions on proxied beans

Issue: SPR-15678
(cherry picked from commit eb928ce)
This commit is contained in:
Juergen Hoeller
2017-07-05 13:41:56 +02:00
parent 8d743181da
commit 8ad3c958e1
2 changed files with 37 additions and 6 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -37,6 +37,7 @@ import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.core.annotation.Order;
import org.springframework.expression.EvaluationContext;
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;
@@ -87,7 +88,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);
@@ -224,7 +227,7 @@ public class ApplicationListenerMethodAdapter implements GenericApplicationListe
private void publishEvent(Object event) {
if (event != null) {
Assert.notNull(this.applicationContext, "ApplicationContext must no be null");
Assert.notNull(this.applicationContext, "ApplicationContext must not be null");
this.applicationContext.publishEvent(event);
}
}

View File

@@ -60,6 +60,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.*;
@@ -488,10 +490,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);
@@ -502,6 +514,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);
@@ -885,8 +900,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