public interface BeanDefinitionRegistryPostProcessor extends BeanFactoryPostProcessor {
/**
* 此时所有常规 bean 定义(BeanDefinition)都已被加载,但尚未实例化任何 bean。
* 这允许在下一个后处理阶段开始之前添加更多的 bean 定义。
* Modify the application context's internal bean definition registry after its
* standard initialization. All regular bean definitions will have been loaded,
* but no beans will have been instantiated yet. This allows for adding further
* bean definitions before the next post-processing phase kicks in.
* @param registry the bean definition registry used by the application context
* @throws org.springframework.beans.BeansException in case of errors
*/
void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException;
}
BeanDefinitionRegistryPostProcessor继承自BeanFactoryPostProcessor;
其执行时机在:
org.springframework.context.support.AbstractApplicationContext#refresh --调用–> org.springframework.context.support.AbstractApplicationContext#invokeBeanFactoryPostProcessors --调用–>
org.springframework.context.support.PostProcessorRegistrationDelegate#invokeBeanFactoryPostProcessors(org.springframework.beans.factory.config.ConfigurableListableBeanFactory, java.util.List
执行时,按实现的优先级接口(PriorityOrdered、Ordered、不实现)顺序执行对应的postProcessBeanDefinitionRegistry方法;
待所有BeanDefinitionRegistryPostProcessor的postProcessBeanDefinitionRegistry方法执行完成后,再统一执行org.springframework.beans.factory.config.BeanFactoryPostProcessor#postProcessBeanFactory
org.springframework.context.annotation.ConfigurationClassPostProcessor(配置类的解析后置处理器)就是实现的BeanDefinitionRegistryPostProcessor接口
@FunctionalInterface
public interface BeanFactoryPostProcessor {
/**
* 所有 bean 定义都已被加载,但还没有 bean 被实例化。这允许覆盖或添加属性,甚至是提前初始化的 bean。
* Modify the application context's internal bean factory after its standard
* initialization. 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.
* @param beanFactory the bean factory used by the application context
* @throws org.springframework.beans.BeansException in case of errors
*/
void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException;
}
其执行时机同BeanDefinitionRegistryPostProcessor;同样按实现的优先级接口(PriorityOrdered、Ordered、不实现)顺序执行
BeanPostProcessor注册时机:
org.springframework.context.support.AbstractApplicationContext#registerBeanPostProcessors --调用–>
org.springframework.context.support.PostProcessorRegistrationDelegate#registerBeanPostProcessors(org.springframework.beans.factory.config.ConfigurableListableBeanFactory, org.springframework.context.support.AbstractApplicationContext)
public interface SmartInstantiationAwareBeanPostProcessor extends InstantiationAwareBeanPostProcessor {
/**
* 返回Bean的类型,一个“指鹿为马”的机会;org.springframework.beans.factory.support.DefaultListableBeanFactory#doGetBeanNamesForType就会用到该方法
* 预测postProcessBeforeInstantiation方法返回的对象类型
*/
@Nullable
default Class<?> predictBeanType(Class<?> beanClass, String beanName) throws BeansException {
return null;
}
/**
* 可以使用该方法的实现在创建bean时使用指定的构造方法
*/
@Nullable
default Constructor<?>[] determineCandidateConstructors(Class<?> beanClass, String beanName)
throws BeansException {
return null;
}
/**
* 获取对指定 bean 的早期访问的引用,通常用于解析循环引用
*/
default Object getEarlyBeanReference(Object bean, String beanName) throws BeansException {
return bean;
}
}
调用org.springframework.beans.factory.support.DefaultListableBeanFactory#doGetBeanNamesForType时会触发predictBeanType方法
public interface InstantiationAwareBeanPostProcessor extends BeanPostProcessor {
/**
* 在目标 bean 被实例化之前调用。返回的 bean 对象可能是要使用的代理而不是目标 bean,从而有效地抑制了目标 bean 的默认实例化。
* 如果这个方法返回了一个非空的对象,那么bean的创建过程就会短路。
*/
@Nullable
default Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
return null;
}
/**
* 在通过构造函数或工厂方法实例化 bean 之后,但在 Spring 属性填充(来自显式属性或自动装配)发生之前执行操作。在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#populateBean里
* 这是在给定 bean 实例上执行自定义字段注入的理想回调,就在 Spring 的自动装配开始之前。
* 默认实现返回true,返回false将直接退出populateBean方法,后续属性不再赋值
*/
default boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
return true;
}
/**
* 在工厂将给定的属性值应用到给定的 bean 之前对其进行后处理
* Spring的自动装配后置处理器就是在这里实现,其返回的PropertyValues会被org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#applyPropertyValues使用,赋值
*/
@Nullable
default PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName)
throws BeansException {
return null;
}
/**
* @deprecated as of 5.1, in favor of {@link #postProcessProperties(PropertyValues, Object, String)}
*/
@Deprecated
@Nullable
default PropertyValues postProcessPropertyValues(
PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException {
return pvs;
}
}
public interface MergedBeanDefinitionPostProcessor extends BeanPostProcessor {
/**
* 对指定 bean 的给定合并 bean 定义进行后处理
* 在实例化bean之前执行,在InstantiationAwareBeanPostProcessor#postProcessBeforeInstantiation之前执行
*/
void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName);
/**
* A notification that the bean definition for the specified name has been reset,
* and that this post-processor should clear any metadata for the affected bean.
* The default implementation is empty.
* @param beanName the name of the bean
* @since 5.1
* @see DefaultListableBeanFactory#resetBeanDefinition
*/
default void resetBeanDefinition(String beanName) {
}
}
public interface BeanPostProcessor {
/**
* 执行此方法时,bean已创建,但尚未初始化
* 实例:ApplicationContextAwareProcessor注入ApplicationEventPublisherAware、ApplicationContextAware、
* MessageSourceAware、ResourceLoaderAware、EnvironmentAware、EmbeddedValueResolverAware、ApplicationStartupAware
*/
@Nullable
default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
/**
* 初始化完成之后再对Bean属性修改,此时org.springframework.beans.factory.InitializingBean#afterPropertiesSet、自定义init方法已执行
*/
@Nullable
default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
}