Springboot 拦截器@Autowired 注入失败

阅读更多

最近在用springboot写一个项目,发现springboot确实比springMvc好用,通过各种配置就可以较少很多代码

但是在做的过程中,发现自己定义的拦截器在注入@Autowired   时不成功   @Autowired


下的接口类是null值。


 

 

经过查找,才知道拦截器加载的时间点在springcontext之前,所以在拦截器中注入才为null,这就是问题的原因。所以需要在配置文件中使用@Bean注解提前去加载。


Springboot 拦截器@Autowired 注入失败_第1张图片
 

最后在在添加到注册里面就可以了,再回去测试就发现成功注解上了

 

 


 

问题愉快解决!!!!!!
 

  • Springboot 拦截器@Autowired 注入失败_第2张图片
  • 大小: 96.3 KB
  • Springboot 拦截器@Autowired 注入失败_第3张图片
  • 大小: 159.5 KB
  • Springboot 拦截器@Autowired 注入失败_第4张图片
  • 大小: 88 KB
  • Springboot 拦截器@Autowired 注入失败_第5张图片
  • 大小: 64.2 KB
  • 查看图片附件

你可能感兴趣的:(在拦截器中@Autowired,注解失败,springboot)