import org.springframework.beans.BeansException;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.stereotype.Component;
/**
* org.springframework.context.support.AbstractApplicationContext#invokeBeanFactoryPostProcessors
* 调用 BeanDefinitionRegistryPostProcessor 实现向容器内添加bean的定义
* 调用 BeanFactoryPostProcessor 实现向容器内bean的定义的添加属性
*/
@Component
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
BeanDefinition teacher = beanFactory.getBeanDefinition("teacher");
MutablePropertyValues propertyValues = teacher.getPropertyValues();
propertyValues.add("name","wangwu");
}
}
@Component
public class Teacher {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Teacher{" +
"name='" + name + '\'' +
'}';
}
}
org.springframework.context.support.AbstractApplicationContext#refresh
@Override
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
// Prepare this context for refreshing.
prepareRefresh();//调用容器准备刷新的方法,获取容器的当前时间,同时给容器设置同步标识 //容器状态设置,初始化属性设置,检查必备属性是否存在
// Tell the subclass to refresh the internal bean factory.//启动载入 //设置beanFactory序列化id, 获取beanFactory
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();//告诉子类启动refreshBeanFactory()方法, Bean定义资源文件的载入从子类的refreshBeanFactory() 方法启动
// Prepare the bean factory for use in this context.//设置beanFactory一些属性,添加后置处理器,设置忽略的自动装配接口,注册一些组件
prepareBeanFactory(beanFactory);//为 BeanFactory 配置容器特性,例如类加载器、事件处理器等
try {
// Allows post-processing of the bean factory in context subclasses.
postProcessBeanFactory(beanFactory);//为容器的某些子类指定特殊的 Post 事件处理器
// Invoke factory processors registered as beans in the context. //调用 BeanDefinitionRegistryPostProcessor 实现向容器内添加bean的定义,调用 BeanFactoryPostProcessor 实现向容器内bean的定义的添加属性
invokeBeanFactoryPostProcessors(beanFactory);//调用所有注册的 BeanFactoryPostProcessor 的 Bean,调用Bean Factory后处理器//springboot的处理就是在此扩展的
// Register bean processors that intercept bean creation.//找到 BeanPostProcessor 的实现,排序后注册进容器内
registerBeanPostProcessors(beanFactory);//为 BeanFactory 注册 Post 事件处理器//BeanPostProcessor是 Bean后置处理器, 用于监听容器触发的事件
// Initialize message source for this context.
initMessageSource();//初始化信息源,和国际化相关
// Initialize event multicaster for this context.
initApplicationEventMulticaster();//初始化容器事件传播器
// Initialize other special beans in specific context subclasses.
onRefresh();//调用子类的某些特殊 Bean 的初始化方法
// Check for listener beans and register them.
registerListeners();//为事件传播器注册事件监听器,派发早期事件
// Instantiate all remaining (non-lazy-init) singletons.
finishBeanFactoryInitialization(beanFactory);//初始化所有剩余的单例 Bean
// Last step: publish corresponding event.//调用生命周期处理器onRefresh方法,发布ContextRefreshedEvent事件,JMX相关处理
finishRefresh();//初始化容器的生命周期事件处理器,并发布容器的生命周期事件
}
catch (BeansException ex) {
if (logger.isWarnEnabled()) {
logger.warn("Exception encountered during context initialization - " +
"cancelling refresh attempt: " + ex);
}
// Destroy already created singletons to avoid dangling resources.
destroyBeans();//销毁己创建的 Bean
// Reset 'active' flag.
cancelRefresh(ex);//取消刷新操作 ,重置容器的同步标识
// Propagate exception to caller.
throw ex;
}
finally {
// Reset common introspection caches in Spring's core, since we
// might not ever need metadata for singleton beans anymore...
resetCommonCaches();//重设公共缓存
}
}
}