Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BeanFactoryPostProcessor and BeanPostProcessor in lifecycle events

Tags:

java

spring

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.

like image 220
jasminum Avatar asked May 26 '15 10:05

jasminum


People also ask

What is the difference between BeanPostProcessor and BeanFactoryPostProcessor?

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)

What is the use of BeanPostProcessor?

Spring's BeanPostProcessor gives us hooks into the Spring bean lifecycle to modify its configuration. BeanPostProcessor allows for direct modification of the beans themselves.

What is BeanPostProcessor?

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.

What is a BeanFactoryPostProcessor and what is it used for when is it invoked?

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.


1 Answers

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:

  1. A bean implementing 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).
  2. A bean implementing 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.
  3. 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)
like image 102
araknoid Avatar answered Sep 27 '22 17:09

araknoid