我收到此错误-是否存在无法解析的循环引用? 可能的怀疑是我的代码中的这种自动装配:
@Configuration
public class Bean1 {
@Autowired
private Bean3 bean3;
@Autowired
private Bean2 bean2;
}
@Configuration
public class Bean2 {
@Autowired
private Bean3 bean3;
}
这会导致循环依赖吗?怎么办?
编辑:添加对我有用的修复程序。但是寻找它起作用的原因。
在Bean1中自动完成Bean3的自动装配后,错误消失了。但是我不明白为什么?
@Configuration
public class Bean1 {
@Autowired
@Lazy
private Bean3 bean3;
@Autowired
private Bean2 bean2;
}
添加bean的实际代码: Bean1- DbConfig Bean2- FindUser Bean3- DocumentClient
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>
答案 0 :(得分:1)
不,那里没有circular dependency。
依赖关系层次结构是:
Bean1 ──> Bean2 ──┐
│ ↓
└───────────> Bean3
即使您确实具有循环依赖关系,也仅在使用构造函数注入时才会导致错误,并且由于使用的是字段注入,它会永远不会因为这个原因而失败。
答案 1 :(得分:1)
不,您所定义的情况没有循环依赖关系。如@Andreas所述,这是一种非循环依赖方式。
通函是依赖项,
@Configuration
public class Bean2 {
@Autowired
private Bean3 bean3;
}
和
@Configuration
public class Bean3 {
@Autowired
private Bean2 bean2;
}
这种例子属于循环依赖类型。
有多种方法可以避免这种情况,例如在对象的setter方法上使用@Autowire或在对象上使用@Lazy。这样,在加载应用程序时创建了一个对象,而在需求时创建了一个对象。
希望获得帮助。
答案 2 :(得分:0)
对我有用。对于实例,请使用Component
批注确保在上下文中具有Bean3。
@Component
public class Bean3 {}