Class EventPublicationInterceptor
- All Implemented Interfaces:
Advice, Interceptor, MethodInterceptor, Aware, InitializingBean, ApplicationEventPublisherAware
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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceCallback interface for building anApplicationEventafter a method invocation. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidInvoked by the containingBeanFactoryafter it has set all bean properties and satisfiedBeanFactoryAware,ApplicationContextAwareetc.invoke(MethodInvocation invocation) Implement this method to perform extra treatments before and after the invocation.voidsetApplicationEventClass(Class<? extends ApplicationEvent> applicationEventClass) Set the application event class to publish after each successful invocation.voidsetApplicationEventFactory(EventPublicationInterceptor.ApplicationEventFactory applicationEventFactory) Specify a factory function forApplicationEventinstances built from aMethodInvocation, representing each successful method invocation.voidsetApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) Set the ApplicationEventPublisher that this object runs in.
-
Constructor Details
-
EventPublicationInterceptor
public EventPublicationInterceptor()
-
-
Method Details
-
setApplicationEventClass
Set the application event class to publish after each successful invocation.The event class must have a constructor with a single
Objectargument for the event source. The interceptor will pass in the invoked object.- Throws:
IllegalArgumentException- if the suppliedClassisnullor if it is not anApplicationEventsubclass or if it does not expose a constructor that takes a singleObjectargument- See Also:
-
setApplicationEventFactory
public void setApplicationEventFactory(EventPublicationInterceptor.ApplicationEventFactory applicationEventFactory) Specify a factory function forApplicationEventinstances built from aMethodInvocation, representing each successful method invocation.- Since:
- 7.0.3
- See Also:
-
setApplicationEventPublisher
Description copied from interface:ApplicationEventPublisherAwareSet 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:
setApplicationEventPublisherin interfaceApplicationEventPublisherAware- Parameters:
applicationEventPublisher- event publisher to be used by this object
-
afterPropertiesSet
Description copied from interface:InitializingBeanInvoked by the containingBeanFactoryafter it has set all bean properties and satisfiedBeanFactoryAware,ApplicationContextAwareetc.This method allows the bean instance to perform validation of its overall configuration and final initialization when all bean properties have been set.
- Specified by:
afterPropertiesSetin interfaceInitializingBean- Throws:
Exception- in the event of misconfiguration (such as failure to set an essential property) or if initialization fails for any other reason
-
invoke
Description copied from interface:MethodInterceptorImplement this method to perform extra treatments before and after the invocation. Polite implementations would certainly like to invokeJoinpoint.proceed().- Specified by:
invokein interfaceMethodInterceptor- Parameters:
invocation- the method invocation joinpoint- Returns:
- the result of the call to
Joinpoint.proceed(); might be intercepted by the interceptor - Throws:
Throwable- if the interceptors or the target object throws an exception
-