When I try to build a SpringBoot application using GraalVM, spring.profiles.active
does not seem to take effect.
spring boot and graal vm plugin version of my application:
id("org.springframework.boot") version "3.5.6"
id("org.graalvm.buildtools.native") version "0.10.6"
application.properties
spring.application.name=dynamic-namimg-config-server
server.port=9191
application-prod.properties
spring.r2dbc.url=r2dbc:mysql://root:root@192.168.11.110:3306/dynamic_config_naming
spring.r2dbc.name=root
spring.r2dbc.password=root
spring.sql.init.mode=always
When I try start spring boot native application using command./dynamic-naming-config-server --spring.profiles.active=prod
, I get these error, spring.profiles.active
does not seem to take effect.
***************************
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 (the profiles prod are currently active).
Then I try to register RuntimeHintsRegistrar, but spring.profiles.active
does not seem to take effect.
@ImportRuntimeHints(AotConfiguration.AotRuntimeHintsRegistrar::class)
@Configuration(proxyBeanMethods = false)
class AotConfiguration {
class AotRuntimeHintsRegistrar : RuntimeHintsRegistrar {
override fun registerHints(
hints: RuntimeHints,
classLoader: ClassLoader?
) {
hints.resources()
.registerPattern("application-dev.properties")
.registerPattern("application-beta.properties")
.registerPattern("application-prod.properties")
}
}
}
finally, I try to add spring.profiles.active=prod
in application.properties
.
spring.application.name=dynamic-namimg-config-server
server.port=9191
spring.profiles.active=prod
I still try to run command with./dynamic-naming-config-server --spring.profiles.active=prod
.it works fine.
2025-10-03T03:36:13.160+08:00 INFO 59263 --- [dynamic-namimg-config-server] [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port 9191 (http) with context path '/'
2025-10-03T03:36:13.161+08:00 INFO 59263 --- [dynamic-namimg-config-server] [ main] s.DynamicNamimgConfigServerApplicationKt : Started DynamicNamimgConfigServerApplicationKt in 1.747 seconds (process running for 1.781)
Comment From: bclozel
@wanna280 this is the expected behavior. Compiling to a native image is about tradeoffs and there are some limitations to the usual flexibility allowed by the JVM. Limitations and tradeoffs are documented in the Spring Framework AOT reference docs, in the Spring Boot reference docs and on a Spring Boot wiki page.
If the AOT engine did not fix those values at build time, the native image size would be larger and less efficient. Enabling profiles dynamically is not the only property that is fixed at build-time - any property that changes the number of beans will be fixed as well (see https://github.com/spring-projects/spring-boot/issues/47231 for another example).