有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java案例中可能出现错误“是否存在无法解决的循环引用?”

我得到这个错误-是否存在无法解决的循环引用? 可能的嫌疑人是我的代码中的自动连线:

@Configuration
public class Bean1 {

  @Autowired
  private Bean3 bean3;
  @Autowired
  private Bean2 bean2;
}
@Configuration
public class Bean2 {

  @Autowired
  private Bean3 bean3;
}

这会导致循环依赖吗?如何


编辑:添加对我有效的修复。但是寻找它起作用的原因

在我将Bean3的自动布线设置为Bean1中的惰性之后,错误消失了。但我不明白为什么

@Configuration
public class Bean1 {

  @Autowired
  @Lazy
  private Bean3 bean3;
  @Autowired
  private Bean2 bean2;
}

添加bean的实际代码: Bean1-DbConfig Bean2-FindUser Bean3-文档客户端

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import com.microsoft.azure.documentdb.Database;
import com.microsoft.azure.documentdb.DocumentClient;
import com.microsoft.azure.documentdb.DocumentCollection;
import com.microsoft.azure.spring.data.cosmosdb.config.AbstractDocumentDbConfiguration;
import com.microsoft.azure.spring.data.cosmosdb.config.DocumentDBConfig;
@Configuration
@PropertySource("classpath:application.properties")
public class DbConfig extends AbstractDocumentDbConfiguration {

  @Value("${db}")
  private String database;

  @Value("${key}")
  private String databaseURI;

  @Value("${someValue}")
  private String databaseKey;

  @Autowired
  private DocumentClient documentClient;

  @Override
  public DocumentDBConfig getConfig() {
    return DocumentDBConfig.builder(databaseURI, databaseKey, database).build();
  }

  public DocumentCollection getTodoCollection(String collectionName) {
    return documentClient
        .queryCollections("tst",
            "SELECT * FROM r WHERE r.id='" + collectionName + "'", null)
        .getQueryIterable().toList().get(0);
  }

  private Database getDb() {
    return null;
  }

}


import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import com.google.gson.Gson;
import com.microsoft.azure.documentdb.Document;
import com.microsoft.azure.documentdb.DocumentClient;
@Service
public class FindUser {


  @Autowired
  private UserRepository userRepository;
  @Lazy
  @Autowired
  private DocumentClient documentClient;

  @Autowired
  private DbConfig document;


  public String doSomething(String customerId) {
         return "something";
}}

波姆。xml

<properties>
<azure.version>2.1.2</azure.version>
        <spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
</properties>
<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jersey</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web-services</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-sleuth</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>com.microsoft.azure</groupId>
            <artifactId>azure-documentdb-spring-boot-starter</artifactId>
            <version>2.0.4</version>
        </dependency>
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>${springfox-swagger.version}</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>${springfox-swagger.version}</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>rest-assured</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.github.fge</groupId>
            <artifactId>json-schema-core</artifactId>
            <version>1.2.5</version>
        </dependency>
        <dependency>
            <groupId>org.everit.json</groupId>
            <artifactId>org.everit.json.schema</artifactId>
            <version>1.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <!-- Spring Boot Actuator for monitoring -->
        <!-- Google code formatter -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zipkin</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>persistence-api</artifactId>
            <version>1.0</version>
        </dependency>
        <dependency>
            <groupId>org.threeten</groupId>
            <artifactId>threetenbp</artifactId>
            <version>0.7.2</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <scope>compile</scope>
        </dependency>

        <!-- Cucumber Dependency for java -->
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-java8</artifactId>
            <version>1.2.5</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/info.cukes/cucumber-junit -->
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-junit</artifactId>
            <version>1.2.5</version>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>com.microsoft.azure</groupId>
                <artifactId>azure-spring-boot-bom</artifactId>
                <version>${azure.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

共 (3) 个答案

  1. # 1 楼答案

    不,你没有你定义的循环依赖关系。正如@Andreas所提到的,这是依赖的一种方式,而不是循环的

    这个循环是这样的

    @Configuration
    public class Bean2 {
    
      @Autowired
      private Bean3 bean3;
    
    }
    

    @Configuration
    public class Bean3 {
    
      @Autowired
      private Bean2 bean2;
    }
    

    这种示例属于循环依赖类型

    有一些方法可以避免这种情况,比如在对象的setter方法上使用@Autowire,或者在对象上使用@Lazy。通过这种方式,一个对象在应用程序加载时创建,一个对象在需求时创建

    希望这有帮助

  2. # 2 楼答案

    这对我有用。确保Bean3在上下文中,例如使用Component注释

    @Component
    public class Bean3 {}
    
  3. # 3 楼答案

    不,那里没有circular dependency

    依赖关系层次结构是:

    Bean1 ──> Bean2 ──┐
      │               ↓
      └───────────> Bean3 
    

    即使你确实有一个循环依赖,如果你使用构造函数注入,它也只会导致一个错误,因为你使用的是字段注入,因此它永远不会失败