gpt4 book ai didi

java - 可能发生错误的情况 - "Is there an unresolvable circular reference?"

转载 作者:行者123 更新时间:2023-11-30 01:44:48 25 4
gpt4 key购买 nike

我收到此错误 - 是否存在无法解析的循环引用?可能的怀疑是我的代码中的 Autowiring :

@Configuration
public class Bean1 {

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

@Autowired
private Bean3 bean3;
}

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

<小时/>

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

当我在 Bean1 中将 Bean3 的 Autowiring 设为惰性之后,错误就消失了。但我不明白为什么?

@Configuration
public class Bean1 {

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

添加bean的实际代码:Bean1-DbConfigBean2-查找用户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";
}}

pom.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>

最佳答案

不,没有circular dependency那里。

依赖层次结构是:

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

即使您确实有循环依赖项,如果您使用构造函数注入(inject),它也只会导致错误,并且由于您使用字段注入(inject),它会永远不会因为这个原因而失败。

关于java - 可能发生错误的情况 - "Is there an unresolvable circular reference?",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58516334/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com