spring boot 之HandlerInterceptor 自动注入

spring 环境下,java config方式配置HandlerInterceptor ,我们一般继承
org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter,复写方法:

publicvoid addInterceptors(InterceptorRegistry registry)
利用:
registry.addInterceptor(handlerInterceptor)
 
配置handlerInterceptor,是不是非常的啰嗦,工作量重复,就像之前
配置web filter一样的繁琐,工作量重复很多(后面介绍简单方法)。

  所以,我写了一个注解来简化工作量:
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
@Component
public @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>
Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐