다음은 HandlerInterceptor 인터페이스를 사용하여 인터셉터가 각 특정 시점에 동작하는 메서드 네 개를 호출하는 예입니다.
HandlerInterceptor를 사용한 메서드 호출
ExampleInterceptor.java
package com.springmvc.chap11;
...
import org.springframework.web.servlet.HandlerInterceptor;
public class ExampleInterceptor implements HandlerInterceptor {
public Logger logger = LoggerFactory.getLogger(ExampleInterceptor.class);
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
stopWatch.start(handler.toString());
logger.info("preHandle() 호출...");
if (handler instanceof HandlerMethod) {
HandlerMethod method = (HandlerMethod) handler;
logger.info("핸들러 메소드명 : " + method.getMethod().getName());
}
return true;
}
public void postHandle(HttpServletRequest arg0, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
logger.info("postHandle() 호출...");
}
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception exception) throws Exception {
logger.info("afterCompletion() 호출...");
}
}