package com.sdcuike.spring.extend.web;import java.lang.annotation.Documented;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;import org.springframework.core.Ordered;import org.springframework.stereotype.Component;/** * spring HandlerInterceptor扩展 * * @author sdcuike * <p> * Created on 2017.02.13 * <p> * 自定义注解,处理Spring HandlerInterceptor,执行顺序按order 升序排序 */@Target(ElementType.TYPE)@Retention(RetentionPolicy.RUNTIME)@Documented@Componentpublic @interface SpringHandlerInterceptor { String[] value() default {}; String[] includePatterns() default {}; String[] excludePatterns() default {}; int order() default Ordered.LOWEST_PRECEDENCE;} 上面的注解也包括了@Component,这样,spring会自动扫描我们自定义的注解类。自动注册HandlerInterceptor 配置:package com.sdcuike.spring.extend.web;import java.util.ArrayList;import java.util.Arrays;import java.util.Collections;import java.util.Comparator;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.core.annotation.AnnotationUtils;import org.springframework.web.servlet.HandlerInterceptor;import org.springframework.web.servlet.config.annotation.InterceptorRegistration;import org.springframework.web.servlet.config.annotation.InterceptorRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;/** * spring Mvc Configurer Adapter扩展 * * @author sdcuike * <p> * Created on 2017.02.13 * <p> * 处理自定义注解 {@link SpringHandlerInterceptor},自动配置HandlerInterceptor */public abstract class MvcConfigurerAdapter extends WebMvcConfigurerAdapter { @Autowired private List<HandlerInterceptor> handlerInterceptors; @Override public void addInterceptors(InterceptorRegistry registry) { if (handlerInterceptors == null || handlerInterceptors.isEmpty()) { return; } Collections.sort(handlerInterceptors, new Comparator<HandlerInterceptor>() { @Override public int compare(HandlerInterceptor a, HandlerInterceptor b) { return getOrder(a) - getOrder(b); } }); for (HandlerInterceptor handlerInterceptor : handlerInterceptors) { SpringHandlerInterceptor annotation = AnnotationUtils.findAnnotation(handlerInterceptor.getClass(), SpringHandlerInterceptor.class); InterceptorRegistration registration = registry.addInterceptor(handlerInterceptor); if (annotation == null) { continue; } String[] includePatterns = getIncludePatterns(annotation); if (includePatterns.length != 0) { registration.addPathPatterns(includePatterns); } String[] excludePatterns = getExcludePatterns(annotation); if (excludePatterns.length != 0) { registration.excludePathPatterns(excludePatterns); } } } private int getOrder(Object o) { SpringHandlerInterceptor annotation = AnnotationUtils.findAnnotation(o.getClass(), SpringHandlerInterceptor.class); if (annotation == null) { return 0; } return annotation.order(); } private String[] getIncludePatterns(SpringHandlerInterceptor annotation) { List<String> list = new ArrayList<>(); list.addAll(Arrays.asList(annotation.value())); list.addAll(Arrays.asList(annotation.includePatterns())); return list.toArray(new String[] {}); } private String[] getExcludePatterns(SpringHandlerInterceptor annotation) { List<String> list = new ArrayList<>(); list.addAll(Arrays.asList(annotation.excludePatterns())); return list.toArray(new String[] {}); }} 以后,我们的配置只需继承MvcConfigurerAdapter 就可以利用本博客的注解简化工作量,并且支持HandlerInterceptor 排序。相关源码:<spring-boot.version>1.5.1.RELEASE</spring-boot.version>
新闻热点
疑难解答