Description
openedon Sep 23, 2024
Describe the bug
Hello,
When using the Azure SDK for Spring to enable passwordless authentication for PostgreSQL (and potentially other RDBMS as well) with multiple data sources, the application fails to start due to a bean naming conflict. Specifically, the JdbcPropertiesBeanPostProcessor
is executed twice and attempts to register the bean named SpringTokenCredentialProvider::PASSWORDLESS_TOKEN_CREDENTIAL_BEAN_NAME
several times, resulting in an error.
The line which causes the error can be found here.
Exception or Stack Trace
2024-09-23T12:46:51.710+02:00 WARN 19768 --- [example-svc] [ main] onfigReactiveWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'readModelEntityManagerFactory' defined in class path resource [com /example/config/JpaReadModelDatasourceConfiguration.class]: Failed to initialize dependency 'readModelLiquibase' of LoadTimeWeaverAware bean 'readModelEntityManagerFactory': Error creating bean with name 'readModelLiquibase' defined in class path resource [com /example/config/LiquibaseConfiguration.class]: Unsatisfied dependency expressed through method 'readModelLiquibase' parameter 0: Error creating bean with name 'readModelDataSource' defined in class path resource [com /example/config/JpaReadModelDatasourceConfiguration.class]: Unsatisfied dependency expressed through method 'readModelDataSource' parameter 0: Error creating bean with name 'readModelDataSourceProperties' defined in class path resource [com /example/config/JpaReadModelDatasourceConfiguration.class]: Invalid bean definition with name 'passwordlessTokenCredential' defined in null: Cannot register bean definition [Root bean: class [com.azure.core.credential.TokenCredential]; scope=; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodNames=null; destroyMethodNames=null] for bean 'passwordlessTokenCredential' since there is already [Root bean: class [com.azure.core.credential.TokenCredential]; scope=; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodNames=null; destroyMethodNames=null] bound.
2024-09-23T12:46:51.756+02:00 INFO 19768 --- [example-svc] [ main] .s.b.a.l.ConditionEvaluationReportLogger :
Disconnected from the target VM, address: 'localhost:59571', transport: 'socket'
Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
2024-09-23T12:46:51.801+02:00 ERROR 19768 --- [example-svc] [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
The bean 'passwordlessTokenCredential' could not be registered. A bean with that name has already been defined and overriding is disabled.
Action:
Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true
To Reproduce
- Create spring project.
- Define 2 spring beans of type DataSource .
- Configure passwordless authentication to azure Azure Database for PostgreSQL(or other RDBMS which supports passwordless authentication) using spring properties.
Code Snippet
The spring application.yaml
config file
spring:
datasource:
azure:
passwordless-enabled: true
read-model:
url: jdbc:postgresql://[placholder]:5432/read_model_db?sslmode=require
username: [URL to azure database]
write-model:
url: jdbc:postgresql://[placholder]:5432/read_model_db?sslmode=require
username: [placholder]
The data-source configuration
@Primary
@Bean(name = "readModelDataSourceProperties")
@ConfigurationProperties(prefix = "spring.datasource.read-model")
public DataSourceProperties dataSourceProperties() {
return new DataSourceProperties();
}
@Primary
@Bean(name = "readModelDataSource")
@ConfigurationProperties(prefix = "spring.datasource.read-model")
public DataSource readModelDataSource(DataSourceProperties dataSourceProperties) {
return dataSourceProperties.initializeDataSourceBuilder().build();
}
@Bean(name = "writeModelDataSourceProperties")
@ConfigurationProperties(prefix = "spring.datasource.write-model")
public DataSourceProperties dataSourceProperties() {
return new DataSourceProperties();
}
@Bean(name = "writeModelDataSource")
@ConfigurationProperties(prefix = "spring.datasource.write-model")
public DataSource dataSource(
@Qualifier("writeModelDataSourceProperties") DataSourceProperties dataSourceProperties) {
return dataSourceProperties.initializeDataSourceBuilder().build();
}
Expected behavior
The application should initialize without errors, with two distinct data sources, and correctly configure passwordless authentication.
Setup (please complete the following information):
OS: Windows 11
IDE: [ntelliJ IDEA
Library/Libraries:
- com.azure.spring:spring-cloud-azure-autoconfigure:5.14.0
- com.azure.spring:spring-cloud-azure-starter-jdbc-postgresql:5.14.0
Java version: 17
Frameworks: Spring Boot
Information Checklist
Kindly make sure that you have added all the following information above and checkoff the required fields otherwise we will treat the issuer as an incomplete report
- [+] Bug Description Added
- [+] Repro Steps Added
- [+] Setup information Added
Metadata
Assignees
Labels
Type
Projects
Status
In Progress