I try to access a DataSource bean, declared as:
@Bean
@Primary
@ConfigurationProperties(prefix = "app.customer.datasource.properties")
public DataSource customerDataSource() {
return customerDataSourceProperties().initializeDataSourceBuilder()
.type(DataSource.class).build();
}
This works in Spring Boot 1.5 but if fails in Spring Boot 2.5 with the error:
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Specified class is an interface
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:119)
at org.springframework.boot.jdbc.DataSourceBuilder.build(DataSourceBuilder.java:75)
at demo.customer.CustomerConfig.customerDataSource(CustomerConfig.java:55)
at demo.customer.CustomerConfig$$EnhancerBySpringCGLIB$$1637ca06.CGLIB$customerDataSource$4(<generated>)
at demo.customer.CustomerConfig$$EnhancerBySpringCGLIB$$1637ca06$$FastClassBySpringCGLIB$$50f070a1.invoke(<generated>)
I also tried hardcoding the parameters (see below), but the error changed to NullPointerException.
@Bean
@Primary
public DataSource customerDataSource() {
DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
dataSourceBuilder.driverClassName("org.h2.Driver");
dataSourceBuilder.url("jdbc:h2:mem:customers");
dataSourceBuilder.username("SA");
dataSourceBuilder.password("");
return dataSourceBuilder.build();
}
The NPE exception I get is:
Caused by: java.lang.NullPointerException
at org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter.determineDatabaseDialectClass(HibernateJpaVendorAdapter.java:185)
at org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter.buildJpaPropertyMap(HibernateJpaVendorAdapter.java:143)
at org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter.getJpaPropertyMap(HibernateJpaVendorAdapter.java:127)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:346)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341)
....
I also see the same NPE if I choose ".type(HikariDataSource.class)".
Do you have any idea what changed since version 1.5?