Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring boot 3.5.0: DataSourceProperties not defined

I used spring boot 3.4.6 before and everything worked fine for my database configuration but now I have an error when I want to use spring boot 3.5.0:

Description:

Parameter 0 of method hikariDataSource in xxx.DatabaseConfiguration required a bean of type 'org.springframework.boot.autoconfigure.jdbc.DataSourceProperties' that could not be found.


Action:

Consider defining a bean of type 'org.springframework.boot.autoconfigure.jdbc.DataSourceProperties' in your configuration.

Indeed, to use LazyConnectionDataSourceProxy I did this configuration:

In application class, I deactivate the auto configuration:

@EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class })

I define my own configuration:

@Configuration
public class DatabaseConfiguration {
    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.hikari")
    public HikariDataSource hikariDataSource(DataSourceProperties properties) {
        HikariDataSource hikariDataSource = createDataSource(properties, HikariDataSource.class);
        if (StringUtils.hasText(properties.getName())) {
            hikariDataSource.setPoolName(properties.getName());
        }
        return hikariDataSource;
    }

    protected static <T> T createDataSource(DataSourceProperties properties, Class<? extends DataSource> type) {
        return (T) properties.initializeDataSourceBuilder().type(type).build();
    }

    @Primary
    @Bean
    public DataSource dataSource(HikariDataSource hikariDataSource) {
        // Wrap hikariDataSource in a LazyConnectionDataSourceProxy
        LazyConnectionDataSourceProxy lazyDataSource = new LazyConnectionDataSourceProxy();
        lazyDataSource.setTargetDataSource(hikariDataSource);
        return lazyDataSource;
    }
}

To solve that error, I now have to explicitly add this to application class:

@EnableConfigurationProperties(DataSourceProperties.class)

Do you know what has changed that can explain this please?

like image 622
phildeg31 Avatar asked Dec 18 '25 15:12

phildeg31


1 Answers

The DataSourceAutoConfiguration contains the @EnableConfigurationProperties(DataSourceProperties.class) so obviously when you are excluding it that @EnableConfigurationProperties doesn't apply either anymore. I suspect the better auto config support and conditionals parsing now resulted in that, which I think should be expected. IMHO it was strange that it worked (exclude the config but still use the properties).

Nonetheless I would strongly suggest you to ditch your whole DatabaseConfiguration and the exclude and instead add a BeanPostProcessor to wrap your DataSource bean(s) in a LazyConnectionDataSourceProxy.

@Bean
public static BeanPostProcessor lazyDataSourceWrapper() {

  return new BeanPostProcessor() {
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
      if (bean instanceof HikariDataSource ds) {
        return new LazyConnectionDataSourceProxy(ds);
      }
      return bean;
    }
  }
}

This way you achieve exactly the same without you needing to mess around with excludes and your own datasource config.

like image 128
M. Deinum Avatar answered Dec 21 '25 04:12

M. Deinum



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!