Scrivio Interview library
InternalsIntermediate7 min read · updated 12 November 2026

What does Spring AOT processing do at build time, and what restrictions does it impose on an application?

At build time, Spring AOT starts the application context in a special mode that computes all bean definitions without creating the beans, then generates Java source code that registers those definitions directly, so startup no longer needs classpath scanning and most reflection. It also writes RuntimeHints describing the reflection, resources, proxies and serialization a GraalVM native image needs. The price is a fixed world: the classpath and bean definitions are frozen at build time, so @Profile and @Conditional are evaluated then and cannot change which beans exist at runtime.

Why this question is asked

Spring has always done a lot of work when an application starts: it scans packages, reads annotations, evaluates conditions and builds bean definitions using reflection. That flexibility costs seconds of startup and makes GraalVM native images hard, because a native image must know in advance every class it will reflect on. Spring Framework 6 answered this with ahead-of-time (AOT) processing. Interviewers ask about it because it changes what is allowed in a Spring application, and surprises such as a profile that is ignored in production come straight from these rules. In this answer, we will recap what happens at a normal startup, see what the AOT step does instead, look at the generated code and hints, go through the restrictions, and see how to add our own hints.

1A Quick Recap of a Normal Startup

When a regular Spring Boot application starts, the container does two kinds of work. First it works out what beans exist: it scans packages for @Component classes, reads @Configuration classes and their @Bean methods, and evaluates every @Conditional, @Profile and auto-configuration condition. The result is a set of bean definitions, recipes that say which class to create and what to inject. Second, it follows those recipes to create the beans.

The first part is the same every time the same jar starts with the same settings, yet it runs on every startup and leans heavily on reflection, the Java feature that inspects classes and calls methods by name at runtime. AOT processing moves that first part to the build.

2What the AOT Step Does at Build Time

The Spring Boot Maven goal process-aot, or the Gradle task processAot, runs the application's main configuration in a build-time mode. Spring refreshes the context only far enough to compute the final bean definitions: scanning, condition evaluation and configuration parsing all happen, but the singletons are not instantiated. Then Spring turns each definition into Java code and compiles it with the application.

Step throughFrom sources to a native image
  1. Compile the applicationnormal javac output
  2. Run the AOT enginerefresh the context for AOT, no beans created
  3. Generate Java sourcesbean definitions as plain code
  4. Write RuntimeHintsreflection, resources, proxies, serialization
  5. Package the jarruns on the JVM with AOT enabled
  6. Optionally build a native imageGraalVM reads the generated metadata

6 steps in order. Play them, or step through them one at a time.

3What the Generated Code Looks Like

For each bean, the AOT engine generates a small class that builds the definition and creates the instance with a normal constructor call. The code below is simplified, and the exact shape varies by version, but it shows the idea.

Java
// Generated under target/spring-aot/main/sources (simplified)
public class OrderService__BeanDefinitions {
public static BeanDefinition getOrderServiceBeanDefinition() {
RootBeanDefinition definition = new RootBeanDefinition(OrderService.class);
definition.setInstanceSupplier(BeanInstanceSupplier.<OrderService>forConstructor(OrderRepository.class)
.withGenerator((registeredBean, args) -> new OrderService(args.get(0)))); // a direct 'new', no reflection
return definition;
}
}
// An ApplicationContextInitializer registers every such definition at startup:
// no package scan, no annotation parsing, no condition evaluation.

Notice that the constructor is called directly. Nothing needs to look up OrderService by name, which is exactly what a native image wants. Next to the sources, the engine writes the GraalVM reachability metadata, files under META-INF/native-image that list the classes needing reflection, the resource files to include, the JDK proxies to create ahead of time and the types that will be serialized. Spring collects these as RuntimeHints while it processes the context, so most hints for Spring's own features and for our beans are produced for us.

4The Restrictions of a Fixed World

Because the bean definitions are computed once at build time, the application runs in what is called a closed world: the set of beans, and the classpath they came from, cannot change afterwards. This is the main restriction, and several rules follow from it.

@Profile and @Conditional annotations, including Boot's @ConditionalOnProperty and @ConditionalOnClass, are evaluated during the build. Setting spring.profiles.active=prod at runtime does not add or remove beans; only the profiles active during the AOT run count. Property values themselves are still read at runtime, so @Value fields and @ConfigurationProperties still bind from the environment. It is only the decision about which beans exist that is frozen.

A few more rules come from the same idea. Bean definitions should declare precise types, so a @Bean method should return the concrete type it creates rather than Object, because the generated code and hints are based on that type. Logic that registers bean definitions dynamically at runtime, based on something only known after startup, cannot be captured. And a library that loads classes by name from a string read at runtime needs explicit hints, because the AOT engine cannot see it.

Java
@Configuration
class PaymentConfig {
@Bean @Profile("stub")
PaymentGateway stubGateway() { return new StubGateway(); }
@Bean @Profile("!stub")
PaymentGateway realGateway(GatewayProperties p) { return new RealGateway(p.url()); }
}
// AOT run with no profile active -> only realGateway is generated
// java -jar app.jar --spring.profiles.active=stub
// -> still RealGateway: the profile choice was made at build time
// gateway URL from application.yml or the environment -> still read at runtime

So when a team needs different beans per environment, it either builds one artifact per profile or replaces bean switching with a property read inside a single bean.

5Adding Our Own Hints

When our code reflects on a class, loads a resource by a computed name or serializes a type that Spring cannot see, we add hints ourselves. We implement RuntimeHintsRegistrar and register it with @ImportRuntimeHints on a configuration class. The AOT engine then calls it during the build.

Java
class ReportHints implements RuntimeHintsRegistrar {
@Override
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
hints.resources().registerPattern("reports/*.jrxml"); // include template files
hints.reflection().registerType(ReportRow.class,
MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS, MemberCategory.INVOKE_PUBLIC_METHODS);
}
}
@Configuration
@ImportRuntimeHints(ReportHints.class)
class ReportConfig { }

For types that are only bound with Jackson, @RegisterReflectionForBinding(ReportRow.class) is a shorter option. MemberCategory constants have been refined across releases, so check the names for your version.

6AOT on the JVM, Not Only for Native Images

The generated initializer is also useful without GraalVM. If we run the AOT-processed jar on a normal JVM with the system property spring.aot.enabled=true, Spring uses the generated definitions instead of scanning and evaluating conditions. Startup improves, often by a noticeable fraction, while the application keeps the JIT compiler's peak performance. The same closed-world restrictions then apply on the JVM, which is a good way to find problems before attempting a native build.

7Follow-up questions

How would you find missing reflection hints in a native image?

First, run the application on the JVM with the GraalVM tracing agent, -agentlib:native-image-agent=config-output-dir=some/dir, and exercise its real paths, ideally through the test suite. The agent records every reflective call and resource load, and we compare its output with what Spring generated. Second, run the tests as native tests with the Boot build plugins, so failures appear in CI rather than production. Missing hints usually show up as ClassNotFoundException, NoSuchMethodException, a missing resource or a missing registration error from GraalVM; check your GraalVM version for the exact error. Finally, we can assert hints in a unit test with RuntimeHintsPredicates, for example that reflection on ReportRow is registered.

Does AOT processing create my beans at build time?

No. It computes bean definitions, which means it loads classes and evaluates conditions, but it does not instantiate singletons. This is why code in constructors, such as connecting to a database, does not run during the build. Some infrastructure beans, such as bean post-processors, can still be touched, so heavy side effects in those are a bad idea.

8Conclusion

In this answer, we saw that Spring AOT moves the discovery part of startup to the build: it refreshes the context in a build-time mode, computes the bean definitions without creating beans, generates Java code that registers them directly and writes RuntimeHints for GraalVM. That fixes the world at build time, so the classpath and bean set are frozen and @Profile and @Conditional are decided during the build, while property values are still read at runtime. We add our own hints with a RuntimeHintsRegistrar and @ImportRuntimeHints, and we can use the AOT output on a normal JVM for faster startup.

What to say in the interviewAbout 80 seconds aloud

Open with

Spring AOT runs during the build, through the process-aot goal or the processAot task. It starts the application context in a special mode that does all the discovery work, scanning components, parsing configuration and evaluating conditions, but does not create the beans.

Then, if they want more

Then it generates Java source code that registers each bean definition directly, with plain constructor calls instead of reflection, plus an initializer that registers them all. Alongside that it produces RuntimeHints, the reflection, resource, proxy and serialization metadata GraalVM needs to build a native image.

The restriction is that the world is closed: the classpath and the set of beans are fixed at build time. So @Profile and @Conditional, including Boot's property conditions, are evaluated during the build, and setting a different active profile at runtime does not change which beans exist, although property values still bind at runtime.

Bean methods should return precise types, and anything that loads classes or resources by name needs explicit hints, which I add with a RuntimeHintsRegistrar registered through @ImportRuntimeHints.

And AOT is not only for native images: on the JVM with spring.aot.enabled set to true, the generated code speeds up startup and also lets us test the closed-world rules before a native build.

About this answer

Last updated
12 November 2026
Versions it names
Spring Framework 6
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What does Spring AOT processing do at build time, and what restrictions does it impose...": https://scrivio-interview-library.netlify.app/spring-boot/spring-framework-evolution/1edf0e31d2/