Sobes.tech
Middle

Mis vahe on Filters, Interceptors ja Listeners Springis?

sobes.tech AI

Vastus AI-lt

  • Filters (Spring Web): Intercept requests and responses at the servlet level. Used for modifying requests/responses, authentication, authorization, logging before the request reaches the controller. Applied to URL patterns.

  • Interceptors (Spring MVC): Intercept requests at the Spring MVC level. Work after Filters, but before the request is handled by the controller. Allow actions before (preHandle), after (postHandle), and after completion (afterCompletion). Used for model manipulation, access control at the controller level. Applied to controller methods.

  • Listeners (Spring Context): React to events in the Spring context. Allow actions on startup or shutdown of the context, upon receiving specific event types. Used for resource initialization, data cleanup, or executing tasks when application state changes. Operate independently of web requests. Implement the ApplicationListener interface.

Here is a comparative table:

Feature Filters Interceptors Listeners
Level Servlet Spring MVC Spring Context
When it works Before and after servlet Before, after controller and completion When an event occurs
Applies to URL patterns Controller methods Event types
Purpose Modifying request/response, authentication Manipulating model, authorization at controller level Reacting to context events
Interface javax.servlet.Filter org.springframework.web.servlet.HandlerInterceptor org.springframework.context.ApplicationListener