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:
  • Field Summary

    Fields inherited from interface org.springframework.core.Ordered

    HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
  • Method Summary

    Modifier and Type
    Method
    Description
    default int
     
    void
    intercept(org.springframework.context.ApplicationEvent event, org.springframework.core.ResolvableType eventType, ApplicationEventInterceptorChain chain)
    Intercept the specified ApplicationEvent 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 specified ApplicationEvent 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:
      
       public 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);
           }
       }
       
      Short-circuiting: To prevent further processing of an event, simply skip calling 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; never null
      eventType - the resolved type of the event, useful for filtering or conditional logic
      chain - the interceptor chain to continue processing if desired
    • getOrder

      default int getOrder()
      Specified by:
      getOrder in interface org.springframework.core.Ordered