场景
在使用Spring Boot框架时,很经常会使用到拦截器。可是,有时候我们会遇到在拦截器使用@Value注解来获取配置文件中的配置参数获取失败,一直都是为null的情况。
以往
以往的配置如下:
/**
* 添加拦截器配置
* @author Kellan_Song
* @createTime 2018年3月27日
*/
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
// 多个拦截器组成一个拦截器链
// addPathPatterns 用于添加拦截规则
// excludePathPatterns 用户排除拦截
//new ParamInterceptor()是一个参数拦截器拦截 /api/接口的参数
registry.addInterceptor(new ParamInterceptor()).addPathPatterns("/api/**");
WebMvcConfigurer.super.addInterceptors(registry);
}
}
这种配置在网上是最多人建议的,但正是这种配置方式导致在拦截器中使用@value为null。(像我技术这么菜的人,真的找了很久的原因,才发现此处有问题)。
接下来给大家看看使用@Value获取配置不为null的配置方式:
/**
* 添加拦截器配置
* @author Kel