From fef43048b2f3e2c64cf14aed023a4c04b926334e Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Thu, 14 Mar 2019 23:13:41 +0100 Subject: [PATCH] Polish annotation-driven event listener support --- .../ApplicationListenerMethodAdapter.java | 6 ++--- .../event/EventExpressionEvaluator.java | 9 ++++---- .../context/event/EventListener.java | 23 +++++++++++-------- .../test/context/event/TestContextEvent.java | 4 ++-- ...TestExecutionListenerIntegrationTests.java | 4 ++-- 5 files changed, 25 insertions(+), 21 deletions(-) diff --git a/spring-context/src/main/java/org/springframework/context/event/ApplicationListenerMethodAdapter.java b/spring-context/src/main/java/org/springframework/context/event/ApplicationListenerMethodAdapter.java index e81ff3633c..3e0f069114 100644 --- a/spring-context/src/main/java/org/springframework/context/event/ApplicationListenerMethodAdapter.java +++ b/spring-context/src/main/java/org/springframework/context/event/ApplicationListenerMethodAdapter.java @@ -49,7 +49,7 @@ import org.springframework.util.StringUtils; * *

Delegates to {@link #processEvent(ApplicationEvent)} to give sub-classes * a chance to deviate from the default. Unwraps the content of a - * {@link PayloadApplicationEvent} if necessary to allow method declaration + * {@link PayloadApplicationEvent} if necessary to allow a method declaration * to define any arbitrary event type. If a condition is defined, it is * evaluated prior to invoking the underlying method. * @@ -171,7 +171,7 @@ public class ApplicationListenerMethodAdapter implements GenericApplicationListe /** * Process the specified {@link ApplicationEvent}, checking if the condition - * match and handling non-null result, if any. + * matches and handling a non-null result, if any. */ public void processEvent(ApplicationEvent event) { Object[] args = resolveArguments(event); @@ -204,7 +204,7 @@ public class ApplicationListenerMethodAdapter implements GenericApplicationListe Class declaredEventClass = declaredEventType.toClass(); if (!ApplicationEvent.class.isAssignableFrom(declaredEventClass) && event instanceof PayloadApplicationEvent) { - Object payload = ((PayloadApplicationEvent) event).getPayload(); + Object payload = ((PayloadApplicationEvent) event).getPayload(); if (declaredEventClass.isInstance(payload)) { return new Object[] {payload}; } diff --git a/spring-context/src/main/java/org/springframework/context/event/EventExpressionEvaluator.java b/spring-context/src/main/java/org/springframework/context/event/EventExpressionEvaluator.java index 32b1dd16c3..8b24546baa 100644 --- a/spring-context/src/main/java/org/springframework/context/event/EventExpressionEvaluator.java +++ b/spring-context/src/main/java/org/springframework/context/event/EventExpressionEvaluator.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 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. @@ -30,8 +30,8 @@ import org.springframework.expression.Expression; import org.springframework.lang.Nullable; /** - * Utility class handling the SpEL expression parsing. Meant to be used - * as a reusable, thread-safe component. + * Utility class for handling SpEL expression parsing for application events. + *

Meant to be used as a reusable, thread-safe component. * * @author Stephane Nicoll * @since 4.2 @@ -43,7 +43,8 @@ class EventExpressionEvaluator extends CachedExpressionEvaluator { /** - * Specify if the condition defined by the specified expression matches. + * Determine if the condition defined by the specified expression evaluates + * to {@code true}. */ public boolean condition(String conditionExpression, ApplicationEvent event, Method targetMethod, AnnotatedElementKey methodKey, Object[] args, @Nullable BeanFactory beanFactory) { diff --git a/spring-context/src/main/java/org/springframework/context/event/EventListener.java b/spring-context/src/main/java/org/springframework/context/event/EventListener.java index 46835e14de..2fc237b0ec 100644 --- a/spring-context/src/main/java/org/springframework/context/event/EventListener.java +++ b/spring-context/src/main/java/org/springframework/context/event/EventListener.java @@ -106,21 +106,24 @@ public @interface EventListener { Class[] classes() default {}; /** - * Spring Expression Language (SpEL) attribute used for making the - * event handling conditional. - *

Default is {@code ""}, meaning the event is always handled. - *

The SpEL expression evaluates against a dedicated context that - * provides the following meta-data: + * Spring Expression Language (SpEL) expression used for making the event + * handling conditional. + *

The event will be handled if the expression evaluates to boolean + * {@code true} or one of the following strings: {@code "true"}, {@code "on"}, + * {@code "yes"}, or {@code "1"}. + *

The default expression is {@code ""}, meaning the event is always handled. + *

The SpEL expression will be evaluated against a dedicated context that + * provides the following metadata: *

*/ String condition() default ""; diff --git a/spring-test/src/main/java/org/springframework/test/context/event/TestContextEvent.java b/spring-test/src/main/java/org/springframework/test/context/event/TestContextEvent.java index e25c14732c..0561db90fd 100644 --- a/spring-test/src/main/java/org/springframework/test/context/event/TestContextEvent.java +++ b/spring-test/src/main/java/org/springframework/test/context/event/TestContextEvent.java @@ -50,8 +50,8 @@ public abstract class TestContextEvent extends ApplicationEvent { /** * Alias for {@link #getSource()}. - *

This method may be favored over {@code getSource()} to improve readability - * in SpEL expressions for event processing + *

This method may be favored over {@code getSource()} — for example, + * to improve readability in SpEL expressions for event processing * {@linkplain org.springframework.context.event.EventListener#condition conditions}. * @return the {@code TestContext} associated with this event (never {@code null}) * @see #getSource() diff --git a/spring-test/src/test/java/org/springframework/test/context/event/EventPublishingTestExecutionListenerIntegrationTests.java b/spring-test/src/test/java/org/springframework/test/context/event/EventPublishingTestExecutionListenerIntegrationTests.java index 3f527f5a2a..980e58c5cd 100644 --- a/spring-test/src/test/java/org/springframework/test/context/event/EventPublishingTestExecutionListenerIntegrationTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/event/EventPublishingTestExecutionListenerIntegrationTests.java @@ -272,12 +272,12 @@ public class EventPublishingTestExecutionListenerIntegrationTests { throw new RuntimeException("Boom!"); } - @BeforeTestExecution + @BeforeTestExecution("'yes'") public void beforeTestExecution(BeforeTestExecutionEvent e) throws Exception { listener().beforeTestExecution(e.getSource()); } - @AfterTestExecution + @AfterTestExecution("'1'") public void afterTestExecution(AfterTestExecutionEvent e) throws Exception { listener().afterTestExecution(e.getSource()); }