Scrivio Interview library
InternalsIntermediate7 min read · updated 12 November 2026

How does ConfigurationClassPostProcessor turn @Configuration, @ComponentScan and @Import into bean definitions?

ConfigurationClassPostProcessor is a BeanDefinitionRegistryPostProcessor that Spring runs early in refresh, before any regular bean is created. It finds configuration class candidates, and its parser processes each one: @PropertySource, @ComponentScan (scanning immediately), @Import, @ImportResource and @Bean methods, parsing scanned and imported configuration classes recursively and evaluating @Conditional along the way. A reader then registers bean definitions for what was found, the loop repeats for new candidates, and in a later step full @Configuration classes are enhanced with CGLIB so calls between @Bean methods return the container's singletons.

Background to this question

When we write @SpringBootApplication and a few @Configuration classes, hundreds of beans appear. The class that makes this happen is ConfigurationClassPostProcessor, and understanding it explains many practical behaviours: why @ComponentScan finds classes in sub-packages, why auto-configuration is processed after our own configuration, why a condition can see some beans but not others, and why calling one @Bean method from another returns the same instance. In this answer, we will see where it runs in refresh, how candidates are chosen, how the parser handles each annotation, how conditions fit in, how definitions are registered, and finally the CGLIB enhancement step.

1Where It Runs in the Refresh

An annotation-based context registers a few infrastructure bean definitions as soon as it is created, and ConfigurationClassPostProcessor is one of them. During refresh, the context calls invokeBeanFactoryPostProcessors, which runs the factory post-processors before any regular bean is instantiated. Our class implements BeanDefinitionRegistryPostProcessor, so it gets postProcessBeanDefinitionRegistry and can add definitions, and it implements PriorityOrdered, so it runs before almost every other factory post-processor.

At this point the registry contains very little: the infrastructure definitions and the classes we registered directly, such as the main application class in Spring Boot. Everything else will be discovered from there.

Step throughConfigurationClassPostProcessor during refresh
  1. Find candidates in the registryfull @Configuration or lite classes
  2. Parse each candidatemember classes, @PropertySource, @ComponentScan, @Import, @ImportResource, @Bean
  3. Process deferred importsauto-configuration selectors
  4. Register definitionsimported classes, @Bean methods, XML, registrars
  5. New candidates registered?
    YesParse them in another roundNoParsing complete
  6. postProcessBeanFactory: enhance full classesCGLIB subclass for @Bean method calls

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

2Choosing the Candidates

The processor looks at every definition and checks whether it is a configuration class. A class annotated with @Configuration and the default proxyBeanMethods = true is a 'full' configuration class. A class that only has @Component, @ComponentScan, @Import or @ImportResource, or that simply declares @Bean methods, is a 'lite' configuration class, as is @Configuration(proxyBeanMethods = false). Both kinds are parsed; the difference matters only for the CGLIB step at the end.

The candidates are sorted by @Order and handed to ConfigurationClassParser, which builds a ConfigurationClass model for each, a description of everything the class contributes.

3How the Parser Processes One Class

For each configuration class, the parser walks through a fixed sequence, and this sequence explains many ordering behaviours.

Java
@Configuration
@PropertySource("classpath:billing.properties") // 1 added to the Environment immediately
@ComponentScan("com.shop.billing") // 2 scanned now; found config classes parsed recursively
@Import({TaxConfig.class, AuditRegistrar.class}) // 3 a plain class, a selector or a registrar
@ImportResource("classpath:legacy.xml") // 4 remembered; XML loaded at registration time
class BillingConfig {
@Bean Invoicer invoicer(TaxTable taxes) { // 5 recorded as a bean method
return new Invoicer(taxes);
}
}

First, member classes that are themselves configuration classes are processed. Then @PropertySource is added to the Environment right away, so that later conditions and placeholders can see those properties. Then @ComponentScan runs immediately: a scanner walks the package, registers a definition for every @Component it finds, and any of those that is itself a configuration class is parsed recursively, at once. That is why scanning finds configuration in sub-packages and why those classes can scan or import further.

Then @Import is handled, and it comes in three flavours. A plain class is parsed as a configuration class. An ImportSelector returns class names to import, which are processed the same way. An ImportBeanDefinitionRegistrar is stored to be called later with the registry, so it can register definitions programmatically. A DeferredImportSelector is a special selector whose processing waits until all other configuration classes have been parsed; Spring Boot's auto-configuration is imported this way, which is why our own configuration is always seen before auto-configuration and @ConditionalOnMissingBean can back off when we define a bean.

Finally @ImportResource locations are remembered, @Bean methods are recorded, including default methods from interfaces, and the parser moves to the superclass, if it has not been processed yet, and repeats the sequence for it.

4Conditions During Parsing and Registration

@Conditional is evaluated at two moments, called configuration phases. In the PARSE_CONFIGURATION phase, before a class is parsed at all, the parser asks whether conditions on it allow it; if not, the class and everything it would scan or import are skipped. In the REGISTER_BEAN phase, conditions are checked again when definitions are registered, including conditions on individual @Bean methods.

Conditions that look at other beans, such as Spring Boot's @ConditionalOnBean and @ConditionalOnMissingBean, only make sense in the second phase, because during parsing the definitions they look for may not be registered yet. They implement ConfigurationCondition to declare that phase. Even then, they can only see definitions registered so far, which is why the order of configuration matters, and why Spring Boot processes auto-configuration last and orders auto-configuration classes with before and after attributes.

5Registering the Definitions

Once parsing is done, ConfigurationClassBeanDefinitionReader turns the models into definitions. For each configuration class it registers the class itself if it was imported, registers one definition per @Bean method, loads the remembered XML resources and calls the ImportBeanDefinitionRegistrars with the registry. A @Bean definition does not name a class to instantiate; it names a factory bean, the configuration class, and a factory method, the @Bean method, together with the scope, init and destroy methods taken from the annotation.

Registrars and imported XML can register new configuration classes. So the processor checks the registry again for candidates it has not parsed and, if it finds any, runs another round of parsing and registration, until nothing new appears.

6The Later Step: Enhancing Full Configuration Classes

The processor's second method, postProcessBeanFactory, runs afterwards. For every full @Configuration class it replaces the bean class in the definition with a CGLIB subclass. The subclass intercepts calls to @Bean methods: when one @Bean method calls another, the call is redirected to the container, which returns the existing singleton instead of running the method again.

Java
@Configuration // full: enhanced with CGLIB
class DataConfig {
@Bean DataSource dataSource() { return new HikariDataSource(cfg()); }
@Bean JdbcTemplate jdbc() { return new JdbcTemplate(dataSource()); } // returns the container's dataSource bean
}
ctx.getBean(DataConfig.class).getClass().getSimpleName(); // DataConfig$$SpringCGLIB$$0
ctx.getBean(JdbcTemplate.class).getDataSource() == ctx.getBean(DataSource.class); // true
// With @Configuration(proxyBeanMethods = false): no subclass, dataSource() runs again
// and creates a second, unmanaged pool, so the comparison prints false

Notice why this is a separate, later step. Parsing must finish first so that every bean method is known, and the enhancement must happen before any configuration class is instantiated. Spring Boot's own auto-configurations use proxyBeanMethods = false, because skipping CGLIB makes startup cheaper and they pass dependencies as method parameters instead of calling other @Bean methods.

7Follow-up questions

Why does auto-configuration run after our own configuration?

Because it is imported through a DeferredImportSelector, AutoConfigurationImportSelector, whose imports are processed only after every other configuration class has been parsed. That ordering lets conditions like @ConditionalOnMissingBean see the beans we defined and skip the defaults.

What is the difference between full and lite configuration classes?

Both are parsed and their @Bean methods registered. Only full classes, @Configuration with proxyBeanMethods = true, are enhanced with CGLIB, so only in them does a call from one @Bean method to another return the container's singleton. In lite classes the call is a plain Java call that creates a new object.

How does this work with Spring AOT?

During the AOT build, the same parsing runs at build time and its result is written out as generated code that registers the definitions. At runtime in AOT mode, the generated code registers them directly, so the scanning and parsing work is skipped (check your version for the details).

8Conclusion

In this answer, we followed ConfigurationClassPostProcessor through the refresh. As a BeanDefinitionRegistryPostProcessor with high priority, it runs before regular beans exist, finds full and lite configuration candidates, and parses each one: member classes, @PropertySource, @ComponentScan with recursive parsing, the three kinds of @Import, with deferred selectors last, @ImportResource and @Bean methods. Conditions are checked in the parse and register phases, the reader registers definitions in rounds until nothing new appears, and full @Configuration classes are enhanced with CGLIB in postProcessBeanFactory.

What to say in the interviewAbout 70 seconds aloud

Open with

ConfigurationClassPostProcessor is a BeanDefinitionRegistryPostProcessor, registered automatically by annotation-based contexts and PriorityOrdered, so it runs early in refresh, before any regular bean is created. It looks through the registry for configuration candidates: full ones, @Configuration with proxy bean methods, and lite ones with @Component, @Import or @Bean methods.

Then, if they want more

The ConfigurationClassParser then processes each class in order: member classes, @PropertySource, which is added to the Environment immediately, @ComponentScan, which scans right away and parses any scanned configuration classes recursively, then @Import, where a plain class is parsed as configuration, an ImportSelector returns more classes, a registrar is kept to be called with the registry, and a DeferredImportSelector, which is how auto-configuration comes in, waits until everything else is parsed.

Then @ImportResource and @Bean methods are recorded, and it repeats for the superclass. Conditions are evaluated in the parse phase and again in the register phase, which is where conditions on other beans apply. ConfigurationClassBeanDefinitionReader then registers the definitions, @Bean methods as factory methods, loads XML, calls registrars, and the loop repeats for new candidates.

Finally postProcessBeanFactory enhances full @Configuration classes with CGLIB so inter-bean method calls return the singleton.

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 "How does ConfigurationClassPostProcessor turn @Configuration, @ComponentScan and @Import...": https://scrivio-interview-library.netlify.app/spring-boot/bean-lifecycle/1b4b7c9125/