记录@Autowire为null

@Autowired注入为null的几种情况

1.在应用的Filter或Listener中使用了@Autowired

原因:因为Filter和Listener加载顺序优先于spring容器初始化实例,所以使用@Autowired肯定为null了~~

解决:用ApplicationContext根据bean名称(注意名称为实现类而不是接口)去获取bean,随便写个工具类即可

2.你写的代码有问题,没加@Service、@Componet、@Configuration、@Repository注解

3.你写的@Service、@Componet、@Configuration、@Repository等Spring注解未被扫描到,例如:springboot的主类扫描规则@ComponentScan扫描多个包配置

4.你打上@Componet注解的类, 被手动new出来了, @Autowired注入会为null
————————————————
原文链接:https://blog.csdn.net/weixin_43687353/article/details/107057279

原文地址:https://www.cnblogs.com/yanjy/p/15149773.html