Sub topic 8
What gets fixed at build time
This chapter assumes you know Spring Boot auto-configuration, @Conditional annotations, profiles, and the basics of how Spring Boot loads properties. It also assumes you have read the earlier chapter on Spring AOT processing.
Why the build decides
On the JVM, Spring builds its bean definitions when the application starts. It reads configuration classes, evaluates conditions against the live environment, and registers beans. In a native executable, the closed-world analysis needs the application's structure up front. So Spring AOT processing runs these steps during the build, and it generates plain Java code that creates the beans directly.
The result is faster startup. It also means that anything a condition decided during the build stays decided in the executable.
Conditions are evaluated once
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 {}
}Suppose app.feature.enabled is false or missing when AOT processing runs. The generated code then contains no featureService bean. Starting the executable with --app.feature.enabled=true or an environment variable does not add it. The condition is not checked again.
A bean whose condition was true at build time is always present. You cannot turn it off at runtime.
What else is fixed
| Item | Behavior in a native executable |
|---|---|
| `@ConditionalOnProperty` and `@ConditionalOnExpression` | Evaluated during the build against the build-time environment |
| `@ConditionalOnClass` and `@ConditionalOnMissingClass` | Decided by the build classpath, which is the only classpath there is |
| `@Profile` | Uses the profiles active during AOT processing |
| Auto-configuration | The set of applied auto-configurations is fixed |
| Bean definitions | Cannot be added or changed after startup by conditions or by registering definitions dynamically |
Values that beans only read are still resolved at runtime. A @Value("${app.greeting}") field or a @ConfigurationProperties class can pick up a different value from an environment variable or command-line argument. A property that gates whether a bean exists is different, because it is consumed by the build.
Setting profiles and properties for the build
If you use profiles to select beans, activate the right ones when AOT processing runs. With Maven, you can pass them to the process-aot goal of spring-boot-maven-plugin using its profiles setting. You can also put the values in application.properties so the build sees them.
spring.profiles.active=prod
app.feature.enabled=trueProfile choice and feature flags that affect bean existence therefore become a build decision. If you need the same code with different bean sets, such as dev and prod, build one executable per set. Alternatively, keep the beans always present and choose behavior at runtime with ordinary property values.
Checking what was generated
AOT output is generated source code. After a build, look in target/spring-aot/main/sources for Maven, or under build/generated/aot for Gradle. Searching for your configuration class's generated __BeanDefinitions file shows which beans were registered. If a bean is absent there, the condition failed during the build.
Common mistakes
- Expecting
SPRING_PROFILES_ACTIVEat runtime to switch which beans exist. - Relying on a
@ConditionalOnPropertyflag that is only set in the deployment environment, not in the build. - Assuming a bean excluded during the build can be restored by configuration.
Exercise
A native executable was built without app.feature.enabled set. You start it with APP_FEATURE_ENABLED=true, but FeatureService is still not in the context. Why, and what do you change?
Show answer
The @ConditionalOnProperty condition was evaluated during AOT processing, when the property was absent, so the generated code never creates the bean. A runtime environment variable does not trigger evaluation again. Set app.feature.enabled=true (for example in application.properties) when running AOT processing and rebuild the executable.