Support for bean refs in event SpEL condition

Issue: SPR-13814
master
Stephane Nicoll 9 years ago
parent 9b5e47026c
commit d444ef4871
  1. 2
      spring-context/src/main/java/org/springframework/context/event/ApplicationListenerMethodAdapter.java
  2. 11
      spring-context/src/main/java/org/springframework/context/event/EventExpressionEvaluator.java
  3. 25
      spring-context/src/test/java/org/springframework/context/event/AnnotationDrivenEventListenerTests.java

@ -207,7 +207,7 @@ public class ApplicationListenerMethodAdapter implements GenericApplicationListe
if (StringUtils.hasText(condition)) { if (StringUtils.hasText(condition)) {
Assert.notNull(this.evaluator, "EventExpressionEvaluator must no be null"); Assert.notNull(this.evaluator, "EventExpressionEvaluator must no be null");
EvaluationContext evaluationContext = this.evaluator.createEvaluationContext( EvaluationContext evaluationContext = this.evaluator.createEvaluationContext(
event, this.targetClass, this.method, args); event, this.targetClass, this.method, args, this.applicationContext);
return this.evaluator.condition(condition, this.methodKey, evaluationContext); return this.evaluator.condition(condition, this.methodKey, evaluationContext);
} }
return true; return true;

@ -21,8 +21,10 @@ import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import org.springframework.aop.support.AopUtils; import org.springframework.aop.support.AopUtils;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationEvent;
import org.springframework.context.expression.AnnotatedElementKey; import org.springframework.context.expression.AnnotatedElementKey;
import org.springframework.context.expression.BeanFactoryResolver;
import org.springframework.context.expression.CachedExpressionEvaluator; import org.springframework.context.expression.CachedExpressionEvaluator;
import org.springframework.context.expression.MethodBasedEvaluationContext; import org.springframework.context.expression.MethodBasedEvaluationContext;
import org.springframework.core.DefaultParameterNameDiscoverer; import org.springframework.core.DefaultParameterNameDiscoverer;
@ -52,11 +54,16 @@ class EventExpressionEvaluator extends CachedExpressionEvaluator {
* on the specified method. * on the specified method.
*/ */
public EvaluationContext createEvaluationContext(ApplicationEvent event, Class<?> targetClass, public EvaluationContext createEvaluationContext(ApplicationEvent event, Class<?> targetClass,
Method method, Object[] args) { Method method, Object[] args, BeanFactory beanFactory) {
Method targetMethod = getTargetMethod(targetClass, method); Method targetMethod = getTargetMethod(targetClass, method);
EventExpressionRootObject root = new EventExpressionRootObject(event, args); EventExpressionRootObject root = new EventExpressionRootObject(event, args);
return new MethodBasedEvaluationContext(root, targetMethod, args, this.paramNameDiscoverer); MethodBasedEvaluationContext evaluationContext =
new MethodBasedEvaluationContext(root, targetMethod, args, this.paramNameDiscoverer);
if (beanFactory != null) {
evaluationContext.setBeanResolver(new BeanFactoryResolver(beanFactory));
}
return evaluationContext;
} }
/** /**

@ -462,6 +462,10 @@ public class AnnotationDrivenEventListenerTests {
this.context.publishEvent(timestamp); this.context.publishEvent(timestamp);
this.eventCollector.assertEvent(listener, event, "OK", timestamp); this.eventCollector.assertEvent(listener, event, "OK", timestamp);
this.eventCollector.assertTotalEventsCount(3); this.eventCollector.assertTotalEventsCount(3);
this.context.publishEvent(42d);
this.eventCollector.assertEvent(listener, event, "OK", timestamp, 42d);
this.eventCollector.assertTotalEventsCount(4);
} }
@Test @Test
@ -483,6 +487,10 @@ public class AnnotationDrivenEventListenerTests {
this.context.publishEvent(maxLong); this.context.publishEvent(maxLong);
this.eventCollector.assertNoEventReceived(listener); this.eventCollector.assertNoEventReceived(listener);
this.eventCollector.assertTotalEventsCount(0); this.eventCollector.assertTotalEventsCount(0);
this.context.publishEvent(24d);
this.eventCollector.assertNoEventReceived(listener);
this.eventCollector.assertTotalEventsCount(0);
} }
@Test @Test
@ -534,6 +542,18 @@ public class AnnotationDrivenEventListenerTests {
public CountDownLatch testCountDownLatch() { public CountDownLatch testCountDownLatch() {
return new CountDownLatch(1); return new CountDownLatch(1);
} }
@Bean
public TestConditionEvaluator conditionEvaluator() {
return new TestConditionEvaluator();
}
static class TestConditionEvaluator {
public boolean valid(Double ratio) {
return new Double(42).equals(ratio);
}
}
} }
@ -810,6 +830,11 @@ public class AnnotationDrivenEventListenerTests {
public void handleTimestamp(Long timestamp) { public void handleTimestamp(Long timestamp) {
collectEvent(timestamp); collectEvent(timestamp);
} }
@EventListener(condition = "@conditionEvaluator.valid(#p0)")
public void handleRatio(Double ratio) {
collectEvent(ratio);
}
} }

Loading…
Cancel
Save