Interface ApplicationEventInterceptor
- All Superinterfaces:
org.springframework.core.Ordered
public interface ApplicationEventInterceptor
extends org.springframework.core.Ordered
An interface used to intercept
application events
allowing for pre-processing,
post-processing, or even prevention of event propagation through the
interceptor chain
.
Interceptors can be used to implement cross-cutting concerns such as logging, security checks, or performance monitoring around the application event handling process.
Example Usage
public class LoggingInterceptor implements ApplicationEventInterceptor {
@Override
public void intercept(ApplicationEvent event, ResolvableType eventType, ApplicationEventInterceptorChain chain) {
System.out.println("Before handling event: " + event);
chain.intercept(event, eventType); // Continue the interceptor chain
System.out.println("After handling event: " + event);
}
}
Interceptors are typically ordered using the Ordered
interface or
@Order
annotation to control the order in which they are applied.
- Since:
- 1.0.0
- Author:
- Mercy
- See Also:
-
ApplicationEvent
ApplicationEventInterceptorChain
Ordered
-
Field Summary
Fields inherited from interface org.springframework.core.Ordered
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
-
Method Summary
Modifier and TypeMethodDescriptiondefault int
getOrder()
void
intercept
(org.springframework.context.ApplicationEvent event, org.springframework.core.ResolvableType eventType, ApplicationEventInterceptorChain chain) Intercept the specifiedApplicationEvent
with its resolved type, allowing custom pre-processing, post-processing, or short-circuiting the event propagation by not invoking the next interceptor in the chain.
-
Method Details
-
intercept
void intercept(org.springframework.context.ApplicationEvent event, org.springframework.core.ResolvableType eventType, ApplicationEventInterceptorChain chain) Intercept the specifiedApplicationEvent
with its resolved type, allowing custom pre-processing, post-processing, or short-circuiting the event propagation by not invoking the next interceptor in the chain.Example Usage
Implementing a simple logging interceptor:
Short-circuiting: To prevent further processing of an event, simply skip callingpublic class SampleInterceptor implements ApplicationEventInterceptor { public void intercept(ApplicationEvent event, ResolvableType eventType, ApplicationEventInterceptorChain chain) { System.out.println("Intercepting event: " + event); // Proceed to the next interceptor in the chain chain.intercept(event, eventType); System.out.println("Finished handling event: " + event); } }
ApplicationEventInterceptorChain.intercept(ApplicationEvent, ResolvableType)
:public class SilentEventInterceptor implements ApplicationEventInterceptor { public void intercept(ApplicationEvent event, ResolvableType eventType, ApplicationEventInterceptorChain chain) { // Skip calling chain.proceed() to prevent event propagation System.out.println("Event blocked: " + event); } }
- Parameters:
event
- the event being intercepted; nevernull
eventType
- the resolved type of the event, useful for filtering or conditional logicchain
- the interceptor chain to continue processing if desired
-
getOrder
default int getOrder()- Specified by:
getOrder
in interfaceorg.springframework.core.Ordered
-