解决 Springboot中Interceptor拦截器中依赖注入失败
2018-03-06 15:19
645 查看
问题:
在Springboot拦截器Interceptor中使用@Autowired依赖注入时,发现运行的时候被注解的对象居然是null,没被注入进去
源代码为:
修改之后的代码为:
问题结论分析:
在Springboot拦截器Interceptor中使用@Autowired依赖注入时,发现运行的时候被注解的对象居然是null,没被注入进去
源代码为:
@Configuration public class WebAppConfig extends WebMvcConfigurerAdapter { @Override public void addInterceptors(final InterceptorRegistry registry) { registry.addInterceptor(new UserInterceptor()) .addPathPatterns("/**") .excludePathPatterns("/login/**", "/logout/**", "/loginPage/**", "/error/**"); super.addInterceptors(registry); } }
修改之后的代码为:
@Configuration public class WebAppConfig extends WebMvcConfigurerAdapter { //提前注入bean。 @Bean public UserInterceptor userInterceptor() { return new UserInterceptor(); } @Override public void addInterceptors(final InterceptorRegistry registry) { registry.addInterceptor(userInterceptor()) .addPathPatterns("/**") .excludePathPatterns("/login/**", "/logout/**", "/loginPage/**", "/error/**"); super.addInterceptors(registry); } }
问题结论分析:
需要在拦截器初始化之前初始化bean
相关文章推荐
- 解决 Springboot中Interceptor拦截器中依赖注入失败
- 解决Springboot中Interceptor拦截器中依赖注入失败
- 解决Springboot中Interceptor拦截器中依赖注入失败
- spring boot项目依赖另外一个spring boot项目打包失败的解决方式
- spring boot 多模块管理时,依赖注入bean失败
- spring boot 多模块管理时,依赖注入bean失败
- Spring AOP切面类依赖注入失败问题解决
- SpringBoot中Junit测试注入Bean失败的解决方法
- 因Spring AOP导致@Autowired依赖注入失败的解决方法
- spring boot项目依赖spring mvc项目注入失败问题
- springboot整合mybatis dao注入失败 这个是一篇提问的稿子啊啊啊啊,悬赏不够只能写在这里了
- 在SSH整合中,关于spring依赖注入失败的问题
- springboot整合mybatis mapper注入时显示could not autowire的解决
- 关于怎么解决从ajax传入的json参数注入到Controller的接收对象 以及如何在Spring Boot项目使用参数校验
- 解决WebSocket无法依赖注入的问题(Spring)
- 【SpringBoot】拦截器使用@Autowired注入接口为null解决方法
- SpringBoot中实现依赖注入功能
- 初识SpringBoot @Autowired注入失败
- Spring Boot2.x-05Spring Boot基础-使用注解完成依赖注入
- Springboot循环依赖解决办法