Spring 之 BeanPostProcessor
一、相关概念
BeanPostProcessor
- Bean 的后置处理器,用于Bean在初始化前后对Bean Definetion进行动态操作。
public interface BeanPostProcessor {
//该方法在Bean初始化操作之前调用
@Nullable
default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
//该方法在Bean初始化完成后调用
@Nullable
default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
}
简单来说,Spring容器内开始是以BeanDefinition
形式存储POJO
对象,第一次调用getBean()
获取Bean
对象时,会根据BeanDefinition
内的信息对Bean
进行初始化操作,若此时容器中存在BeanPostProcessor
类型的Bean,则会在Bean初始化前调用所有BeanPostProcessor
对象的postProcessBeforeInitialization
方法,在Bean初始化完成后,调用所有BeanPostProcessor
对象的postProcessAfterInitialization
方法。
设置Bean的初始化方式有三种:
- 先执行
@PostConstruct
- 再执行
InitializingBean#afterPropertiesSet()
- 最后执行 `@Bean(initMethod =“”)
ps:这里有几个问题,初始化顺序为什么是这样?这个与BeanPostProcessor
有关联吗?
二、源码探究
上面我们谈到了BeanPostProcessor
内的方法是在Bean初始化前后调用的,以及简单介绍了Bean的三种初始化方式以及调用顺序,那么接下来我们就去看一下Bean初始化的相关源码。
众所周知,Spring容器的启动过程是从 AbstractApplicationContext
类中的refresh()
方法开始的(纳尼,你还不知道,那你现在立刻记住)。refresh()
方法中调用了finishBeanFactoryInitialization(beanFactory);
该方法就是用来初始化容器中剩余的还未被初始化的Bean
。我们就从这个方法开始看起
protected void finishBeanFactoryInitialization(ConfigurableListableBeanFactory beanFactory) {
if (beanFactory.containsBean(CONVERSION_SERVICE_BEAN_NAME) &&
beanFactory.isTypeMatch(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class)) {
beanFactory.setConversionService(
beanFactory.getBean(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class));
}
if (!beanFactory.hasEmbeddedValueResolver()) {
beanFactory.addEmbeddedValueResolver(strVal -> getEnvironment().resolvePlaceholders(strVal));
}
//获取 LoadTimeWeaverAware 类型的所有BeanName,此时获取的都是为初始化的Bean的名称
String[] weaverAwareNames = beanFactory.getBeanNamesForType(LoadTimeWeaverAware.class, false, false);
for (String weaverAwareName : weaverAwareNames) {
//该方法就是获取Bean,若是第一次获取,则对Bean进行初始化
getBean(weaverAwareName);
}
beanFactory.setTempClassLoader(null);
beanFactory.freezeConfiguration();
//实例化所有剩余(非惰性初始化)单例。该方法是接口定义的方法,最终由子类 DefaultListableBeanFactory 实现
beanFactory.preInstantiateSingletons();
}
// DefaultListableBeanFactory 类,预实例化单例
@Override
public void preInstantiateSingletons() throws BeansException {
// 获取所有尚未初始的beanNames,
List<String> beanNames = new ArrayList<>(this.beanDefinitionNames);
//触发所有非惰性单例 bean 的初始化...
for (String beanName : beanNames) {
RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName);
//这里判断Bean是不抽象的、单例的、非惰性加载的
if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) {
//判断是不是factoryBean
if (isFactoryBean(beanName)) {
Object bean = getBean(FACTORY_BEAN_PREFIX + beanName);
if (bean instanceof FactoryBean) {
FactoryBean<?> factory = (FactoryBean<?>) bean;
boolean isEagerInit;
if (System.getSecurityManager() != null && factory instanceof SmartFactoryBean) {
isEagerInit = AccessController.doPrivileged(
(PrivilegedAction<Boolean>) ((SmartFactoryBean<?>) factory)::isEagerInit,
getAccessControlContext());
}
else {
isEagerInit = (factory instanceof SmartFactoryBean &&
((SmartFactoryBean<?>) factory).isEagerInit());
}
if (isEagerInit) {
getBean(beanName);
}
}
}
else {
//这里进行初始化
getBean(beanName);
}
}
}
// 触发所有适用 bean 的初始化后回调...
for (String beanName : beanNames) {
Object singletonInstance = getSingleton(beanName);
if (singletonInstance instanceof SmartInitializingSingleton) {
StartupStep smartInitialize = this.getApplicationStartup().start("spring.beans.smart-initialize")
.tag("beanName", beanName);
SmartInitializingSingleton smartSingleton = (SmartInitializingSingleton) singletonInstance;
if (System.getSecurityManager() != null) {
AccessController.doPrivileged((PrivilegedAction