Scrivio Interview library
InternalsIntermediate6 min read · updated 12 November 2026

Why does Boot filter auto-configuration candidates by class presence before loading them, and how is this done without loading every class?

Boot ships well over a hundred auto-configuration candidates, and loading and parsing each class to find that most do not apply would slow every startup. So at build time, the spring-boot-autoconfigure-processor writes META-INF/spring-autoconfigure-metadata.properties, which records each candidate's conditions such as ConditionalOnClass as plain strings. At startup, AutoConfigurationImportFilter implementations such as OnClassCondition read those strings and drop candidates before their classes are loaded; the survivors are sorted by @AutoConfiguration before/after and @AutoConfigureOrder and only then fully evaluated. Writing class conditions by name, or keeping them on the class rather than on @Bean methods, avoids class loading errors for classes that are missing.

Why this question is asked

Startup time matters more than it used to: containers are restarted often, autoscaling adds instances under load, and tests start contexts hundreds of times a day. Auto-configuration could easily have been the slowest part of startup, and the filtering step is the reason it is not. Interviewers ask about it at this level because it explains a design choice that library authors must respect when they write their own auto-configurations. In this answer, we will see what would happen without filtering, how the metadata file is produced, how the filters use it, what happens to the survivors, and why conditions should name classes carefully.

1What Would Happen Without Filtering

Every Boot application starts with the full list of candidates from all AutoConfiguration.imports files on the classpath, which in Boot 3 is well over a hundred classes from Boot alone, plus any from libraries. A typical service needs only a fraction of them; an application without Redis has no use for the Redis auto-configurations.

To evaluate a candidate's conditions the normal way, Spring would have to read the class, parse its annotations, create condition objects and evaluate them, for every one of those candidates, on every startup. And there is a second problem: an auto-configuration class for Redis refers to Redis types in its methods and fields. Loading that class through reflection in an application without Redis on the classpath can fail with NoClassDefFoundError. So Boot needs a way to reject most candidates without touching their classes at all.

2The Metadata File Written at Build Time

The answer is to do the expensive part when the library is built. The spring-boot-autoconfigure-processor is an annotation processor, a compiler plugin that runs during javac. It looks at every auto-configuration class and writes its simple conditions and ordering into META-INF/spring-autoconfigure-metadata.properties inside the jar.

Properties
# META-INF/spring-autoconfigure-metadata.properties (excerpt, shape only)
com.example.audit.AuditAutoConfiguration=
com.example.audit.AuditAutoConfiguration.ConditionalOnClass=com.example.audit.AuditClient
com.example.audit.AuditAutoConfiguration.ConditionalOnWebApplication=SERVLET
com.example.audit.AuditAutoConfiguration.AutoConfigureAfter=org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration
com.example.audit.AuditAutoConfiguration.ConditionalOnBean=javax.sql.DataSource

Notice that every value is a class name written as a string. Reading this file is just reading a properties file, which is fast, and it never loads AuditAutoConfiguration itself. Boot's own jars ship this file, and library authors should add the processor to their builds too, as an annotationProcessor dependency in Gradle or an optional dependency in Maven, so that their candidates get the same fast path.

3How the Filters Drop Candidates Early

AutoConfigurationImportSelector passes the candidate list through a set of AutoConfigurationImportFilter implementations, registered in spring.factories. Boot provides three: OnClassCondition, OnWebApplicationCondition and OnBeanCondition. Each reads the metadata for a candidate and answers a quick yes or no.

OnClassCondition checks whether each class named in ConditionalOnClass can be found by the class loader; if any is missing, the candidate is dropped. OnWebApplicationCondition checks whether the application is of the required web type. OnBeanCondition does a partial check against bean definitions registered so far, so it can only reject obvious mismatches; full bean conditions are evaluated later. A candidate that passes the filters is not yet accepted; it is only no longer obviously excluded.

Step throughFrom candidate list to registered beans
  1. Read all AutoConfiguration.imports filesall candidate names
  2. Remove exclusionsspring.autoconfigure.exclude
  3. Filter with metadataOnClassCondition, OnWebApplicationCondition, OnBeanCondition
  4. Sort survivorsalphabetical, @AutoConfigureOrder, before/after
  5. Load and fully evaluate conditionsall @Conditional annotations, class and methods
  6. Register beans that passe.g. DataSource, ObjectMapper

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

4Sorting and Full Evaluation of the Survivors

The remaining candidates are put in order by AutoConfigurationSorter. It starts alphabetically so that the order is stable, then applies @AutoConfigureOrder, and finally honours before and after declarations from @AutoConfiguration(before = ..., after = ...), which win over the rest. Ordering matters because conditions such as @ConditionalOnBean only see beans registered by configurations processed earlier.

Only now are the candidates loaded and parsed as configuration classes, and every condition, including those on nested classes and @Bean methods, and complex ones like @ConditionalOnProperty or custom conditions, is evaluated fully. The condition evaluation report, printed with debug=true, records the result of both stages, so a candidate dropped by the filter still appears there with the missing class named.

5Why Conditions Should Name Classes Carefully

Spring reads annotation metadata from the class file bytes, without loading the class, while it parses configuration. That is why a class-level @ConditionalOnClass(RedisOperations.class) is safe even when Redis is absent: the annotation's value is read as a name. The danger is on @Bean methods. Before a method-level condition is checked, the JVM may already have loaded the configuration class and resolved the method's signature, and a missing type there causes a NoClassDefFoundError instead of a clean condition failure.

Java
@AutoConfiguration
@ConditionalOnClass(name = "com.example.audit.AuditClient") // by name: safe at any level
public class AuditAutoConfiguration {
// risky: the return type itself is the missing class
// @Bean @ConditionalOnClass(KafkaTemplate.class) KafkaAuditSink kafkaSink(KafkaTemplate<?, ?> t) { ... }
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(name = "org.springframework.kafka.core.KafkaTemplate")
static class KafkaSinkConfiguration { // only loaded if the class exists
@Bean KafkaAuditSink kafkaSink(KafkaTemplate<?, ?> template) { return new KafkaAuditSink(template); }
}
}

The pattern Boot itself uses is to put each optional dependency's beans into a nested configuration class guarded by @ConditionalOnClass, so the class referencing the optional type is never loaded unless the type exists. Using the name attribute with a string is the safest form, because it does not depend on how the annotation is read.

6Follow-up questions

What happens if a library does not include the metadata file?

Its candidates are not dropped early. They go through the normal path, are loaded and evaluated fully, which is slower and can cause class loading errors if the class references missing types. They still work correctly when conditions are written carefully, but startup pays for it.

Does the processor run automatically?

No. It must be added to the library's build as an annotation processor. Boot's own modules include it, and Boot's documentation recommends it for custom starters.

Can you see how long auto-configuration takes?

Yes. Spring's ApplicationStartup, for example BufferingApplicationStartup with the Actuator startup endpoint, records steps such as configuration class parsing and bean creation with timings, so we can see where startup time goes.

7Conclusion

In this answer, we saw why Boot avoids loading every candidate: it is slow and can fail on missing classes. The annotation processor writes conditions into spring-autoconfigure-metadata.properties at build time, filters such as OnClassCondition drop candidates from those strings at startup, survivors are sorted by order and before/after rules and then fully evaluated, and library authors keep optional types out of reach by naming classes and nesting configurations.

What to say in the interviewAbout 80 seconds aloud

Open with

Boot starts with every candidate from the AutoConfiguration.imports files, which is well over a hundred from Boot alone, and most don't apply to a given app. Loading and parsing each class just to find out would slow every startup, and loading a class like a Redis auto-configuration without Redis on the classpath can fail with NoClassDefFoundError.

Then, if they want more

So Boot moves the work to build time. The spring-boot-autoconfigure-processor, an annotation processor, writes META-INF/spring-autoconfigure-metadata.properties with each candidate's simple conditions and ordering as strings, like ConditionalOnClass equals some class name. At startup, AutoConfigurationImportSelector runs the AutoConfigurationImportFilter implementations, OnClassCondition, OnWebApplicationCondition and OnBeanCondition, against that metadata.

If a named class isn't on the classpath, the candidate is dropped without ever loading its configuration class. The survivors are sorted, alphabetically first, then by @AutoConfigureOrder and the before and after attributes of @AutoConfiguration, and only then loaded and fully evaluated, including method-level and property conditions.

For library authors the lesson is twofold: include the processor so your candidates get the fast path, and write class conditions safely.

Class-level @ConditionalOnClass is read from bytecode, but on @Bean methods a missing type can break class loading, so I'd use the name attribute with a string and put optional beans in a nested configuration class guarded by the condition.

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.
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 "Why does Boot filter auto-configuration candidates by class presence before loading...": https://scrivio-interview-library.netlify.app/spring-boot/auto-configuration/002aaf4fac/