spring boot 之HandlerInterceptor 自动注入
来源:互联网 发布:淘宝被投诉商标侵权 编辑:程序博客网 时间:2024/05/16 01:43
spring boot 之HandlerInterceptor 自动注入
spring 环境下,java config方式配置HandlerInterceptor ,我们一般继承
org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter,复写方法:
publicvoidaddInterceptors(InterceptorRegistryregistry)
利用:
registry.addInterceptor(handlerInterceptor)
配置handlerInterceptor,是不是非常的啰嗦,工作量重复,就像之前
博客http://blog.csdn.net/doctor_who2004/article/details/56055505,利用FilterRegistrationBean
配置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@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>
1 0
- spring boot 之HandlerInterceptor 自动注入
- spring boot拦截器HandlerInterceptor详解
- 22. Spring Boot 拦截器HandlerInterceptor【从零开始学Spring Boot】
- 22. Spring Boot 拦截器HandlerInterceptor【从零开始学Spring Boot】
- 22. Spring Boot 拦截器HandlerInterceptor【从零开始学Spring Boot】
- Spring之自动装配注入
- spring-boot笔记-HandlerInterceptor和MethodInterceptor(AspectJ)(四)
- Spring Boot @Autowired 没法自动注入的问题
- spring boot 中@Autowired注解无法自动注入的错误
- 关于spring boot无法自动注入bean问题解决方案
- spring boot 中@Autowired注解无法自动注入的错误
- Spring Boot @Autowired 没法自动注入的问题
- (22)Spring Boot 拦截器HandlerInterceptor【从零开始学Spring Boot】
- spring boot 依赖注入
- Spring Boot 依赖注入
- Spring注解之自动注入Autowired
- Spring Boot 运行原理之自动配置
- Spring Boot之自动配置的原理
- Idea+SpringMVC+Maven+Tomcat搭建一个简单的hello world工程
- 21天Python从入门到放弃(1)
- 15.停止线程 守护线程 join() yield()
- StringUtils的isBlank()和isEmpty()
- 常用基本控件测试用例
- spring boot 之HandlerInterceptor 自动注入
- 安卓阿里百川hotfix集成
- 千图成像(用图片拼图片)的java教程
- Java单例模式深入详解
- NameNode管理元数据的机制
- errno
- Python 函数与变量--习题19,learn python the hard way
- js跨域问题详解
- RSA加密