【发布时间】:2019-08-09 10:00:29
【问题描述】:
我有一个中型弹簧应用程序。当我重构一些 loc 时,我注意到以下行为:
事实上,注入工作正常:
public class AppConfig {
@Autowired
private Environment env;
…
当我尝试使用构造函数注入时,环境为空我的应用程序告诉我由于循环引用,它无法创建我的配置 bean:
public class AppConfig {
private final Environment env;
private final IndexableService indexableService;
@Autowired
public AppConfig(Environment env, IndexableService indexableService) {
this.env = env;
this.indexableService = indexableService;
}
…
堆栈的某处:
原因:org.springframework.beans.factory.BeanCurrentlyInCreationException:创建名为“appConfig”的bean时出错:当前正在创建请求的bean:是否存在无法解析的循环引用?
我尝试了一些我在网上找到的解决方案,但都没有帮助。我怎样才能正确调试呢?如何找到循环引用的创建位置?
编辑:
编辑 2:
IndexableService 类:
package de.xx.yy.server.service;
import de.xx.yy.server.model.Indexable;
import java.util.List;
public interface IndexableService {
List<Indexable> search(String searchString);
}
类的实现:
package de.xxx.yyy.server.service;
import de.xxx.yyy.server.model.Indexable;
import io.leangen.graphql.annotations.GraphQLArgument;
import io.leangen.graphql.annotations.GraphQLQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class IndexableServiceImpl implements IndexableService {
private final Searcher searcher;
@Autowired
public IndexableServiceImpl(Searcher searcher) {
this.searcher = searcher;
}
@GraphQLQuery(name = "search")
public List<Indexable> search(@GraphQLArgument(name = "searchString") String searchString) {
return searcher.search(searchString);
}
}
PS:之前,我的环境刚刚为空(这就是该行被删除的原因)。我无法重现空环境,现在出现循环引用错误。
【问题讨论】:
-
我会在
BeanCurrentlyInCreationException上设置一个断点并跟踪究竟是什么原因造成的。异常本身很清楚。 -
请提供完整的异常日志?
-
@ThomasPötzsch IndexableService 类是自定义类吗?也提供。
-
@MuhammadUsman 你去吧
-
indexIdRepository不是不能早于IndexableService创建吗?
标签: java spring dependency-injection runtime-error