如果一个类没有加注解如@component, @controller, @service等扫描这个类到容器中,在类中的变量加@Autowired注解无法生效。因为如果一个类new对象生成的,那么这个类就不归spring容器管理,IOC等spring的功能也就无法使用了。 @Autowired是通过spring容器自动创建对象属性方法,@Service注解告诉spring这是Service类,才能实现注入,而我的demo类并没有使用注解交给spring管理,导致无法依赖注入,@Component注解刚好可以用于无法定义属于什么类型,但又想交给spring管理时使用。