3.如果你想要完全自定义SpringMVC,不保留SpringBoot提供的一切特征,你可以自己定义类并且添加@Configuration
注解和@EnableWebMvc
注解。
4.总结:
5.案例:
首先我们定义一个拦截器:
public class LoginInterceptor implements HandlerInterceptor { private Logger logger = LoggerFactory.getLogger(LoginInterceptor.class); @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { logger.debug("preHandle method is now running!"); return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) { logger.debug("postHandle method is now running!"); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) { logger.debug("afterCompletion method is now running!"); } }
然后,我们定义配置类,注册拦截器:
@Configuration public class MvcConfig implements WebMvcConfigurer{ /** * 通过@Bean注解,将我们定义的拦截器注册到Spring容器 * @return */ @Bean public LoginInterceptor loginInterceptor(){ return new LoginInterceptor(); } /** * 重写接口中的addInterceptors方法,添加自定义拦截器 * @param registry */ @Override public void addInterceptors(InterceptorRegistry registry) { // 通过registry来注册拦截器,通过addPathPatterns来添加拦截路径 registry.addInterceptor(this.loginInterceptor()).addPathPatterns("/**"); } }
修改日志级别(默认为info):
# 设置com.leyou包的日志级别为debug logging.level.com.leyou=debug
来源:https://www.cnblogs.com/djdjfj/p/10715629.html