Scrivio Interview library
CodingFoundations5 min read · updated 12 November 2026

Read this:

Java
@Bean
@ConditionalOnProperty(prefix = "feature.audit", name = "enabled", havingValue = "true", matchIfMissing = false)
AuditService audit() { ... }

When is the bean created?

The AuditService bean is created only when feature.audit.enabled is set to true. Because matchIfMissing = false, the bean is skipped when the property is absent, and because havingValue = "true", any other value, including false, off or a typo, also skips it (the comparison ignores case, so TRUE works). If havingValue were left out, the rule would be different: any value except false would match.

Why this question is asked

Feature flags in Spring Boot are very often written with @ConditionalOnProperty, and small differences in its attributes decide whether a feature is on by default, off by default, or on by accident. Interviewers show this exact line to check that we can read the attributes precisely, especially the two that people mix up, havingValue and matchIfMissing. In this answer, we will recap how the condition builds and reads the property, go through each case with its result, see what changes without havingValue, and finish with how to test the rule and the newer annotation for boolean flags.

1A Quick Recap of @ConditionalOnProperty

@ConditionalOnProperty decides whether a bean or a configuration class is registered by looking at a property in the Spring Environment, which includes application.properties, YAML files, environment variables and command-line arguments. The prefix and name attributes are joined to form the property key, here feature.audit.enabled. havingValue is the value the property must have for the condition to match. matchIfMissing says what happens when the property is not set at all.

Because the Environment uses relaxed binding for these keys, the property can come from FEATURE_AUDIT_ENABLED=true as an environment variable or --feature.audit.enabled=true on the command line, as well as from a properties file.

2Walking Through Every Case

The condition first looks the property up. If it is missing, matchIfMissing decides. If it is present, its value is compared with havingValue, ignoring case.

Java
@Bean
@ConditionalOnProperty(prefix = "feature.audit", name = "enabled", havingValue = "true", matchIfMissing = false)
AuditService audit() { return new AuditService(); }
// feature.audit.enabled=true -> bean created
// feature.audit.enabled=TRUE -> bean created (case is ignored)
// feature.audit.enabled=false -> no bean
// feature.audit.enabled=yes -> no bean (not "true")
// feature.audit.enabled= -> no bean (empty string is present but not "true")
// property not set at all -> no bean (matchIfMissing = false)
Step throughHow the condition decides
  1. Is feature.audit.enabled set?any source in the Environment
    YesCompare the valueNoUse matchIfMissing
  2. matchIfMissing = true?here: false
    YesCreate the beanNoSkip the bean
  3. Value equals "true", ignoring case?havingValue = "true"
    YesCreate the beanNoSkip the bean

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

Notice the yes case. Some people expect it to count as true, but the condition compares strings, not booleans, so only the exact text true, in any case, matches. A typo such as ture silently leaves the feature off, which is the safe failure for an audit feature but can be confusing to debug.

Since matchIfMissing defaults to false, writing it here changes nothing; it is there to make the intent obvious to the reader.

3What Changes Without havingValue

If havingValue is left out, the condition does not look for a particular value. Instead it matches when the property is present and its value is anything other than false. This default is designed for flags that are on unless explicitly switched off.

Java
@ConditionalOnProperty(prefix = "feature.audit", name = "enabled")
// feature.audit.enabled=true -> bean created
// feature.audit.enabled=yes -> bean created (anything but false)
// feature.audit.enabled=false -> no bean
// property not set at all -> no bean (matchIfMissing still defaults to false)

So with the default havingValue, a value of yes or 1 turns the bean on, which is the opposite of the explicit version. That is the difference the question is testing: havingValue = "true" means only true matches; no havingValue means everything except false matches.

4Choosing the Default: On or Off

The two attributes together express the feature's default state. A feature that should be off unless someone turns it on, like a new audit trail, uses havingValue = "true" and matchIfMissing = false, which is exactly the line in the question. A feature that should be on unless someone turns it off uses matchIfMissing = true, so a service that never mentions the property still gets the bean.

Boot uses the second pattern for many of its own features, which is why, for example, a feature is often active until we set its enabled property to false. For platform libraries, being explicit about the default, and documenting the property in configuration metadata so IDEs show it, saves a lot of guesswork for consuming teams.

5Testing the Rule and the Boolean Annotation

ApplicationContextRunner makes it easy to check each case in a fast test, without starting a full application.

Java
private final ApplicationContextRunner runner = new ApplicationContextRunner()
.withUserConfiguration(AuditConfig.class);
@Test void offWhenMissing() { runner.run(ctx -> assertThat(ctx).doesNotHaveBean(AuditService.class)); }
@Test void onWhenTrue() { runner.withPropertyValues("feature.audit.enabled=true")
.run(ctx -> assertThat(ctx).hasSingleBean(AuditService.class)); }
@Test void offWhenYes() { runner.withPropertyValues("feature.audit.enabled=yes")
.run(ctx -> assertThat(ctx).doesNotHaveBean(AuditService.class)); }

Spring Boot 3.5 added @ConditionalOnBooleanProperty, which reads the property as a boolean and is clearer for this kind of flag, for example @ConditionalOnBooleanProperty("feature.audit.enabled"), with havingValue and matchIfMissing as boolean attributes. On older versions, @ConditionalOnProperty with havingValue = "true" is the standard way; check your version.

6Follow-up questions

Can @ConditionalOnProperty check several properties at once?

Yes. name accepts an array, and the condition matches only if every listed property passes the same havingValue test. For an either-or rule, a custom condition or AnyNestedCondition is needed.

Does the condition see properties set in @PropertySource on a regular configuration class?

Only if that property source has been added to the Environment before the condition is evaluated. Conditions on auto-configurations run late enough to see it, but conditions on user configuration can run before a later @PropertySource is processed, so feature flags belong in application properties or the environment.

How do you see why the bean was skipped?

The condition evaluation report, with debug=true, lists it under negative matches with a message such as did not find property 'enabled' or found different value in property 'enabled'.

7Conclusion

In this answer, we read the condition attribute by attribute: the bean exists only when feature.audit.enabled is true, ignoring case; matchIfMissing = false skips it when the property is absent; every other value skips it; and without havingValue, any value except false would match. We also tested each case with ApplicationContextRunner and met @ConditionalOnBooleanProperty from Boot 3.5.

What to say in the interviewAbout 80 seconds aloud

Open with

The bean is created only when feature.audit.enabled is set to true. The prefix and name are joined to form the key, and it can come from any property source, a properties file, an environment variable like FEATURE_AUDIT_ENABLED, or the command line.

Then, if they want more

If the property isn't set at all, matchIfMissing decides, and it's false here, which is also the default, so the bean is skipped. If the property is set, its value is compared with havingValue, which is the string true, ignoring case.

So true or TRUE create the bean, and anything else skips it: false, but also yes, 1, an empty value or a typo. That makes this an off-by-default flag, which is what I'd want for something like auditing. The subtle part is what happens without havingValue.

Then the condition matches any value except false, so yes or 1 would turn the bean on. With havingValue equals true, only true matches. If I wanted the opposite default, on unless switched off, I'd set matchIfMissing to true.

I'd test each case quickly with ApplicationContextRunner and withPropertyValues, and if it's ever skipped unexpectedly, the condition evaluation report shows the reason under negative matches. On Boot 3.5 and later, @ConditionalOnBooleanProperty expresses this more directly.

About this answer

Last updated
12 November 2026
Versions it names
Spring Boot 3.5
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.
Official documentation
Spring Boot reference
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 "Read this: @Bean @ConditionalOnProperty(prefix = "feature.audit", name = "enabled"...": https://scrivio-interview-library.netlify.app/spring-boot/auto-configuration/4bb0fad438/