BeanFactoryPostProcessor
February 25, 2020 · View on GitHub
与 BeanPostProcessor 类似
When load BeanFactoryPostProcessor
BeanFactoryPostProcessor的作用,在AbstractApplicationContext.refresh中有下面的注释
/**
* Allows for custom modification of an application context's bean definitions,
* adapting the bean property values of the context's underlying bean factory.
*
* <p>Application contexts can auto-detect BeanFactoryPostProcessor beans in
* their bean definitions and apply them before any other beans get created.
*
* <p>Useful for custom config files targeted at system administrators that
* override bean properties configured in the application context.
*
* <p>See PropertyResourceConfigurer and its concrete implementations
* for out-of-the-box solutions that address such configuration needs.
*
* <p>A BeanFactoryPostProcessor may interact with and modify bean
* definitions, but never bean instances. Doing so may cause premature bean
* instantiation, violating the container and causing unintended side-effects.
* If bean instance interaction is required, consider implementing
* {@link BeanPostProcessor} instead.
*/
// AbstractApplicationContext
@Override
public void refresh() throws BeansException, IllegalStateException {
// ...
// Invoke factory processors registered as beans in the context.
invokeBeanFactoryPostProcessors(beanFactory)
// ...
}
The hook method postProcessBeanFactory
@FunctionalInterface
public interface BeanFactoryPostProcessor {
/**
* 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;
}
The load method PostProcessorRegistrationDelegate
PostProcessorRegistrationDelegate是一个工具类
PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors());
初始化实现了
BeanFactoryPostProcessor和BeanDefinitionRegistry接口的类
PostProcessorRegistrationDelegate-registerBeanPostProcessors
public static void registerBeanPostProcessors(
ConfigurableListableBeanFactory beanFactory, AbstractApplicationContext applicationContext) {
}
Demo for BeanFactoryPostProcessor
PropertyResourceConfigurer
PropertyResourceConfigurer Spring 实现 ${} 占位符注入的 BeanFactoryPostProcessor 一种实现
ConfigurationClassPostProcessor
ConfigurationClassPostProcessor 主要负责 @Configuration 注解的解析
// ConfigurationClassPostProcessor
public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPostProcessor,
PriorityOrdered, ResourceLoaderAware, BeanClassLoaderAware, EnvironmentAware {
}
// BeanFactoryPostProcessor
public interface BeanDefinitionRegistryPostProcessor extends BeanFactoryPostProcessor {
//
}