Class EventPublicationInterceptor

java.lang.Object
org.springframework.context.event.EventPublicationInterceptor
All Implemented Interfaces:
org.aopalliance.aop.Advice, org.aopalliance.intercept.Interceptor, org.aopalliance.intercept.MethodInterceptor, org.springframework.beans.factory.Aware, org.springframework.beans.factory.InitializingBean, ApplicationEventPublisherAware

public class EventPublicationInterceptor extends Object implements org.aopalliance.intercept.MethodInterceptor, ApplicationEventPublisherAware, org.springframework.beans.factory.InitializingBean
Interceptor that publishes an ApplicationEvent to all ApplicationListeners registered with an ApplicationEventPublisher after each successful method invocation.

Note that this interceptor is capable of publishing a custom event after each successful method invocation, configured via the "applicationEventClass" property. As of 7.0.3, you can configure a factory function instead, implementing the primary EventPublicationInterceptor.ApplicationEventFactory.onSuccess(MethodInvocation, Object) method there.

By default (as of 7.0.3), this interceptor publishes a MethodFailureEvent for every exception encountered from a method invocation. This can be conveniently tracked via an ApplicationListener<MethodFailureEvent> class or an @EventListener(MethodFailureEvent.class) method. The failure event can be customized through overriding the EventPublicationInterceptor.ApplicationEventFactory.onFailure(MethodInvocation, Throwable) method.

Author:
Dmitriy Kopylenko, Juergen Hoeller, Rick Evans
See Also:
  • Constructor Details

    • EventPublicationInterceptor

      public EventPublicationInterceptor()
  • Method Details

    • setApplicationEventClass

      public void setApplicationEventClass(Class<? extends ApplicationEvent> applicationEventClass)
      Set the application event class to publish after each successful invocation.

      The event class must have a constructor with a single Object argument for the event source. The interceptor will pass in the invoked object.

      Throws:
      IllegalArgumentException - if the supplied Class is null or if it is not an ApplicationEvent subclass or if it does not expose a constructor that takes a single Object argument
      See Also:
    • setApplicationEventFactory

      public void setApplicationEventFactory(EventPublicationInterceptor.ApplicationEventFactory applicationEventFactory)
      Specify a factory function for ApplicationEvent instances built from a MethodInvocation, representing each successful method invocation.
      Since:
      7.0.3
      See Also:
    • setApplicationEventPublisher

      public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher)
      Description copied from interface: ApplicationEventPublisherAware
      Set the ApplicationEventPublisher that this object runs in.

      Invoked after population of normal bean properties but before an init callback like InitializingBean's afterPropertiesSet or a custom init-method. Invoked before ApplicationContextAware's setApplicationContext.

      Specified by:
      setApplicationEventPublisher in interface ApplicationEventPublisherAware
      Parameters:
      applicationEventPublisher - event publisher to be used by this object
    • afterPropertiesSet

      public void afterPropertiesSet() throws Exception
      Specified by:
      afterPropertiesSet in interface org.springframework.beans.factory.InitializingBean
      Throws:
      Exception
    • invoke

      public @Nullable Object invoke(org.aopalliance.intercept.MethodInvocation invocation) throws Throwable
      Specified by:
      invoke in interface org.aopalliance.intercept.MethodInterceptor
      Throws:
      Throwable