Scrivio Interview library
DebuggingIntermediate5 min read · updated 12 November 2026

A Spring Boot native image ignores a feature flag set with an environment variable at runtime: a bean annotated with @ConditionalOnProperty is never created. Explain why.

In a native image, Spring AOT processing decided the bean definitions at build time, and @ConditionalOnProperty was evaluated then, with the build's configuration, where the flag was not set. The generated code registers only the beans chosen during the build, so setting the environment variable at run time cannot add the bean. Profiles that select beans behave the same way. Property values are still bound at run time, so the fix is to register the bean unconditionally and read the flag inside it, or to decide the flag at build time on purpose.

Why this question is asked

Feature flags set through environment variables are one of the first things that surprise teams moving a Spring Boot service to a native image. The same jar on the JVM honours the flag; the native executable ignores it, without an error, which makes it hard to diagnose. Interviewers ask this to see whether we understand what Spring AOT freezes and what it does not. In this answer, we will reproduce the problem, follow how AOT decides the bean set, separate what is fixed from what still works at run time, and fix it in a way that keeps the flag usable per environment.

1Reproducing the Problem

A service has a new fraud check that should be off by default and switched on per environment with FRAUD_CHECK_ENABLED=true. Spring Boot's relaxed binding maps that environment variable to the property fraud.check.enabled.

Java
@Configuration
class FraudConfig {
@Bean
@ConditionalOnProperty(name = "fraud.check.enabled", havingValue = "true")
FraudCheck fraudCheck() { return new FraudCheck(); }
}
// On the JVM:
// FRAUD_CHECK_ENABLED=true java -jar orders.jar
// context.containsBean("fraudCheck") -> true
// Native image built from the same code:
// FRAUD_CHECK_ENABLED=true ./orders
// context.containsBean("fraudCheck") -> false (no error, the bean just is not there)

Notice that the native run raises no error. Any code that injects Optional<FraudCheck> or ObjectProvider<FraudCheck> simply sees no bean, so the check is silently skipped in production.

2How AOT Decides the Bean Set

On the JVM without AOT, Spring evaluates @ConditionalOnProperty at startup, reading the current environment. A native image cannot do that, because the reflection-heavy work of parsing configuration classes and evaluating conditions is not available. So the native build first runs Spring AOT processing. It refreshes the application context at build time, in a mode that evaluates every bean definition and condition but creates no beans, and it generates plain Java code that registers exactly the bean definitions that survived.

During that build-time evaluation, fraud.check.enabled was not set, because the CI build had no FRAUD_CHECK_ENABLED variable. The condition was false, so fraudCheck was not in the generated code. At run time, the native image runs the generated registration code; it does not evaluate @ConditionalOnProperty again. The environment variable is read and bound, but nothing asks about it when choosing beans.

Step throughWhen the condition is evaluated
CI buildSpring AOTNative image at run time
processAot with build environment (flag unset)
evaluate @ConditionalOnProperty -> false
generated code without fraudCheck
native-image compiles generated code
FRAUD_CHECK_ENABLED=true is bound as a property
registers beans from generated code: no fraudCheck

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

The decision was made once, in the build, not when the service starts.

3What Is Fixed and What Still Works

Fixed at build time: which beans exist. That covers @ConditionalOnProperty, @ConditionalOnBean, @ConditionalOnClass, and also @Profile on beans and configuration classes, because profiles are just another condition. If the build ran without the prod profile, beans that only exist under prod are absent, whatever profile is active later.

Still dynamic at run time: property values. @Value fields, @ConfigurationProperties classes and the Environment are populated from the run-time environment, so a URL, a timeout or a boolean flag read inside a bean can change per environment. That difference is the key to the fix: move the decision from whether a bean exists to what a bean does.

Under AOT and native images
FeatureDecidedChanges per environment?
@ConditionalOnProperty / @Profile on beansat build timeno
Which auto-configurations applyat build timeno
@Value and @ConfigurationProperties valuesat run timeyes
Profile-specific property filesat run time (values only)yes
Beans are frozen; values are not.

4Fix: Read the Flag Inside a Bean

We register the bean in all builds and make it check the flag when it runs. A small @ConfigurationProperties record keeps the flag typed and documented. Callers always get a FraudCheck, and it decides at run time whether to do anything.

Java
@ConfigurationProperties("fraud.check")
record FraudCheckProperties(boolean enabled) {}
@Configuration
@EnableConfigurationProperties(FraudCheckProperties.class)
class FraudConfig {
@Bean
FraudCheck fraudCheck(FraudCheckProperties props) { // always registered
return new FraudCheck(props);
}
}
class FraudCheck {
private final FraudCheckProperties props;
FraudCheck(FraudCheckProperties props) { this.props = props; }
Verdict check(Order order) {
if (!props.enabled()) return Verdict.SKIPPED; // flag read at run time
return score(order);
}
}
// FRAUD_CHECK_ENABLED=true ./orders -> check(order) scores the order
// FRAUD_CHECK_ENABLED unset ./orders -> check(order) returns SKIPPED

Now the flag works the same on the JVM, with AOT on the JVM and in the native image. For flags that must change while the service runs, a feature flag library or a refreshable configuration source fits the same pattern, since the decision already lives inside the bean.

5When Deciding at Build Time Is Right

Sometimes we really want different beans per environment, for example a different payment gateway implementation with different dependencies. Then we make the choice at build time on purpose: run AOT processing with the right profile or properties set, and produce one artifact per variant. The Maven and Gradle plugins let us pass profiles or arguments to the AOT step; check your plugin version for the exact setting. The cost is more artifacts to build and track, so we keep this for real structural differences and use runtime flags for everything else.

Java
// Gradle: run AOT with the prod profile so prod-only beans are generated
tasks.named<org.springframework.boot.gradle.tasks.aot.ProcessAot>("processAot") {
args("--spring.profiles.active=prod")
}

6Follow-up questions

Why is there no error or warning when the flag is set but the bean is missing?

From the native image's point of view, nothing is wrong: it registers the beans it was built with, and fraud.check.enabled is just a property that no condition reads any more. That is why native tests and a smoke test that checks expected beans or endpoints per environment are valuable.

Does this also happen with AOT on the JVM?

Yes. When a JVM application is started with -Dspring.aot.enabled=true, it uses the same generated bean registration code, so conditions are again frozen at build time. Without that flag, the JVM ignores the generated code and evaluates conditions normally.

Is @ConditionalOnClass a problem too?

Usually not, because the classpath is also fixed at build time for a native image. A class that is absent during the build is absent at run time as well, so the build-time answer stays correct.

7Conclusion

In this answer, we saw that a native image runs bean registration code generated by Spring AOT at build time, so @ConditionalOnProperty and @Profile were evaluated once, with the build's configuration, and a run-time environment variable cannot add the bean. Property values are still bound at run time, so we fixed the flag by always registering the bean and reading the flag inside it, and kept build-time selection with separate artifacts for real structural differences.

What to say in the interviewAbout 80 seconds aloud

Open with

The bean is missing because of Spring AOT. To build a native image, Spring first runs AOT processing at build time: it refreshes the application context in a special mode, evaluates every condition, and generates plain Java code that registers just the beans that passed. @ConditionalOnProperty was evaluated during the CI build, where the flag wasn't set, so the bean never made it into the generated code.

Then, if they want more

At runtime the native image just runs that generated registration code; it doesn't evaluate the condition again, so setting the environment variable changes nothing, and there's no error, the bean is simply absent. Profiles behave the same way: @Profile on beans is decided at build time too.

What still works at runtime is property binding: @Value, @ConfigurationProperties and the Environment get the runtime values. So the fix is to move the decision inside a bean.

I'd register the bean unconditionally, bind the flag into a @ConfigurationProperties record, and have the bean check the flag when it runs, so the toggle works the same on the JVM and native.

If we really need different beans per environment, I'd make that explicit by running AOT with the right profile and producing separate artifacts, and I'd add a smoke test per environment that checks the expected beans.

About this answer

Last updated
12 November 2026
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 "A Spring Boot native image ignores a feature flag set with an environment variable at...": https://scrivio-interview-library.netlify.app/spring-boot/native-and-aot/2ec8501453/