Interface BeanPostProcessor
- All Known Subinterfaces:
DestructionAwareBeanPostProcessor, InstantiationAwareBeanPostProcessor, MergedBeanDefinitionPostProcessor, SmartInstantiationAwareBeanPostProcessor
- All Known Implementing Classes:
AutowiredAnnotationBeanPostProcessor, InitDestroyAnnotationBeanPostProcessor
Typically, post-processors that populate beans via marker interfaces
or the like will implement postProcessBeforeInitialization(Object, String),
while post-processors that wrap beans with proxies will normally
implement postProcessAfterInitialization(Object, String).
Registration
An ApplicationContext can autodetect BeanPostProcessor beans
in its bean definitions and apply those post-processors to any beans subsequently
created. A plain BeanFactory allows for programmatic registration of
post-processors, applying them to all beans created through the bean factory.
When registering a BeanPostProcessor via an @Bean method in
a @Configuration class, use a static method with ideally no
dependencies in order to avoid eager initialization that can make other beans
ineligible for full post-processing. See the "BeanPostProcessor-returning
@Bean methods" section in
invalid reference
@Bean
Ordering
BeanPostProcessor beans that are autodetected in an
ApplicationContext will be ordered according to
PriorityOrdered and
Ordered semantics. In contrast,
BeanPostProcessor beans that are registered programmatically with a
BeanFactory will be applied in the order of registration; any ordering
semantics expressed through implementing the
PriorityOrdered or Ordered interface will be ignored for
programmatically registered post-processors. Furthermore, the
@Order annotation is not
taken into account for BeanPostProcessor beans.
- Since:
- 10.10.2003
- Author:
- Juergen Hoeller, Sam Brannen
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionpostProcessAfterInitialization(Object bean, String beanName) Apply thisBeanPostProcessorto the given new bean instance after any bean initialization callbacks (like InitializingBean'safterPropertiesSetor a custom init-method).postProcessBeforeInitialization(Object bean, String beanName) Apply thisBeanPostProcessorto the given new bean instance before any bean initialization callbacks (like InitializingBean'safterPropertiesSetor a custom init-method).
-
Method Details
-
postProcessBeforeInitialization
default @Nullable Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException Apply thisBeanPostProcessorto the given new bean instance before any bean initialization callbacks (like InitializingBean'safterPropertiesSetor a custom init-method). The bean will already be populated with property values. The returned bean instance may be a wrapper around the original.The default implementation returns the given
beanas-is.- Parameters:
bean- the new bean instancebeanName- the name of the bean- Returns:
- the bean instance to use, either the original or a wrapped one;
if
null, no subsequent BeanPostProcessors will be invoked - Throws:
BeansException- in case of errors- See Also:
-
postProcessAfterInitialization
default @Nullable Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException Apply thisBeanPostProcessorto the given new bean instance after any bean initialization callbacks (like InitializingBean'safterPropertiesSetor a custom init-method). The bean will already be populated with property values. The returned bean instance may be a wrapper around the original.In case of a FactoryBean, this callback will be invoked for both the FactoryBean instance and the objects created by the FactoryBean. The post-processor can decide whether to apply to either the FactoryBean or created objects or both through corresponding
bean instanceof FactoryBeanchecks.This callback will also be invoked after a short-circuiting triggered by a
InstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation(Class, String)method, in contrast to all otherBeanPostProcessorcallbacks.The default implementation returns the given
beanas-is.- Parameters:
bean- the new bean instancebeanName- the name of the bean- Returns:
- the bean instance to use, either the original or a wrapped one;
if
null, no subsequent BeanPostProcessors will be invoked - Throws:
BeansException- in case of errors- See Also:
-