【发布时间】:2021-02-02 15:45:10
【问题描述】:
我是 Spring Boot 新手,我想知道这个错误消息。
我正在尝试创建一个 GraphQL 类以连接到各种 GraphQL 配置,并且我想将一个值传递给构造函数(最终用于我的类路径):
public class ArticleResource {
@Autowired
GraphQLService graphQLService = new GraphQLService("classpath:articles.graphql");
... other code
}
public class GraphQLService {
public GraphQLService(String value) {
System.out.println(value);
}
... other code with @Autowired & @PostConstruct annotations
}
我正在使用一个如何将 GraphQL 连接到 Spring Boot 的示例,并且我有几个地方使用了注解 @Autowired 以及 @PostConstruct。我觉得其中之一导致了我所看到的问题。
完整的错误如下:
Description:
Parameter 0 of constructor in com.project.api.graphql.service.GraphQLService required a bean of type 'java.lang.String' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'java.lang.String' in your configuration.
我将如何解决这个问题?我不能使用带有 Autowired 或 PostConstruct 注释的自定义构造函数吗?
【问题讨论】:
-
似乎
GraphQLService没有其他构造函数,因此它将尝试调用该构造函数。而且由于您可能没有在任何地方定义Stringbean,因此您会收到错误消息。考虑定义无参数构造函数或定义String类型的bean。这完全取决于您想要达到的目标。你也可以从你的属性文件中注入这个值,它实际上会更有意义。在这种情况下,您也滥用了自动装配,因为您正在使用new关键字创建实例。
标签: java spring spring-boot graphql