【发布时间】:2017-03-07 06:20:44
【问题描述】:
我正在使用 springBootVersion = '1.5.2.RELEASE',奇怪的是它在执行测试 JavaMainApplicationTests 时为类 PlainFileReader 提供了以下异常,它只发生在某些类中。我将 PlainFileReader 重构到其他类正在工作的包中,并在 JavaMainApplicationTests 注释中添加了@componentScan,但奇怪的是只有一些类不工作,可能是什么原因以及如何解决它。
@Component
public class Graph<T> {
@Autowired
private PlainFileReader pjr;
private Vertex<T> root;
private ArrayList<Vertex<T>> vertices = new ArrayList<>();
......
@RunWith(SpringRunner.class)
@SpringBootTest
@TestPropertySource(properties = "debug=true")
@ComponentScan("com.*")
public class JavaMainApplicationTests
.......
@Component
public class PlainFileReader {
@Autowired
private Parsers parsers;
......
创建名称为“plainFileReader”的 bean 时出错:通过字段“解析器”表达的依赖关系不满足;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有“com.PlainFileReader”类型的合格 bean 可用:预计至少有 1 个有资格作为自动装配候选者的 bean。依赖注解:{@org.springframework.beans.factory.annotation.Autowired(required=true)}
【问题讨论】:
-
Parsers 类是用 @Component 注释的,还是你正在为此创建一个 bean?
-
是的,我正在创建 bean。
标签: java spring-boot