springboot拦截器提取@Value属性值时为空的解决方案。



@EnableWebMvc
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {

@Bean
public LocaleInterceptor localeInterceptor() {
return new LocaleInterceptor();
}

@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor( localeInterceptor() );
}
}



原因就是interceptor默认是不被spring context掌管的。所以还添加@bean ,加入的spring 容器下,才可以读取的spring容器内的变量值



参考链接:https://stackoverflow.com/questions/23349180/java-config-for-spring-interceptor-where-interceptor-is-using-autowired-spring-b

你可能感兴趣的:(个人博客)