Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BeanPostProcessor confusion

Tags:

java

spring

I am trying to understand BeanPostProcessor in Spring and I don't understand what it does. Is it correct that the BeanPostProcessor defines two methods that is called at these points:

  • Before initialization (either init method or afterPropertiesSet), but the instance is created.
  • After the init method or afterPropertiesSet method is called

Is that correct? Given the example and text on page 118 and further is confusing. I don't think I am allowed to copy more from the text to the question but the annotations and whats happening there is hard to understand.

And are you supposed to implement this interface on the beans you want or are you supposed to use this on a bean that is general for many beans? I see that you get both and object and string argument passed in.

Sometimes, you may find yourself in a position where you need to performsome additional processing immediately before and after Spring instantiates the bean. The processing can be as simple as modifying the bean or as complex as returning a completely different object! The BeanPostProcessor interface has two methods: postProcessBeforeInitialization, which is called before Spring calls any bean initialization hooks (such as InitializingBean.afterPropertiesSet or the init-method), and postProcessAfterInitialization, which Spring calls after the initialization hooks succeed.

Pro Spring 2.5, page 118

like image 934
LuckyLuke Avatar asked Mar 18 '12 20:03

LuckyLuke


People also ask

What is 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 a 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 the use of BeanPostProcessor?

Interface BeanPostProcessor. Factory hook that allows for custom modification of new bean instances — for example, checking for marker interfaces or wrapping beans with proxies. Typically, post-processors that populate beans via marker interfaces or the like will implement postProcessBeforeInitialization(java. lang.

Which of the following is pre existing bean factory post processors?

PropertyResourceConfigurer and PropertyPlaceHolderConfigurer – implemented as a bean factory post-processor, is used to externalize some property values from a BeanFactory definition, into another separate file in Java Properties format.


1 Answers

Spring gives you a lot of post processors, not only BeanPostProcessor. Also, most of them are used by the Spring itself. The one you mentioned in this question, is used (as its name states) to post process bean after its instantiation. Spring container behavior is as follows:

  • Spring instantiates bean calling its constructor
  • postProcessBeforeInitialization(Object bean, String beanName) is called
  • bean initialization process: @PostConstruct, afterPropertiesSet() (defined by the InitializingBean callback interface), custom configured init method
  • postProcessAfterInitialization(Object bean, String beanName) is called

At the first sight, it may look complicated and overwhelming, but when you build complex applications on a top of Spring, all these features are just invaluable.

Possible scenarios, are for example (taken from Spring itself):

  • AutowiredAnnotationBeanPostProcessor - scans bean looking for @Autowire annotation in order to perform dependency injection
  • RequiredAnnotationBeanPostProcessor - checks if all dependencies marked as @Required has been injected.
  • ServletContextAwareProcessor - injects ServletContext to beans implementing ServletContextAware interface
  • actually, initialization/desctruction callbacks such as JSR-250 @PostConstruct and @PreDestroy are implemented using post processor: CommonAnnotationBeanPostProcessor

Of course all mentioned post processors must be executed in a specific order, but this is Spring responsibility to ensure it.

like image 192
omnomnom Avatar answered Oct 04 '22 10:10

omnomnom