public class ApplicationListenerMethodAdapter extends java.lang.Object implements GenericApplicationListener
GenericApplicationListener
adapter that delegates the processing of
an event to an EventListener
annotated method.
Delegates to processEvent(ApplicationEvent)
to give sub-classes
a chance to deviate from the default. Unwraps the content of a
PayloadApplicationEvent
if necessary to allow method declaration
to define any arbitrary event type. If a condition is defined, it is
evaluated prior to invoking the underlying method.
Modifier and Type | Field and Description |
---|---|
private ApplicationContext |
applicationContext |
private java.lang.String |
beanName |
private java.lang.reflect.Method |
bridgedMethod |
private java.lang.String |
condition |
private java.util.List<ResolvableType> |
declaredEventTypes |
private EventExpressionEvaluator |
evaluator |
protected Log |
logger |
private java.lang.reflect.Method |
method |
private AnnotatedElementKey |
methodKey |
private int |
order |
private java.lang.Class<?> |
targetClass |
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
Constructor and Description |
---|
ApplicationListenerMethodAdapter(java.lang.String beanName,
java.lang.Class<?> targetClass,
java.lang.reflect.Method method) |
Modifier and Type | Method and Description |
---|---|
private void |
assertTargetBean(java.lang.reflect.Method method,
java.lang.Object targetBean,
java.lang.Object[] args)
Assert that the target bean class is an instance of the class where the given
method is declared.
|
protected java.lang.Object |
doInvoke(java.lang.Object... args)
Invoke the event listener method with the given argument values.
|
protected java.lang.String |
getCondition()
Return the condition to use.
|
protected java.lang.String |
getDetailedErrorMessage(java.lang.Object bean,
java.lang.String message)
Add additional details such as the bean type and method signature to
the given error message.
|
private java.lang.String |
getInvocationErrorMessage(java.lang.Object bean,
java.lang.String message,
java.lang.Object[] resolvedArgs) |
int |
getOrder()
Get the order value of this object.
|
private ResolvableType |
getResolvableType(ApplicationEvent event) |
protected java.lang.Object |
getTargetBean()
Return the target bean instance to use.
|
protected void |
handleResult(java.lang.Object result) |
(package private) void |
init(ApplicationContext applicationContext,
EventExpressionEvaluator evaluator)
Initialize this instance.
|
void |
onApplicationEvent(ApplicationEvent event)
Handle an application event.
|
void |
processEvent(ApplicationEvent event)
Process the specified
ApplicationEvent , checking if the condition
match and handling non-null result, if any. |
private void |
publishEvent(java.lang.Object event) |
protected java.lang.Object[] |
resolveArguments(ApplicationEvent event)
Resolve the method arguments to use for the specified
ApplicationEvent . |
private java.util.List<ResolvableType> |
resolveDeclaredEventTypes(java.lang.reflect.Method method,
EventListener ann) |
private int |
resolveOrder(java.lang.reflect.Method method) |
private boolean |
shouldHandle(ApplicationEvent event,
java.lang.Object[] args) |
boolean |
supportsEventType(ResolvableType eventType)
Determine whether this listener actually supports the given event type.
|
boolean |
supportsSourceType(java.lang.Class<?> sourceType)
Determine whether this listener actually supports the given source type.
|
java.lang.String |
toString() |
protected final Log logger
private final java.lang.String beanName
private final java.lang.reflect.Method method
private final java.lang.Class<?> targetClass
private final java.lang.reflect.Method bridgedMethod
private final java.util.List<ResolvableType> declaredEventTypes
private final java.lang.String condition
private final int order
private final AnnotatedElementKey methodKey
private ApplicationContext applicationContext
private EventExpressionEvaluator evaluator
public ApplicationListenerMethodAdapter(java.lang.String beanName, java.lang.Class<?> targetClass, java.lang.reflect.Method method)
private java.util.List<ResolvableType> resolveDeclaredEventTypes(java.lang.reflect.Method method, EventListener ann)
private int resolveOrder(java.lang.reflect.Method method)
void init(ApplicationContext applicationContext, EventExpressionEvaluator evaluator)
public void onApplicationEvent(ApplicationEvent event)
ApplicationListener
onApplicationEvent
in interface ApplicationListener<ApplicationEvent>
event
- the event to respond topublic boolean supportsEventType(ResolvableType eventType)
GenericApplicationListener
supportsEventType
in interface GenericApplicationListener
public boolean supportsSourceType(java.lang.Class<?> sourceType)
GenericApplicationListener
supportsSourceType
in interface GenericApplicationListener
public int getOrder()
Ordered
Higher values are interpreted as lower priority. As a consequence,
the object with the lowest value has the highest priority (somewhat
analogous to Servlet load-on-startup
values).
Same order values will result in arbitrary sort positions for the affected objects.
getOrder
in interface Ordered
Ordered.HIGHEST_PRECEDENCE
,
Ordered.LOWEST_PRECEDENCE
public void processEvent(ApplicationEvent event)
ApplicationEvent
, checking if the condition
match and handling non-null result, if any.protected java.lang.Object[] resolveArguments(ApplicationEvent event)
ApplicationEvent
.
These arguments will be used to invoke the method handled by this instance. Can
return null
to indicate that no suitable arguments could be resolved and
therefore the method should not be invoked at all for the specified event.
protected void handleResult(java.lang.Object result)
private void publishEvent(java.lang.Object event)
private boolean shouldHandle(ApplicationEvent event, java.lang.Object[] args)
protected java.lang.Object doInvoke(java.lang.Object... args)
protected java.lang.Object getTargetBean()
protected java.lang.String getCondition()
Matches the condition
attribute of the EventListener
annotation or any matching attribute on a composed annotation that
is meta-annotated with @EventListener
.
protected java.lang.String getDetailedErrorMessage(java.lang.Object bean, java.lang.String message)
message
- error message to append the HandlerMethod details toprivate void assertTargetBean(java.lang.reflect.Method method, java.lang.Object targetBean, java.lang.Object[] args)
private java.lang.String getInvocationErrorMessage(java.lang.Object bean, java.lang.String message, java.lang.Object[] resolvedArgs)
private ResolvableType getResolvableType(ApplicationEvent event)
public java.lang.String toString()
toString
in class java.lang.Object