推荐先阅读
:Spring全家桶
1.制作拦截器功能类
2.配置拦截器的执行位置
@Component public class ProjectInterceptor implements HandlerInterceptor { public boolean preHandler(..) throws Exception { System.out.println("preHandler..."); } public boolean postHandler(..) throws Exception { System.out.println("postHandler..."); } public boolean afterCompletion(..) throws Exception { System.out.println("afterCompletion..."); } }
|
@Configuration public class SpringMvcSupport extends WebMvcConfigurationSupport { @Autowired private ProjectInterceptor projectInterceptor;
@Override public void addInterceptors(InterceptorRegistry registry){ registry.addInterceptor(projectInterceptor).addPathPatterns("/books"); } }
|
也可以使用标准接口WebMvcConfigure简化开发(注意:侵入性较强)
@Configuration @ComponentScan("com.zx.controller") @EnableWebMvc public class SpringMvcConfig implements WebMvcConfigurer { @Autowired private ProjectInterceptor projectInterceptor;
@Override public void addInterceptors(InterceptorRegistry registry){ registry.addInterceptor(projectInterceptor).addPathPatterns("/books","/books/*"); } }
|
拦截器执行流程