【问题标题】:Using constructor injection in @Application bean causes a circular reference在 @Application bean 中使用构造函数注入会导致循环引用
【发布时间】: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:是否存在无法解析的循环引用?

我尝试了一些我在网上找到的解决方案,但都没有帮助。我怎样才能正确调试呢?如何找到循环引用的创建位置?


编辑:

stacktrace on pastebin

log on pastebin


编辑 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


【解决方案1】:

主要问题是由于依赖缺失

通过构造函数参数0表示的不满足的依赖关系;嵌套异常是 org.springframework.beans.factory.UnsatisfiedDependencyException:在文件 [/Users/ry77/Workspace/Campus/$PROJECT-server/target/$COMPANY.$PROJECT.server-0.0 中定义的名称为“elasticSearch”的 bean 创建错误.1-alpha/WEB-INF/classes/de/$COMPANY/$PROJECT/server/service/searchengine/ElasticSearch.class]:通过构造函数参数0表示的不满足的依赖关系;

1)@EnableAutoConfiguration 或 @SpringBootApplication 应该存在于主类中
2) 确保你的 pom 中有依赖项

 <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jpa</artifactId>
    <version>2.0.8</version>
</dependency>
    or
  <dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
  </dependency

3) 你的 Elastic 类应该用 @Component 注释和注入 @autowired

希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-29
    • 1970-01-01
    • 1970-01-01
    • 2016-08-04
    • 2019-05-15
    • 2016-11-26
    相关资源
    最近更新 更多