Precautions
The interceptor configuration in the project must also implement the interface WebMvcConfigurer, and cannot inherit the WebMvcConfigurationSupport class, otherwise the interceptor that implements the interface WebMvcConfiguration class will not take effect.
Enable annotations
@Inherited
@Documented
@Configuration
@EnableWebMvc
@Target({
ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Import({
xxxxSelector.class, WebMvcConfig.class})
public @interface EnableXXXX {
}
WebMvcConfig configuration
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new XXXXInterceptor())
.addPathPatterns("/**");
}
}
XXXXInterceptor implementation
public class XXXXInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
// 拦截器的实现
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
// 执行完的逻辑
}
/** @noinspection NullableProblems*/
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
}