Sub topic 4
How Spring AOT prepares your application
This chapter assumes you know Spring Boot basics (beans, auto-configuration, application properties), can build a project with Maven or Gradle, and understand at a conceptual level that GraalVM native-image analyzes a closed world of classes. You will learn what Spring AOT processing does, what it generates, and which parts of your application it fixes before the native executable exists.
Why Spring needs an ahead-of-time step
On a normal JVM, Spring builds its application context at startup. It scans the classpath, reads annotations, evaluates @Conditional rules, creates bean definitions, and instantiates beans through reflection. All of this is dynamic. It works because the JVM can load any class on demand.
GraalVM native-image works under the closed-world assumption. It decides at build time which code is reachable, and anything it cannot see is left out of the executable. Dynamic classpath scanning and reflective bean creation are exactly the kind of behavior it cannot see through.
Spring AOT (ahead-of-time) processing closes this gap. It runs your application's configuration at build time, before native-image starts, and turns the result into plain generated code and metadata files. native-image then analyzes ordinary Java that creates beans directly, so it needs little reflection to start the context.
What AOT processing does
During the build, Spring starts a special context that loads bean definitions but does not create any bean instances. It then inspects those definitions and writes out files. In Spring Boot 3 the work is done by the process-aot goal in Maven or the processAot task in Gradle. The native build triggers it for you, so you rarely call it directly.
The process has these steps:
- Spring loads your configuration classes and evaluates conditions against the build-time environment.
- It refreshes the context for AOT only. Bean definitions are registered and post-processed, but beans are not instantiated.
- It generates Java source that registers the same bean definitions programmatically.
- It generates runtime hints that describe reflection, resource, and proxy usage.
- The generated sources are compiled with your code, and the hints are written as native-image configuration.
What gets generated
The generated code lands under target/spring-aot for Maven and build/generated/aotSources for Gradle. Inspecting it is the best way to understand what Spring AOT does.
The output contains these kinds of artifacts:
| Artifact | Purpose |
|---|---|
| `*__BeanDefinitions` classes | Java code that registers each bean definition, including its constructor, factory method, and injected dependencies |
| `ApplicationContextInitializer` classes | An entry point that wires all generated bean definitions into the context at startup |
| Proxy classes | Pre-generated subclasses for configuration classes, replacing runtime CGLIB generation |
| `reflect-config.json`, `resource-config.json`, `proxy-config.json` | Reachability metadata derived from the runtime hints |
| `native-image.properties` | Build arguments for native-image |
For a configuration class with a @Bean method, the generated definition class calls your factory method directly. A simplified version looks like this:
public class AppConfig__BeanDefinitions {
public static BeanDefinition getGreetingServiceBeanDefinition() {
Class<?> beanType = GreetingService.class;
RootBeanDefinition beanDefinition = new RootBeanDefinition(beanType);
beanDefinition.setInstanceSupplier(GreetingService::new);
return beanDefinition;
}
}The real generated code is more detailed, but the idea is the same. The instance supplier is a direct constructor reference. native-image sees that GreetingService is constructed, so it needs no reflection entry for it.
Constraints that follow from build-time processing
Because the context is analyzed before the application runs, some things that work on a JVM stop working. The key rule is that the set of bean definitions is fixed at build time.
Conditions are evaluated at build time
Conditions such as @ConditionalOnProperty, @ConditionalOnClass, and @Profile are evaluated during AOT processing. The result is baked into the generated code. Consider this configuration:
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class FeatureConfig {
@Bean
@ConditionalOnProperty(name = "app.feature.enabled", havingValue = "true")
public FeatureService featureService() {
return new FeatureService();
}
public static class FeatureService {}
}If app.feature.enabled is not true when AOT runs, no bean definition for featureService is generated. Setting the property to true when you start the executable does nothing, because the code that would create the bean does not exist.
To enable the feature, set the property during the build. In Maven you can pass it to the AOT step through the spring-boot-maven-plugin configuration, for example by adding --app.feature.enabled=true to its arguments or by setting spring.aot.enabled-independent properties in application.properties that AOT reads.
Profiles are fixed
Active profiles are also fixed at build time. A profile you want in the native executable must be active during AOT processing. For the Maven plugin, set it in the plugin's profiles setting, and for Gradle use the equivalent bootRun-independent AOT configuration. Switching to another profile at runtime will not create beans that were excluded at build time. Profile-specific property files can still be read at runtime, but they cannot change which beans exist.
Runtime changes to the bean set are ignored
The same limit applies to anything that reshapes the bean set after startup. Registering bean definitions programmatically in an unsupported way, using @Conditional logic that depends on runtime state, or relying on classpath changes between build and run will not work. The classpath at runtime is the classpath at build time.
Beans created with @Bean methods, component scanning of your own packages, and standard auto-configuration all work, because AOT processing can see and record them. Code that builds beans from information only available at runtime does not.
Condition outcomes you should know
A few specifics tend to cause surprises:
@ConditionalOnBeanand@ConditionalOnMissingBeanare evaluated against the build-time bean set. That is consistent, but it can differ from what you expect if a bean depended on a runtime property.@ConditionalOnClasssees the build classpath. A class you add later is not discovered.@Valueand@ConfigurationPropertiesvalues are not fixed. They bind at runtime, so externalized configuration still works for ordinary values. Only conditions and profiles that decide which beans exist are frozen.
Runtime hints
Some behavior cannot be turned into plain code. Jackson serializes a record by reflection. A template loads from the classpath by name. For these cases, Spring AOT collects runtime hints, which tell native-image which classes need reflection, which resources to include, and which proxies to create.
Spring Framework and Spring Boot contribute hints for their own features. For your own code, a RuntimeHintsRegistrar declares the extra hints:
import org.springframework.aot.hint.MemberCategory;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsRegistrar;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportRuntimeHints;
@Configuration
@ImportRuntimeHints(AppConfig.GreetingHints.class)
public class AppConfig {
static class GreetingHints implements RuntimeHintsRegistrar {
@Override
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
hints.reflection().registerType(Greeting.class,
MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS,
MemberCategory.INVOKE_PUBLIC_METHODS);
hints.resources().registerPattern("templates/greeting.txt");
}
}
public record Greeting(String text) {}
}The registrar runs during AOT processing. Its hints are written to the generated metadata. A later chapter covers hints and metadata in detail. Here the point is that hints are part of the AOT output, alongside generated bean definitions.
Running AOT processing on the JVM
You can run AOT-generated code on a regular JVM before building a native image. This is a fast way to check that the generated initializer works, because it avoids the long native compile. Start the application with the spring.aot.enabled property set to true:
./mvnw clean package
java -Dspring.aot.enabled=true -jar target/demo.jarFor this to take effect, the AOT-generated classes must be on the classpath, which happens when you run process-aot first (./mvnw spring-boot:process-aot followed by packaging with the generated output included). With the flag on, Spring uses the generated ApplicationContextInitializer instead of scanning and evaluating conditions. Conditions then behave the way they will in the native executable, so a missing bean shows up here before you wait for a native build.
Common mistakes
- Expecting runtime properties to toggle beans. A condition on a property is decided at build time. Pass the property to the AOT step too.
- Building with one profile and running with another. Beans tied to the runtime profile will be missing.
- Reading the generated code as the source of truth. It is a build artifact. Change your configuration, then rebuild.
- Assuming all reflection is covered. Spring covers its own usage. Your own reflective access to application classes needs a hint.
Summary
Spring AOT turns your configuration into plain Java and a set of hint files before native-image runs. This removes most of the reflection needed to start the context, which is why Spring Boot can run as a native executable. The price is that conditions, profiles, and the set of beans are decided at build time and cannot change at runtime.
Exercise
You build a native executable with app.feature.enabled unset. At startup you pass --app.feature.enabled=true, but FeatureService is still missing. Why, and how do you fix it?
Show answer
@ConditionalOnProperty is evaluated during Spring AOT processing, and the result is written into the generated bean definition code. With the property unset at build time, no definition for featureService was generated, so the executable has no code to create it. Supply app.feature.enabled=true when AOT processing runs (for example through the build plugin's arguments or a property available at build time), then rebuild the native executable.
Exercise
Name two kinds of output that Spring AOT processing produces, and say how each helps native-image.
Show answer
It produces generated Java sources, such as bean definition classes and an ApplicationContextInitializer, which create beans through direct code that native-image can analyze without reflection. It also produces runtime hint metadata (reflect-config, resource-config, proxy-config), which tells native-image which classes, resources, and proxies must be kept for reflective or dynamic use.