I was trying to understand the difference between BeanFactoryPostProcessor
and BeanPostProcessor
.
I understood that BeanFactoryPostProcessor
operates on bean definition i.e. before the bean instance is getting created it gets executed and BeanPostProcessor
gets executed after bean is instantiated and lifecycle events are called.
Does this mean BeanFactoryPostProcessor
is not a part of spring lifecycle events as it's called before instantiation while BeanPostProcessor
is the part of Spring lifecycle events? Kindly verify if my understanding is right.
BeanFactoryPostProcessor implementations are "called" during startup of the Spring context after all bean definitions will have been loaded while BeanPostProcessor are "called" when the Spring IoC container instantiates a bean (i.e. during the startup for all the singleton and on demand for the proptotypes one)
Spring's BeanPostProcessor gives us hooks into the Spring bean lifecycle to modify its configuration. BeanPostProcessor allows for direct modification of the beans themselves.
A bean post processor allows for custom modification of new bean instances created by spring bean factory. If you want to implement some custom logic after the Spring container finishes instantiating, configuring, and initializing a bean, we can plug in one or more BeanPostProcessor implementations.
BeanFactoryPostProcessor is an interface that contains single method postProcessBeanFactory , implementing it allows you to create logic that will modify Spring Bean Metadata before any Bean is created.
BeanFactoryPostProcessor
is an interface and beans that implement it are actually beans that undergo the Spring lifecycle (Example below) but these beans don't take part of the other declared beans' lifecycle.
public class CustomBeanFactory implements BeanFactoryPostProcessor { @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { for (String beanName : beanFactory.getBeanDefinitionNames()) { BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName); // Manipulate the beanDefiniton or whatever you need to do } } }
The differences about BeanFactoryPostProcessor
and BeanPostProcessor
:
BeanFactoryPostProcessor
is called when all bean definitions will have been loaded, but no beans will have been instantiated yet. This allows for overriding or adding properties even to eager-initializing beans. This will let you have access to all the beans that you have defined in XML or that are annotated (scanned via component-scan).BeanPostProcessor
operate on bean (or object) instances which means that when the Spring IoC container instantiates a bean instance then BeanPostProcessor interfaces do their work.BeanFactoryPostProcessor
implementations are "called" during startup of the Spring context after all bean definitions will have been loaded while BeanPostProcessor
are "called" when the Spring IoC container instantiates a bean (i.e. during the startup for all the singleton and on demand for the proptotypes one)If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With