今天在IDEA写拦截器的时候遇到点困惑,继承了HandlerInterceptor没有报错,我一直认为他会提醒,要重写方法。如下图

没有重写接口方法,IDEA没有报错。

通过查资料,嗯,终于找到原因来,先来上HandlerInterceptor接口的源码

public interface HandlerInterceptor {

	default boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		return true;
	}

	default void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			@Nullable ModelAndView modelAndView) throws Exception {
	}
	
	default void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
			@Nullable Exception ex) throws Exception {
	}
}

敲一敲黑板,重点来了

以上是HandlerInterceptor 接口的源码,可以看到高版本的spring-webmvc jar包中,HandlerInterceptor 接口定义的是默认方法,这是jdk1.8的新特性,也就是说接口中的方法你重写也可以,不重写也不会报错。

所以所这里并不会提醒你重写,你要自己去手动写。

相关文章:

  • 2021-08-20
  • 2021-05-19
  • 2022-12-23
  • 2022-12-23
  • 2021-06-23
  • 2021-04-14
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-05-16
  • 2021-10-07
  • 2021-12-25
  • 2022-12-23
  • 2022-12-23
  • 2021-07-31
  • 2021-07-11
相关资源
相似解决方案