在测试类中导入@Autowired
注释后出现错误
今天因为尚硅谷spring boot2第11集教程中内容有一点小纰漏,导致踩到了一个坑
错误内容:
Could not autowired
运行程序后报错:
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'check.springtest.NaniApplicationTest':
Unsatisfied dependency expressed through field 'person';
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type 'check.springtest.bean.Person' available:
expected at least 1 bean which qualifies as autowire candidate.
Dependency annotations:
{@org.springframework.beans.factory.annotation.Autowired(required=true)}
出错原因:
没有在person
类上面添加@Component
,它的作用是:把普通Java
对象实例化到spring
容器中