Scrivio Interview library
ConceptFoundations5 min read · updated 12 November 2026

What is the difference between a BeanFactoryPostProcessor and a BeanPostProcessor?

A BeanFactoryPostProcessor works on bean definitions, the recipes for beans, after they are loaded and before any regular bean is instantiated, so it can change what will be created. A BeanPostProcessor works on bean instances, one by one, after each is created, so it can change or replace what was created. PropertySourcesPlaceholderConfigurer and ConfigurationClassPostProcessor are typical BeanFactoryPostProcessors, and BeanDefinitionRegistryPostProcessor is a subtype that can also register new definitions.

Why this question is asked

The two interfaces have almost the same name, which makes them a favourite interview question, but they work at completely different moments and on completely different things. Knowing the difference explains how @Value placeholders are resolved, how @Configuration classes become beans, and why some extensions must be declared with static @Bean methods. In this answer, we will recap bean definitions, see where each kind of post-processor runs during refresh, write a small example of each, look at the built-in examples and at BeanDefinitionRegistryPostProcessor, and finish with the rules for declaring them.

1A Quick Recap: Definitions Before Instances

Spring builds an application in two stages. First it collects bean definitions. A bean definition is a description of a bean, not the bean itself: its class or factory method, its scope, whether it is lazy, its constructor arguments and property values, its init and destroy methods. Definitions come from component scanning, @Configuration classes, @Import, auto-configuration and XML.

Second, it creates the beans from those definitions. Only now are constructors called and dependencies injected. The two kinds of post-processor plug into these two stages: one into the first, one into the second.

Step throughWhere each post-processor works during refresh
  1. Load bean definitionsscanning, @Configuration, imports
  2. BeanDefinitionRegistryPostProcessormay add or remove definitions
  3. BeanFactoryPostProcessormay change existing definitions
  4. Register BeanPostProcessorsthey are beans too, created now
  5. Create each singletonBeanPostProcessor before and after initialisation
  6. Context ready

6 layers, from the top down. Step through them one at a time.

Factory post-processors see recipes; bean post-processors see objects.

2BeanFactoryPostProcessor: Changing the Recipes

BeanFactoryPostProcessor has one method, postProcessBeanFactory(ConfigurableListableBeanFactory). Spring calls it once, after all definitions are loaded and before any regular bean is instantiated. Through the factory we can read and modify every definition.

Java
public class ReportLazyMarker implements BeanFactoryPostProcessor {
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory factory) {
System.out.println(factory.getBeanDefinitionCount() + " definitions"); // 214 definitions
BeanDefinition bd = factory.getBeanDefinition("reportEngine");
bd.setLazyInit(true); // will not be created at startup
System.out.println(factory.containsSingleton("reportEngine")); // false: no instance exists yet
}
}

Notice the last line: no reportEngine instance exists yet, and that is the point. At this stage there are only recipes, so a factory post-processor changes what Spring will build, such as scope, laziness, property values or the class itself. It should not create beans; calling getBean() here would create a bean before the bean post-processors exist, and that bean would miss injection and proxies.

3BeanPostProcessor: Changing the Objects

BeanPostProcessor has two methods, postProcessBeforeInitialization and postProcessAfterInitialization, and Spring calls both for every bean instance it creates, around the bean's init callbacks. Here we receive real objects.

Java
public class StartupLogger implements BeanPostProcessor {
@Override
public Object postProcessAfterInitialization(Object bean, String name) {
if (name.equals("reportEngine")) {
System.out.println("created " + bean.getClass().getSimpleName()); // created ReportEngine (on first use)
}
return bean; // or return a wrapper, which then replaces the bean
}
}

A bean post-processor can call methods on the bean, check its annotations, or return a different object such as a proxy. Spring's own @Autowired injection, @PostConstruct handling and AOP proxies all work this way. What it cannot do is change the recipe: by the time it runs, the decision to create this bean, with this class and scope, has already been made.

4Built-in Examples of Factory Post-Processors

PropertySourcesPlaceholderConfigurer is the classic one. It resolves ${...} placeholders in bean definitions, for example in XML property values, against the Environment, and it registers an embedded value resolver that @Value uses when the beans are later created. Spring Boot registers it automatically.

ConfigurationClassPostProcessor is the most important one. It finds @Configuration classes and processes @ComponentScan, @Import, @PropertySource and @Bean methods, registering a definition for each bean they declare. Without it, an annotation-based application would have almost no definitions at all. It can do that because it is a BeanDefinitionRegistryPostProcessor, the subtype that can add definitions.

5BeanDefinitionRegistryPostProcessor: Adding Definitions

BeanDefinitionRegistryPostProcessor extends BeanFactoryPostProcessor with one extra method, postProcessBeanDefinitionRegistry(BeanDefinitionRegistry). Spring calls these first, before any plain factory post-processor, and through the registry they can register new definitions or remove existing ones.

Java
public class ClientRegistrar implements BeanDefinitionRegistryPostProcessor {
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) {
for (String region : List.of("eu", "us")) {
var bd = BeanDefinitionBuilder.genericBeanDefinition(RegionClient.class)
.addConstructorArgValue(region)
.getBeanDefinition();
registry.registerBeanDefinition(region + "Client", bd); // euClient and usClient now exist as recipes
}
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory factory) { } // nothing to change
}
The two kinds side by side
BeanFactoryPostProcessorBeanPostProcessor
Works onbean definitions (recipes)bean instances (objects)
Calledonce, before any regular bean existsfor every bean, around its init callbacks
Canchange scope, laziness, values; subtype adds definitionsmodify the object or replace it with a proxy
ExamplesPropertySourcesPlaceholderConfigurer, ConfigurationClassPostProcessorAutowiredAnnotationBeanPostProcessor, auto-proxy creator
Declare withstatic @Bean methodstatic @Bean method, no regular dependencies

The reason registry post-processors run first is that they add the recipes everyone else works on. A plain BeanFactoryPostProcessor that runs later sees the definitions they added, and the bean post-processors later process the beans built from them.

6Follow-up questions

Why should a @Bean method returning a BeanFactoryPostProcessor be static?

Because factory post-processors run before regular beans exist. An instance method would force Spring to create the @Configuration class very early, before the bean post-processors that handle @Autowired, @Value and @PostConstruct exist, so those annotations in that configuration class would not be processed. Spring logs a warning suggesting the static modifier for this reason.

Can a BeanFactoryPostProcessor use @Value or @Autowired?

Not reliably, because the post-processors that handle those annotations are not active yet when it is created. If it needs configuration, it can implement EnvironmentAware and read properties from the Environment, or use Spring Boot's Binder on that Environment.

In what order do several factory post-processors run?

Registry post-processors first, then plain factory post-processors, and within each group PriorityOrdered ones, then Ordered ones, then the rest. ConfigurationClassPostProcessor is PriorityOrdered with high precedence, so the definitions it registers exist before most others run.

7Conclusion

In this answer, we saw that Spring first collects bean definitions and then creates beans, and that the two post-processors plug into these two stages. A BeanFactoryPostProcessor runs once, before any regular bean exists, and changes definitions; PropertySourcesPlaceholderConfigurer and ConfigurationClassPostProcessor are examples. A BeanPostProcessor runs for every bean instance and can modify or replace it. BeanDefinitionRegistryPostProcessor is a factory post-processor that runs first and can register new definitions, which is how @Configuration processing adds most of an application's beans.

What to say in the interviewAbout 70 seconds aloud

Open with

The difference is what they work on and when. A BeanFactoryPostProcessor works on bean definitions, the descriptions of beans with their class, scope, laziness and property values. Spring calls it once, after all definitions are loaded and before any regular bean is instantiated, so it can change what will be created, for example mark a bean lazy or change a property value.

Then, if they want more

A BeanPostProcessor works on bean instances: Spring calls it for every bean as it is created, before and after the bean's init callbacks, and it can modify the object or return a replacement, which is how AOP proxies and @Autowired injection work.

Two standard factory post-processors are PropertySourcesPlaceholderConfigurer, which resolves ${...} placeholders and supports @Value, and ConfigurationClassPostProcessor, which processes @Configuration, @ComponentScan, @Import and @Bean methods. That second one is a BeanDefinitionRegistryPostProcessor, a subtype that runs first and can register new definitions, not just change existing ones.

Because factory post-processors run so early, I declare them with static @Bean methods and never call getBean inside them, since any bean created there would miss the bean post-processors.

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 "What is the difference between a BeanFactoryPostProcessor and a BeanPostProcessor?": https://scrivio-interview-library.netlify.app/spring-boot/bean-lifecycle/c0849a48b9/