public interface InstantiationAwareBeanPostProcessor extends BeanPostProcessor
BeanPostProcessor
that adds a before-instantiation callback,
and a callback after instantiation but before explicit properties are set or
autowiring occurs.
Typically used to suppress default instantiation for specific target beans, for example to create proxies with special TargetSources (pooling targets, lazily initializing targets, etc), or to implement additional injection strategies such as field injection.
NOTE: This interface is a special purpose interface, mainly for
internal use within the framework. It is recommended to implement the plain
BeanPostProcessor
interface as far as possible, or to derive from
InstantiationAwareBeanPostProcessorAdapter
in order to be shielded
from extensions to this interface.
AbstractAutoProxyCreator.setCustomTargetSourceCreators(org.springframework.aop.framework.autoproxy.TargetSourceCreator...)
,
LazyInitTargetSourceCreator
Modifier and Type | Method and Description |
---|---|
default boolean |
postProcessAfterInstantiation(java.lang.Object bean,
java.lang.String beanName)
Perform operations after the bean has been instantiated, via a constructor or factory method,
but before Spring property population (from explicit properties or autowiring) occurs.
|
default java.lang.Object |
postProcessBeforeInstantiation(java.lang.Class<?> beanClass,
java.lang.String beanName)
Apply this BeanPostProcessor before the target bean gets instantiated.
|
default PropertyValues |
postProcessPropertyValues(PropertyValues pvs,
java.beans.PropertyDescriptor[] pds,
java.lang.Object bean,
java.lang.String beanName)
Post-process the given property values before the factory applies them
to the given bean.
|
postProcessAfterInitialization, postProcessBeforeInitialization
@Nullable default java.lang.Object postProcessBeforeInstantiation(java.lang.Class<?> beanClass, java.lang.String beanName) throws BeansException
If a non-null object is returned by this method, the bean creation process
will be short-circuited. The only further processing applied is the
BeanPostProcessor.postProcessAfterInitialization(java.lang.Object, java.lang.String)
callback from the configured
BeanPostProcessors
.
This callback will only be applied to bean definitions with a bean class. In particular, it will not be applied to beans with a "factory-method".
Post-processors may implement the extended
SmartInstantiationAwareBeanPostProcessor
interface in order
to predict the type of the bean object that they are going to return here.
The default implementation returns null
.
beanClass
- the class of the bean to be instantiatedbeanName
- the name of the beannull
to proceed with default instantiationBeansException
- in case of errorsAbstractBeanDefinition.hasBeanClass()
,
AbstractBeanDefinition.getFactoryMethodName()
default boolean postProcessAfterInstantiation(java.lang.Object bean, java.lang.String beanName) throws BeansException
This is the ideal callback for performing custom field injection on the given bean instance, right before Spring's autowiring kicks in.
The default implementation returns true
.
bean
- the bean instance created, with properties not having been set yetbeanName
- the name of the beantrue
if properties should be set on the bean; false
if property population should be skipped. Normal implementations should return true
.
Returning false
will also prevent any subsequent InstantiationAwareBeanPostProcessor
instances being invoked on this bean instance.BeansException
- in case of errors@Nullable default PropertyValues postProcessPropertyValues(PropertyValues pvs, java.beans.PropertyDescriptor[] pds, java.lang.Object bean, java.lang.String beanName) throws BeansException
Also allows for replacing the property values to apply, typically through creating a new MutablePropertyValues instance based on the original PropertyValues, adding or removing specific values.
The default implementation returns the given pvs
as-is.
pvs
- the property values that the factory is about to apply (never null
)pds
- the relevant property descriptors for the target bean (with ignored
dependency types - which the factory handles specifically - already filtered out)bean
- the bean instance created, but whose properties have not yet been setbeanName
- the name of the beannull
to skip property populationBeansException
- in case of errorsMutablePropertyValues