首页 > 解决方案 > Spring cloud:请求的bean当前正在创建中:是否存在无法解析的循环引用?

问题描述

我有一个应用程序,我想在其中添加 Spring Cloud Netlix Eureka。

我已将此服务发现添加到其他 2 个服务中,没有问题。在第三个服务中,这是更复杂的一个,添加后:

Spring 云依赖

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

bootstrap.properties,带有应用程序名称

spring.application.name=my-app

application.properties 中的 Eureka 相关配置

eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true
eureka.instance.instance-id=${spring.application.name}:${random.int}
eureka.instance.hostname=localhost

在 Application 类上启用 Eureka 客户端

@EnableEurekaClient

在此之后启动应用程序我收到以下错误:

2018-06-25 18:22:31.229 ERROR 7862 --- [ost-startStop-1] o.s.b.web.embedded.tomcat.TomcatStarter  : Error starting Tomcat context. Exception: org.springframework.beans.factory.BeanCreationException. Message: Error creating bean with name 'servletEndpointRegistrar' defined in class path resource [org/springframework/boot/actuate/autoconfigure/endpoint/web/ServletEndpointManagementContextConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.endpoint.web.ServletEndpointRegistrar]: Factory method 'servletEndpointRegistrar' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'healthEndpoint' defined in class path resource [org/springframework/boot/actuate/autoconfigure/health/HealthEndpointConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.health.HealthEndpoint]: Factory method 'healthEndpoint' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.actuate.autoconfigure.jdbc.DataSourceHealthIndicatorAutoConfiguration': Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.autoconfigure.jdbc.DataSourceHealthIndicatorAutoConfiguration$$EnhancerBySpringCGLIB$$f4aa1064]: Constructor threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource': Post-processing of FactoryBean's singleton object failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.jdbc.DataSourceInitializerInvoker': Invocation of init method failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource': Post-processing of FactoryBean's singleton object failed; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'scopedTarget.dataSource': Requested bean is currently in creation: Is there an unresolvable circular reference?

我不确定为什么创建数据源会出现问题。与其他服务的唯一区别是该服务连接到 postgres 数据库。

我已经在“DataSourceHealthIndicatorAutoConfiguration”类中指出了这个问题:

在此处输入图像描述

但是,我仍然不确定为什么只在类路径上使用 spring cloud。

另外,我怀疑它是否相关,因为删除它并不能解决问题,但我还添加了一个新 Bean:

@LoadBalanced
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
  return builder.build();
}

并在课堂上像这样使用:

private final RestTemplate restTemplate;

@Autowired
public AudienceConsoleAudienceFetcher(RestTemplate restTemplate) {
  this.restTemplate = restTemplate;
}

这取代了我之前在同一堂课上的内容:

private RestTemplate restTemplate = new RestTemplate();

编辑:

这是 pom.xml 和 Application 类:https ://gist.github.com/nWidart/1f3a635d6d392afe68d09603c260ed04

谢谢!

标签: springspring-bootspring-cloudspring-cloud-netflix

解决方案


推荐阅读