How are Spring HandlerInterceptors instantiated?

Is there a new Spring HandlerInterceptors instance for every request?

I have an interceptor in Spring that has a class field.

public class MyInterceptor extends HandlerInterceptorAdapter {
    Private boolean state = false;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        state = true;
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
        if (state == true) {
        System.out.println("Success");
        }
}

If this interceptor is used, will it always print “Success”? (No matter how many threads do this at the same time?)

+5
source share
1 answer

As an instance of the interceptor is created, you can configure it as a bean. If you do not explicitly specify an area for the bean, then it will be a single, like any other bean, and therefore the field statewill be used by all requests.

- , , .

, , ar equest-scoped bean.

+5

All Articles