Class ApplicationListenerMethodAdapter

java.lang.Object
org.springframework.context.event.ApplicationListenerMethodAdapter
All Implemented Interfaces:
EventListener, ApplicationListener<ApplicationEvent>, GenericApplicationListener, SmartApplicationListener, org.springframework.core.Ordered

public class ApplicationListenerMethodAdapter extends Object implements GenericApplicationListener
GenericApplicationListener adapter that delegates the processing of an event to an EventListener annotated method.

Delegates to processEvent(ApplicationEvent) to give subclasses a chance to deviate from the default. Unwraps the content of a 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.

Since:
4.2
Author:
Stephane Nicoll, Juergen Hoeller, Sam Brannen, Sebastien Deleuze
  • Field Details

    • logger

      protected final org.apache.commons.logging.Log logger
  • Constructor Details

    • ApplicationListenerMethodAdapter

      public ApplicationListenerMethodAdapter(String beanName, Class<?> targetClass, Method method)
      Construct a new ApplicationListenerMethodAdapter.
      Parameters:
      beanName - the name of the bean to invoke the listener method on
      targetClass - the target class that the method is declared on
      method - the listener method to invoke
  • Method Details

    • onApplicationEvent

      public void onApplicationEvent(ApplicationEvent event)
      Description copied from interface: ApplicationListener
      Handle an application event.
      Specified by:
      onApplicationEvent in interface ApplicationListener<ApplicationEvent>
      Parameters:
      event - the event to respond to
    • supportsEventType

      public boolean supportsEventType(org.springframework.core.ResolvableType eventType)
      Description copied from interface: GenericApplicationListener
      Determine whether this listener actually supports the given event type.
      Specified by:
      supportsEventType in interface GenericApplicationListener
      Parameters:
      eventType - the event type (never null)
    • supportsSourceType

      public boolean supportsSourceType(@Nullable Class<?> sourceType)
      Description copied from interface: SmartApplicationListener
      Determine whether this listener actually supports the given source type.

      The default implementation always returns true.

      Specified by:
      supportsSourceType in interface SmartApplicationListener
      Parameters:
      sourceType - the source type, or null if no source
    • getOrder

      public int getOrder()
      Description copied from interface: SmartApplicationListener
      Determine this listener's order in a set of listeners for the same event.

      The default implementation returns Ordered.LOWEST_PRECEDENCE.

      Specified by:
      getOrder in interface org.springframework.core.Ordered
      Specified by:
      getOrder in interface SmartApplicationListener
    • getListenerId

      public String getListenerId()
      Description copied from interface: SmartApplicationListener
      Return an optional identifier for the listener.

      The default value is an empty String.

      Specified by:
      getListenerId in interface SmartApplicationListener
      See Also:
    • getDefaultListenerId

      protected String getDefaultListenerId()
      Determine the default id for the target listener, to be applied in case of no annotation-specified id value.

      The default implementation builds a method name with parameter types.

      Since:
      5.3.5
      See Also:
    • processEvent

      public void processEvent(ApplicationEvent event)
      Process the specified ApplicationEvent, checking if the condition matches and handling a non-null result, if any.
      Parameters:
      event - the event to process through the listener method
    • shouldHandle

      public boolean shouldHandle(ApplicationEvent event)
      Determine whether the listener method would actually handle the given event, checking if the condition matches.
      Parameters:
      event - the event to process through the listener method
      Since:
      6.1
    • resolveArguments

      @Nullable protected Object[] resolveArguments(ApplicationEvent event)
      Resolve the method arguments to use for the specified 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.

    • handleResult

      protected void handleResult(Object result)
    • handleAsyncError

      protected void handleAsyncError(Throwable t)
    • doInvoke

      @Nullable protected Object doInvoke(Object... args)
      Invoke the event listener method with the given argument values.
    • getTargetBean

      protected Object getTargetBean()
      Return the target bean instance to use.
    • getTargetMethod

      protected Method getTargetMethod()
      Return the target listener method.
      Since:
      5.3
    • getCondition

      @Nullable protected String getCondition()
      Return the condition to use.

      Matches the condition attribute of the EventListener annotation or any matching attribute on a composed annotation that is meta-annotated with @EventListener.

    • getDetailedErrorMessage

      protected String getDetailedErrorMessage(Object bean, String message)
      Add additional details such as the bean type and method signature to the given error message.
      Parameters:
      message - error message to append the HandlerMethod details to
    • toString

      public String toString()
      Overrides:
      toString in class Object