-
Notifications
You must be signed in to change notification settings - Fork 41.1k
DataSourceProperties should be created with conditional beans in Auto Configuration. #14071
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
Thanks for the report but it's difficult to know what to do with it as you have described a solution to a problem without telling us what the problem is.
This example is for defining two
Why is the bean useless? It's not clear what you are trying to do. |
sorry. i did not mention the Primary DataSource. It says Primary DataSourceProperties. In my case, I'm running a multi-module project. And I have multiple data sources. So, instead of 'spring.datasource', we use DataSourceProperties which receives 'app.datasource1' and 'app.datasource2' properties. A module
B module
Some applications use only a single datasource, and some applications require multiple datasources. The application only needs to use the required Datasource configuration module dependencies and write the properties. There is no problem because DataSourceProperties is explicitly bound to each DataSource. However, the problem is that the DataSourceProperties cause a NoUniqueBeanDefinitionException. So I have come to the two conclusions mentioned above.
|
There's a related problem here with how other components use |
I agree that there are issues related to how other components use DataSourceProperties. And I changed my mind and reached the following conclusion.
Below are the configuration classes used in
Except for I also noticed that there is a
Therefore, it is correct that the Below is the conclusion I think. remove EnableConfigurationProperties from @Bean
@ConditionalOnMissingBean(DataSourceProperties.class)
public DataSourceProperties dataSourceProperties() {
return new DataSourceProperties();
} The DataSourceInitializerInvoker(
ObjectProvider<DataSource> dataSource,
ObjectProvider<DataSourceProperties> properties,
ApplicationContext applicationContext) {
this.dataSource = dataSource;
this.properties = properties;
this.applicationContext = applicationContext;
} private DataSourceInitializer getDataSourceInitializer() {
if (this.dataSourceInitializer == null) {
DataSource ds = this.dataSource.getIfUnique();
DataSourceProperties props = this.properties.getIfUnique();
if (ds != null && props != null) {
this.dataSourceInitializer = new DataSourceInitializer(ds, properties, this.applicationContext);
}
}
return this.dataSourceInitializer;
} |
@wilkinsona |
I think this has been addressed as part of reworking DataSource initialization in Spring Boot 2.5. An app with these two beans fails with 2.4.x: @Bean
@ConfigurationProperties("custom.datasource")
public DataSourceProperties dataSourceProperties() {
return new DataSourceProperties();
}
@Bean
public DataSource dataSource() {
DataSourceProperties properties = dataSourceProperties();
return DataSourceBuilder.create().url(properties.determineUrl()).build();
} It starts successfully when using 2.5. |
The
DataSourceProperties
are public, and there are many customization examples in the documentation.The example states the following:
I think it's strange about the
@Primary
setting. Without this setting, throwsNoUniqueBeanDefinitionException
.This is because of
@EnableConfigurationProperties(DataSourceProperties.class)
.@EnableConfigurationProperties (DataSourceProperties.class)
is used in several places of spring boot auto-configuration.The following code is representative.
Why do I need to make a useless
DataSourceProperties
Bean from "spring.datasource"?I think, remove
EnableConfigurationProperties
, andDataSourceProperties
should be created with conditional beans.In addition, all
DatasourceProperties
inserted into the Spring Boot Auto Configuration must be wrapped in anObjectProvider
.The following code is representative.
Although the
DataSourceInitializerInvoker
not works with multiple data sources, throwNoUniqueBeanDefinitionException
Without Primary.I think
DataSourceProperties
should be assigned likeDataSource
.thanks.
The text was updated successfully, but these errors were encountered: