Skip to content

Circular reference error with RestTemplateBuilder and getDataSource Bean #35217

Closed as not planned
@joshua-phillips

Description

@joshua-phillips

Attempted to upgrade from v2.6.13 to 2.7.11 recently due to some security issues requiring Spring Framework upgrade. Trying to run the application was resulting in a circular reference failure.

Description:

The dependencies of some of the beans in the application context form a cycle:

   webMvcMetricsFilter defined in class path resource [org/springframework/boot/actuate/autoconfigure/metrics/web/servlet/WebMvcMetricsAutoConfiguration.class]
???????
|  simpleMeterRegistry defined in class path resource [org/springframework/boot/actuate/autoconfigure/metrics/export/simple/SimpleMetricsExportAutoConfiguration.class]
?     ?
|  dataSourcePoolMetadataMeterBinder defined in class path resource [org/springframework/boot/actuate/autoconfigure/metrics/jdbc/DataSourcePoolMetricsAutoConfiguration$DataSourcePoolMetadataMetricsConfiguration.class]
?     ?
|  getDataSource defined in com.example.demo.DemoApplication
?     ?
|  getRestTemplate defined in com.example.demo.DemoApplication
?     ?
|  restTemplateBuilder defined in class path resource [org/springframework/boot/autoconfigure/web/client/RestTemplateAutoConfiguration.class]
?     ?
|  restTemplateBuilderConfigurer defined in class path resource [org/springframework/boot/autoconfigure/web/client/RestTemplateAutoConfiguration.class]
?     ?
|  metricsRestTemplateCustomizer defined in class path resource [org/springframework/boot/actuate/autoconfigure/metrics/web/client/RestTemplateMetricsConfiguration.class]
???????


Action:

Relying upon circular references is discouraged and they are prohibited by default. Update your application to remove the dependency cycle between beans. As a last resort, it may be possible to break the cycle automatically by setting spring.main.allow-circular-references to true.

Created a new application from Spring Initilizr using Maven, Java, v2.7.11, JAR, JDK 17 to try and determine if it was anything custom in my existing application, and was able to reproduce the issue with very minimal changes and dependencies.

The error started occurring after adding the spring-boot-starter-actuator and spring-boot-starter-data-jpa dependencies and injecting the RestTemplateBuilder class so that it is part of the getDataSource Bean dependencies. The same error happens if injecting the RestTemplateBuilder directly to the getDataSource Bean rather than indirectly through the getRestTemplate Bean.

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

    @Bean
    public DataSource getDataSource(RestTemplate restTemplate) {
        DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
        dataSourceBuilder.driverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        dataSourceBuilder.url("<DB URL>");
        dataSourceBuilder.username("<DB USER NAME>");
        dataSourceBuilder.password("DB PASSWORD");
        return dataSourceBuilder.build();
    }

The only work-around I found is to use the spring.main.allow-circular-references=true setting in the application.properties file.

Example application: https://github.com/joshua-phillips/spring-boot-2-7-x-circular-reference-example

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions