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:
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
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)
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.
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.
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.
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:
postProcessBeforeInitialization(Object bean, String beanName)
is called@PostConstruct
, afterPropertiesSet()
(defined by the InitializingBean
callback interface), custom configured init
methodpostProcessAfterInitialization(Object bean, String beanName)
is calledAt 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 injectionRequiredAnnotationBeanPostProcessor
- checks if all dependencies
marked as @Required
has been injected.ServletContextAwareProcessor
- injects ServletContext
to beans
implementing ServletContextAware
interface@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.
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