Using Spring Boot 3.1.1 when I do AOT / Native Image, I'm not getting the right beans without the spring.r2dbc.url setting at build time. At runtime I get:
2023-07-05T13:50:30.579Z WARN 1 --- [ main] .r.c.ReactiveWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'runner': Unsatisfied dependency expressed through method 'runner' parameter 0: Error creating bean with name 'customerRepository': Cannot resolve reference to bean 'r2dbcEntityTemplate' while setting bean property 'entityOperations'
2023-07-05T13:50:30.579Z ERROR 1 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Failed to configure a ConnectionFactory: 'url' attribute is not specified and no embedded database could be configured.
Reason: Failed to determine a suitable R2DBC Connection URL
Action:
Consider the following:
If you want an embedded database (H2), please put it on the classpath.
If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).
If I set spring.r2dbc.url or set debug=true or enable Actuator and re-run bootBuildImage (with the native plugin) then I get the required bean. It'd be great if I didn't have to set spring.r2dbc.url at build-time to get the right config beans.
Comment From: jamesward
This seems to be a viable workaround (build.gradle.kts):
tasks.withType<org.springframework.boot.gradle.tasks.aot.ProcessAot> {
systemProperty("spring.r2dbc.url", "placeholder_for_aot")
}
Comment From: wilkinsona
When you're using AOT and native images, beans are fixed at build time and cannot be changed at runtime. This restriction is described in the Spring Framework reference documentation. You'll have to set spring.r2dbc.url at build time to ensure that the desired R2DBC-related beans are defined. If necessary, you can then configure a different spring.r2dbc.url at runtime as long as it doesn't require different beans (for example a pooled vs non-pooled URL won't work).