In this recipe, you'll learn how, with interceptors, we can execute some code across all controllers at different moments of a request workflow with the preHandle()
, postHandle()
, and afterCompletion()
hooks:
Interceptors are used for authentication, logging, and profiling (among others).
Here are the steps to create and register an interceptor:
Create a class extending
HandlerInterceptorAdapter
:public class PerformanceInterceptor extends HandlerInterceptorAdapter {
Override the methods you want to use:
@Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { … return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { ... } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response...