44、[源码]-Spring容器创建-BeanFactory预准备
@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.
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
// Prepare the bean factory for use in this context.
prepareBeanFactory(beanFactory);
try {
// Allows post-processing of the bean factory in context subclasses.
postProcessBeanFactory(beanFactory);
// Invoke factory processors registered as beans in the context.
invokeBeanFactoryPostProcessors(beanFactory);
}
}
Spring容器的refresh()【创建刷新】;
1、prepareRefresh()刷新前的预处理;
- initPropertySources()初始化一些属性设置;子类自定义个性化的属性设置方法;
- getEnvironment().validateRequiredProperties();检验属性的合法等
- earlyApplicationEvents= new LinkedHashSet();保存容器中的一些早期的事件;
2、obtainFreshBeanFactory();获取BeanFactory;
- refreshBeanFactory();刷新【创建】BeanFactory;
创建了一个this.beanFactory = new DefaultListableBeanFactory();
设置id;
- getBeanFactory();返回刚才GenericApplicationContext创建的BeanFactory对象;
- 将创建的BeanFactory【DefaultListableBeanFactory】返回;
3、prepareBeanFactory(beanFactory);BeanFactory的预准备工作(BeanFactory进行一些设置);
- 设置BeanFactory的类加载器、支持表达式解析器...
- 添加部分BeanPostProcessor【ApplicationContextAwareProcessor】
- 设置忽略的自动装配的接口EnvironmentAware、EmbeddedValueResolverAware、xxx
- 注册可以解析的自动装配;我们能直接在任何组件中自动注入: BeanFactory、ResourceLoader、ApplicationEventPublisher、ApplicationContext
- 添加BeanPostProcessor【ApplicationListenerDetector】
- 添加编译时的AspectJ;
- 给BeanFactory中注册一些能用的组件;
- environment【ConfigurableEnvironment】、
- systemProperties【Map<String, Object>】、
- systemEnvironment【Map<String, Object>】