Polish annotation-driven event listener support

This commit is contained in:
Sam Brannen
2019-03-14 23:13:41 +01:00
parent 0b53dbf38f
commit fef43048b2
5 changed files with 25 additions and 21 deletions

View File

@@ -49,7 +49,7 @@ import org.springframework.util.StringUtils;
*
* <p>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};
}

View File

@@ -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.
* <p>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) {

View File

@@ -106,21 +106,24 @@ public @interface EventListener {
Class<?>[] classes() default {};
/**
* Spring Expression Language (SpEL) attribute used for making the
* event handling conditional.
* <p>Default is {@code ""}, meaning the event is always handled.
* <p>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.
* <p>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"}.
* <p>The default expression is {@code ""}, meaning the event is always handled.
* <p>The SpEL expression will be evaluated against a dedicated context that
* provides the following metadata:
* <ul>
* <li>{@code #root.event} or {@code event} for references to the
* {@link ApplicationEvent}</li>
* <li>{@code #root.args} or {@code args} for references to the method
* arguments array</li>
* <li>Method arguments can be accessed by index. For instance the
* first argument can be accessed via {@code #root.args[0]}, {@code args[0]},
* {@code #a0}, or {@code #p0}. Arguments can also be accessed by name (with
* a preceding hash tag) if that information is available in the compiled
* byte code.</li>
* <li>Method arguments can be accessed by index. For example, the first
* argument can be accessed via {@code #root.args[0]}, {@code args[0]},
* {@code #a0}, or {@code #p0}.</li>
* <li>Method arguments can be accessed by name (with a preceding hash tag)
* if parameter names are available in the compiled byte code.</li>
* </ul>
*/
String condition() default "";