This repository shows how to use Spring AOT optimizations to speedup startup on cheap Cloud instances.
Be aware that AOT optimizations fix at build time the beans created in your application context and currently do not support changing Spring profiles at runtime, see spring-framework#29844 related issue.
Measures have been done on Azure Container Apps with containers using low resources. Those are Kotlin projects but results are similar with Java.
Data points for a Spring Boot microservice using the functional web router (this applications starts in 0.6s with the JVM on my local machine).
Data points for the Spring Data JDBC variant of Petclinic (includes H2 database creation, this applications starts in 1.6s with the JVM on my local machine):
It is possible to use Spring profiles with the AOT mode but you need to enable them at build time if they change the beans of the application context, see this documentation and samples for more details.
Add the following profile to your pom.xml
:
<profiles>
<profile>
<id>aot</id>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<id>process-aot</id>
<goals>
<goal>process-aot</goal>
</goals>
</execution>
</executions>
<configuration>
<image>
<env>
<BPE_DELIM_JAVA_TOOL_OPTIONS xml:space="preserve"> </BPE_DELIM_JAVA_TOOL_OPTIONS>
<BPE_APPEND_JAVA_TOOL_OPTIONS>-Dspring.aot.enabled=true</BPE_APPEND_JAVA_TOOL_OPTIONS>
</env>
</image>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
Build your container image with mvn -Paot spring-boot:build-image
.
See demo-maven-aot
related sample.
Add id("org.springframework.boot.aot")
plugin then configure:
import org.springframework.boot.gradle.tasks.bundling.BootBuildImage
// ...
tasks.named<BootBuildImage>("bootBuildImage") {
environment.set(mapOf(
"BPE_DELIM_JAVA_TOOL_OPTIONS" to " ",
"BPE_APPEND_JAVA_TOOL_OPTIONS" to "-Dspring.aot.enabled=true"
))
}
Build your container image with ./gradlew bootBuildImage
.
See demo-gradle-kts-aot
related sample.
Add id 'org.springframework.boot.aot'
plugin then configure:
tasks.named("bootBuildImage") {
environment["BPE_DELIM_JAVA_TOOL_OPTIONS"] = " "
environment["BPE_APPEND_JAVA_TOOL_OPTIONS"] = "-Dspring.aot.enabled=true"
}
Build your container image with ./gradlew bootBuildImage
.
See demo-gradle-aot
related sample.