-
Notifications
You must be signed in to change notification settings - Fork 41.2k
Circular reference error with RestTemplateBuilder and getDataSource Bean #35217
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Why are you injecting |
@wilkinsona we retrieve the data source values as well as other secret data from Hashicorp Vault using their REST API. Just as a quick test, I checked if it is just the fact that So I can't even do something like this where the @Bean
public RestTemplate getRestTemplate(RestTemplateBuilder builder) {
return builder.build();
}
@Bean
public VaultData getVaultData(RestTemplate restTemplate) {
// perform HC Vault REST API calls here to get secret data
return new VaultData();
}
@Bean
public DataSource getDataSource(VaultData vaultData) {
DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
dataSourceBuilder.driverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
dataSourceBuilder.url(vaultData.getUrl());
dataSourceBuilder.username(vaultData.getUsername());
dataSourceBuilder.password(vaultData.getPassword());
return dataSourceBuilder.build();
} For my test public class VaultData {
public String getUrl() {
return "<DB URL>";
}
public String getUsername() {
return "<DB USER NAME>";
}
public String getPassword() {
return "<DB PASSWORD>";
}
} |
Thanks for the additional information. This has been fixed in 3.0 by the changes made for #30636. Unfortunately, we don't feel that we can make those changes in a maintenance release so they're only available in 3.0 and later. In the meantime, you can avoid the problem by creating your own |
@wilkinsona thanks for the info and options! That will hopefully allow us to remove the |
The problem of not using RestTemplateBuilder and a custom RestTemplate instead is that you lose Observability autoconfiguration |
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.
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
andspring-boot-starter-data-jpa
dependencies and injecting theRestTemplateBuilder
class so that it is part of thegetDataSource
Bean dependencies. The same error happens if injecting theRestTemplateBuilder
directly to thegetDataSource
Bean rather than indirectly through thegetRestTemplate
Bean.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
The text was updated successfully, but these errors were encountered: