Member-only story
Comparison of Spring Boot Filters, Interceptors and Listeners
Overview
Filters, Interceptors, and Listeners are three common components of the Spring Boot framework whose primary function is to process and enhance requests and responses before they reach the controller or after they leave the controller. Although their roles are similar, the implementation mechanism, scope and usage differ between them.
Filter (Filter)
Filter is a component provided by the Servlet container, mainly used for pre-processing HTTP requests and post-processing HTTP responses. Filters are a global component that can intercept all requests and responses, including static resource files (such as HTML, CSS and JS files) and dynamic resource files (such as JSPs and Servlets). In Spring Boot, it can be registered using FilterRegistrationBean.
Interceptor
Interceptor is a component provided by the Spring Framework and is mainly used to process and enhance requests and responses before they reach the controller or after the response leaves the controller. Interceptors can only intercept requests from the Controller layer and cannot intercept requests for static resources. In Spring Boot, you can customize interceptors by implementing HandlerInterceptor interface or by inheriting HandlerInterceptorAdapter class.
Listeners (Listener)
A listener is a component of a JavaWeb application that is used to listen for events in the Web application such as requests, sessions…