Sub topic 4
How Spring AOT prepares your application
This chapter assumes you know Spring Boot beans and auto-configuration, and that you have read the previous chapters on the closed-world assumption and the native-image tool.
Why Spring needs a build step
On the JVM, Spring does a lot of work at startup. It scans the classpath for annotations, evaluates @Conditional rules, creates bean definitions, and generates CGLIB proxies for @Configuration classes. All of this relies on reflection and runtime class generation. The closed-world analysis in native-image cannot follow that dynamic work.
Spring AOT (ahead-of-time) processing moves this work to build time. It starts your application context far enough to compute the bean definitions, then writes the result as ordinary Java source code and GraalVM metadata. The native build then compiles that generated code together with your own.
What the process produces
The AOT step produces three kinds of output.
| Output | Purpose |
|---|---|
| Generated Java sources, such as `DemoApplication__ApplicationContextInitializer` and `__BeanDefinitions` classes | Register each bean with plain method calls and lambdas instead of reflection and annotation scanning |
| Pre-generated proxy classes | Replace CGLIB subclasses of `@Configuration` classes that would otherwise be created at runtime |
| Files in `META-INF/native-image` (reflection, resource and proxy metadata) | Tell native-image which elements must stay reachable |
The same processing also writes hints for classes Spring knows it will touch, such as the types behind @ConfigurationProperties bindings.
Running AOT processing yourself
The Spring Boot build plugins run AOT processing automatically when you build natively. You can also run it alone to inspect the output.
./mvnw clean package spring-boot:process-aot
ls target/spring-aot/main/sources
ls target/spring-aot/main/resources/META-INF/native-imageOn Gradle, the equivalent task is ./gradlew processAot, and its output goes under build/generated/aotSources and build/generated/aotResources. Reading the generated __BeanDefinitions classes is the quickest way to see how Spring will build your beans in the native executable.
Trying AOT output on the JVM
The generated code is normal Java, so it also works without native-image. Setting spring.aot.enabled=true makes Spring use the generated initializer instead of scanning at startup.
java -Dspring.aot.enabled=true -jar target/demo.jarThis is a cheap check that AOT processing worked before you spend minutes on a native build. A native executable enables the property automatically.
What AOT fixes in place
The application context is analyzed once, at build time, so decisions Spring normally makes at startup are frozen. The main consequences are:
@Conditionalannotations, including@ConditionalOnProperty, are evaluated during the build. Changing the property later does not add or remove beans.- Profiles that affect which beans exist must be active at build time.
- Beans cannot be registered dynamically with logic that AOT processing cannot predict, such as a
BeanFactoryPostProcessorthat adds definitions based on runtime data.
The next chapter covers these limits in detail.
Common errors
- Stale generated code. If you change configuration but skip
clean, old AOT output can remain. Run a clean build when behavior looks wrong. - Wrong build-time profile. Pass
-Dspring.profiles.active=...to the AOT step, or the generated context will not match the profile you run. - Assuming AOT covers your own reflection. Spring generates hints only for what it understands. Reflection in your code or in third-party libraries still needs hints, which a later chapter covers.
Exercise
You build with -Dspring.profiles.active=dev, but start the native executable with --spring.profiles.active=prod. A bean annotated @Profile("prod") is missing. Why?
Show answer
Profiles that decide which beans exist are evaluated during AOT processing. The build ran with dev, so the @Profile("prod") bean was never included in the generated code. Run AOT processing with the prod profile active, or avoid profile-dependent bean definitions.