HandlerInterceptor vs HandlerInterceptorAdapter
We can create create custom interceptors in spring by either implementing
Why Spring framework provides two options?
Approach-I:
Problem with this approach is, just assume a scenario when you just need to provide implementation to only one method and there is no need to provide implementation to remaining 2 methods. Unfortunetly, we will have to provide empty implementation to other 2 methods.This approach will be boilerplate for the application code as we are providing empty implemetations to 2 methods.
public class CustomHandlerInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
// Some complex logic
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response,Object handler, ModelAndView modelAndView) throws Exception {
// empty
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response,Object handler, Exception ex) throws Exception {
//empty
}
}
Approach-II:
public class CustomHandlerInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle (HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//Some complex logic }
}
NOTE :