Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

What is a BeanPostProcessor, and give three examples Spring uses internally.

A BeanPostProcessor is a special bean that Spring calls for every other bean, once before and once after the bean's initialisation callbacks, and that may modify the bean or return a different object to replace it. Spring builds much of its own behaviour this way: AutowiredAnnotationBeanPostProcessor handles @Autowired and @Value injection, CommonAnnotationBeanPostProcessor handles @PostConstruct, @PreDestroy and @Resource, and the AOP auto-proxy creator wraps beans in proxies for @Transactional and aspects, with AsyncAnnotationBeanPostProcessor doing the same for @Async.

Why this question is asked

Most Spring annotations do nothing on their own. Something has to find them and act, and in most cases that something is a BeanPostProcessor. Interviewers ask this question because once we understand post-processors, a lot of Spring stops being magic: we can explain why injection happens after the constructor, why @Transactional only works through a proxy, and how a library can add behaviour to beans it has never seen. In this answer, we will look at the interface and where it is called, write a tiny one to watch it work, go through three post-processors Spring uses internally, and see how returning a different object replaces the bean.

1The Interface and Where It Is Called

BeanPostProcessor is a small interface in org.springframework.beans.factory.config with two methods, both with default implementations that return the bean unchanged. postProcessBeforeInitialization(bean, beanName) is called after the bean has been created and its dependencies injected, but before its init callbacks such as afterPropertiesSet() and a custom init method. postProcessAfterInitialization(bean, beanName) is called after those callbacks.

Spring detects every bean that implements the interface, creates those beans first, sorts them by PriorityOrdered, Ordered and the rest, and then calls them for every regular bean it creates. Two details matter. The processor sees the bean instance, not its definition, so it works on objects rather than configuration. And whatever it returns becomes the bean, which is what makes replacement possible.

Step throughWhere post-processors run for one bean
  1. Instantiateconstructor
  2. PopulateAutowiredAnnotationBeanPostProcessor injects fields and setters
  3. Aware callbacks
  4. postProcessBeforeInitializationevery BPP; @PostConstruct runs here
  5. Init callbacksafterPropertiesSet, custom init method
  6. postProcessAfterInitializationevery BPP; proxies created here
  7. The returned object is the bean

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

2Watching One Work

A minimal post-processor that only prints what it sees shows the two calls clearly.

Java
@Component
class PrintingPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String name) {
if (name.equals("greeter")) System.out.println("before init: " + name);
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String name) {
if (name.equals("greeter")) System.out.println("after init: " + bean.getClass().getSimpleName());
return bean;
}
}
@Component
class Greeter {
@PostConstruct void init() { System.out.println("@PostConstruct"); }
}
// before init: greeter
// @PostConstruct
// after init: Greeter

Notice that @PostConstruct runs between our two lines. That is because it is itself run by another post-processor in the 'before' step, one that is ordered ahead of ours. Also notice the filter by name: a real post-processor is called for every bean in the context, often hundreds, so it should decide quickly whether a bean concerns it.

3Example 1: AutowiredAnnotationBeanPostProcessor

This is the post-processor behind @Autowired, @Value and @Inject on fields, setters and methods. It implements an extended interface, InstantiationAwareBeanPostProcessor, which adds a hook called postProcessProperties that runs during the population step, before the initialisation callbacks. In that hook it looks up the injection points it found on the class, resolves each dependency from the bean factory and sets the field or calls the method.

This explains a behaviour beginners often stumble on: an @Autowired field is null in the constructor. The field can only be set on an object that exists, so this post-processor runs after the constructor returns. Constructor injection is the one form of injection that happens without it, during instantiation itself.

4Example 2: CommonAnnotationBeanPostProcessor

This one handles the Jakarta Common Annotations: @PostConstruct, @PreDestroy and @Resource. It scans each bean class once for these annotations and caches what it finds. In postProcessBeforeInitialization it calls the @PostConstruct methods, which is why they run before afterPropertiesSet(). It is also a DestructionAwareBeanPostProcessor, a variant with a hook called at destruction, and there it calls the @PreDestroy methods. For @Resource it injects by name during population, the same way the autowired processor injects by type.

5Example 3: The AOP Auto-Proxy Creator

The third example creates the proxies behind @Transactional, @Cacheable and @Aspect classes. In a Spring Boot application it is usually AnnotationAwareAspectJAutoProxyCreator. In postProcessAfterInitialization it collects the advisors, the pieces of extra behaviour, that apply to the bean's methods. If any apply, it creates a proxy, a JDK dynamic proxy or a CGLIB subclass, that runs that behaviour around each call, and returns the proxy instead of the bean.

@Async works in the same way but through its own post-processor, AsyncAnnotationBeanPostProcessor, which either adds its advisor to an existing proxy or creates a proxy of its own. Either way, the effect is that the context stores the proxy, and every other bean that injects this one receives the proxy.

Three post-processors Spring registers for us
Post-processorHandlesHook it mainly uses
AutowiredAnnotationBeanPostProcessor@Autowired, @Value, @InjectpostProcessProperties (populate)
CommonAnnotationBeanPostProcessor@PostConstruct, @PreDestroy, @Resourcebefore initialization, before destruction
AnnotationAwareAspectJAutoProxyCreator@Transactional, @Cacheable, @AspectpostProcessAfterInitialization
AsyncAnnotationBeanPostProcessor@AsyncpostProcessAfterInitialization

6Returning a Different Object Replaces the Bean

Because the context stores whatever postProcessAfterInitialization returns, a post-processor can replace the bean entirely. This is how proxies work, and it is how we can add behaviour ourselves.

Java
@Bean
static BeanPostProcessor loggingWrapper() {
return new BeanPostProcessor() {
public Object postProcessAfterInitialization(Object bean, String name) {
if (!(bean instanceof PaymentGateway)) return bean;
ProxyFactory pf = new ProxyFactory(bean);
pf.addAdvice((MethodInterceptor) inv -> {
System.out.println("calling " + inv.getMethod().getName());
return inv.proceed();
});
return pf.getProxy(); // this proxy is now the 'paymentGateway' bean
}
};
}
gateway.charge(order); // calling charge

Notice two things. The @Bean method is static, so Spring can create the post-processor without first creating the configuration class and its dependencies. And because the proxy here implements the PaymentGateway interface, any bean that injects the concrete class instead of the interface would fail, since the proxy is not an instance of that class. Replacing beans is powerful, so it should be done with care.

7Follow-up questions

What is the difference between a BeanPostProcessor and a BeanFactoryPostProcessor?

A BeanFactoryPostProcessor runs once, before any regular bean is created, and works on bean definitions, the configuration describing the beans. A BeanPostProcessor runs for every bean instance as it is created. So the first can change what will be created, and the second can change or replace what was created.

Can a post-processor return null?

Spring keeps the current bean when a post-processor returns null, but it also stops calling the remaining post-processors for that bean, so later processing such as proxying can be skipped. Always return the bean, or a replacement. To remove a bean we must work on its definition with a BeanFactoryPostProcessor instead.

Why should a BeanPostProcessor not inject other beans?

Post-processors are created before regular beans. If one injects a regular bean, that bean is created early, before the remaining post-processors are registered, so it may miss them, including the proxy creator, and Spring logs that it is not eligible for getting processed by all BeanPostProcessors.

8Conclusion

In this answer, we saw that a BeanPostProcessor is called for every bean, before and after its initialisation callbacks, and that whatever it returns becomes the bean. Spring itself uses post-processors for most annotation support: AutowiredAnnotationBeanPostProcessor for @Autowired and @Value, CommonAnnotationBeanPostProcessor for @PostConstruct, @PreDestroy and @Resource, and the auto-proxy creator for @Transactional, caching and aspects, with AsyncAnnotationBeanPostProcessor for @Async. Returning a proxy from postProcessAfterInitialization replaces the bean, which is powerful but changes its type for anyone who injects it.

What to say in the interviewAbout 70 seconds aloud

Open with

A BeanPostProcessor is a bean that Spring calls for every other bean as it is created. It has two methods: postProcessBeforeInitialization, called after injection but before init callbacks like afterPropertiesSet, and postProcessAfterInitialization, called after them.

Then, if they want more

Whatever the method returns becomes the bean, so a post-processor can modify the instance or replace it entirely. A lot of Spring is built on this. AutowiredAnnotationBeanPostProcessor handles @Autowired and @Value: it uses the extended instantiation-aware hook to inject fields and setters during population, which is why an @Autowired field is still null in the constructor.

CommonAnnotationBeanPostProcessor handles @PostConstruct, @PreDestroy and @Resource; it calls @PostConstruct in the before-initialisation step and @PreDestroy at destruction. And the AOP auto-proxy creator, usually AnnotationAwareAspectJAutoProxyCreator in Boot, looks at each bean in the after-initialisation step and, if @Transactional, caching or an aspect applies, returns a proxy instead of the bean. @Async has its own post-processor that works the same way.

Because the proxy replaces the bean, everyone who injects it gets the proxy. When I write my own, I declare it with a static @Bean method and avoid injecting other beans into it.

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 a BeanPostProcessor, and give three examples Spring uses internally.": https://scrivio-interview-library.netlify.app/spring-boot/bean-lifecycle/587300a39f/