Allow Data JDBC Dialect resolution without requiring DataSource initialization
When obtaining a Dialect bean using Spring Data JDBC, the @Bean method depends on NamedParameterJdbcOperations leading to initialization of the entire bean graph including DataSource initialization even if the dialect is provided through properties (spring.data.jdbc.dialect=…). It would be good if access to NamedParameterJdbcOperations could be lazy.
The JDBC config itself isn't helpful in that regard as it declares NamedParameterJdbcOperations in its signature. Maybe an opportunity for improvement as well.
The JDBC config itself isn't helpful in that regard as it declares NamedParameterJdbcOperations in its signature.
Indeed, I think our hands are tied here without a change to the signature of AbstractJdbcConfiguration.jdbcDialect(NamedParameterJdbcOperations) that we override.
We could introduce a @Bean Dialect jdbcDialect() method on the config class and forward the call to Dialect jdbcDialect(NamedParameterJdbcOperations operations) but honestly, the AbstractJdbcConfiguration class would require a larger revision and changing bean methods is a breaking change on its own. Maybe worth targeting 4.1 with a proper deprecation cycle.
Alright, I've put this in 4.x and labelled it as blocked. If you open an issue for the changes on the Spring Data JDBC side, please let us know so that we can track its progress.
The workaround is to explicitly define dialect bean - otherwise processAot phase in Gradle or Maven fails:
/**
* hints for AOT processing - otherwise Spring Boot will try to connect to the DB
*/
@Bean
@ConditionalOnMissingBean
fun jdbcDialect(): JdbcDialect = JdbcPostgresDialect.INSTANCE
We've introduced with https://github.com/spring-projects/spring-data-relational/issues/2165 a utility for better reuse of configuration functionality. Because of such a late change in Spring Data JDBC, it wasn't possible to integrate the change in Spring Boot. We're happy to refine JdbcConfiguration for better composition if the Boot config is missing functionality to lower requirements for certain configuration arrangements and delaying database access.