Clean • Professional
Spring Boot applications often need cross-cutting features like logging, security, performance monitoring, or authentication. These features are not part of the core business logic but need to be applied consistently across the application. Spring Boot provides three main mechanisms to handle these: Filters, Interceptors, and AOP (Aspect-Oriented Programming).
Each operates at a different level in the request-processing flow. Understanding how and when to use them is essential for building clean, maintainable, and professional Spring Boot applications.
A Filter is a low-level component that intercepts all HTTP requests and responses at the Servlet level, before reaching Spring MVC controllers. It is part of the Servlet API, so it works independently of Spring MVC.
Use Cases:
Example: Logging Filter
@Component
publicclassLoggingFilterextendsOncePerRequestFilter {
@Override
protectedvoiddoFilterInternal(HttpServletRequest request,
HttpServletResponse response,
FilterChain filterChain)
throws ServletException, IOException {
System.out.println("Incoming request: " + request.getRequestURI());
filterChain.doFilter(request, response);// Continue processing
System.out.println("Outgoing response");
}
}
Key Points:
An Interceptor is a Spring MVC feature that runs before and after controller methods. It works after Filters but before the controller executes and can also act after the controller returns.
Use Cases:
Example: Authorization Interceptor
@Component
publicclassAuthInterceptorimplementsHandlerInterceptor {
@Override
publicbooleanpreHandle(HttpServletRequest request,
HttpServletResponse response,
Object handler) {
Stringtoken= request.getHeader("Authorization");
if (token ==null) {
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
returnfalse;// Stop execution
}
returntrue;// Continue to controller
}
@Override
publicvoidafterCompletion(HttpServletRequest request,
HttpServletResponse response,
Object handler, Exception ex) {
System.out.println("Request completed for URI: " + request.getRequestURI());
}
}
Registering Interceptor
@Configuration
publicclassWebConfigimplementsWebMvcConfigurer {
@Override
publicvoidaddInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(newAuthInterceptor());
}
}
Key Points:
Aspect-Oriented Programming (AOP) is a method-level programming technique in Spring that allows you to implement cross-cutting concerns across the application. It can intercept any Spring bean method, including controllers, services, or repositories.
Use Cases:
Example: Logging Aspect
@Aspect
@Component
publicclassLoggingAspect {
@Before("execution(* com.example.service.*.*(..))")
publicvoidlogBefore(JoinPoint joinPoint) {
System.out.println("Calling method: " + joinPoint.getSignature());
}
@AfterReturning(pointcut = "execution(* com.example.service.*.*(..))", returning = "result")
publicvoidlogAfter(JoinPoint joinPoint, Object result) {
System.out.println("Method returned: " + result);
}
@Around("execution(* com.example.service.*.*(..))")
public ObjectlogAround(ProceedingJoinPoint joinPoint)throws Throwable {
System.out.println("Around before: " + joinPoint.getSignature());
Objectresult= joinPoint.proceed();
System.out.println("Around after: " + joinPoint.getSignature());
return result;
}
}
Key Points:
These three mechanisms operate at different layers of a Spring Boot application:

Summary of Layers:
| Feature | Filters | Interceptors | AOP (Aspect-Oriented Programming) |
|---|---|---|---|
| Layer | Servlet layer (before reaching Spring MVC) | Spring MVC layer (Controller level) | Application/service layer (any Spring bean method) |
| Interface | javax.servlet.Filter | HandlerInterceptor | @Aspect with @Before/@After/@Around |
| Execution Timing | Before & after request reaches the DispatcherServlet | Before & after controller method execution | Before, after, or around any matched method execution |
| Use Cases | Logging, CORS, compression, authentication, request/response manipulation | Authorization, metrics, request modification, pre/post processing | Logging, transactions, security, performance monitoring, caching |
| Scope | Works for all requests (even static resources) | Only for Spring MVC requests | Only where pointcuts are defined (can be fine-grained) |
| Spring Boot Example | Custom Filter bean | Implement HandlerInterceptor + register in WebMvcConfigurer | @Aspect with pointcuts for selected methods |