spring 源码分析笔记
spring 源码分析笔记
1.入口
ClassPathXmlApplicationContext#ClassPathXmlApplicationContext
AbstractApplicationContext#refresh()
public void refresh() throws BeansException, IllegalStateException {
synchronized(this.startupShutdownMonitor) {
// 初始化 一下 配置 环境信息
this.prepareRefresh();
//工厂初始化
ConfigurableListableBeanFactory beanFactory = this.obtainFreshBeanFactory();
this.prepareBeanFactory(beanFactory);
try {
this.postProcessBeanFactory(beanFactory);
this.invokeBeanFactoryPostProcessors(beanFactory);
this.registerBeanPostProcessors(beanFactory);
this.initMessageSource();
this.initApplicationEventMulticaster();
// springmvc 调用 并且 设置了 applicationContext
this.onRefresh();
this.registerListeners();
this.finishBeanFactoryInitialization(beanFactory);
this.finishRefresh();
} catch (BeansException var9) {
if (this.logger.isWarnEnabled()) {
this.logger.warn("Exception encountered during context initialization - cancelling refresh attempt: " + var9);
}
this.destroyBeans();
this.cancelRefresh(var9);
throw var9;
} finally {
this.resetCommonCaches();
}
}
}
2.初始化工厂 涉及到 解析xml 注册 BeanDefinition
1. AbstractApplicationContext#obtainFreshBeanFactory()
2. this.refreshBeanFactory();
3. AbstractRefreshableApplicationContext#refreshBeanFactory()
// 载入所有 BeanDefinition
4. this.loadBeanDefinitions(beanFactory);
5. AbstractXmlApplicationContext#loadBeanDefinitions
6. this.loadBeanDefinitions
7. reader.loadBeanDefinitions(configResources);
8. this.loadBeanDefinitions
9. XmlBeanDefinitionReader#loadBeanDefinitions
10. this.doLoadBeanDefinitions
// 注册所有 的bean 定义 信息
11. this.registerBeanDefinitions(doc, resource);
12. documentReader.registerBeanDefinitions(doc, this.createReaderContext(resource));
13. this.doRegisterBeanDefinitions(doc.getDocumentElement());
// 委派 给 解析器 解析 XML 解析成 bean 定义 信息
14. this.parseBeanDefinitions(root, this.delegate);
15. DefaultBeanDefinitionDocumentReader#parseBeanDefinitions
16. this.parseDefaultElement(ele, delegate);
17.
private void parseDefaultElement(Element ele, BeanDefinitionParserDelegate delegate) {
if (delegate.nodeNameEquals(ele, "import")) {
this.importBeanDefinitionResource(ele);
} else if (delegate.nodeNameEquals(ele, "alias")) {
this.processAliasRegistration(ele);
} else if (delegate.nodeNameEquals(ele, "bean")) {
// 解析 开始
this.processBeanDefinition(ele, delegate);
} else if (delegate.nodeNameEquals(ele, "beans")) {
this.doRegisterBeanDefinitions(ele);
}
}
18. BeanDefinitionReaderUtils.registerBeanDefinition(bdHolder, this.getReaderContext().getRegistry());
// registry 就是 DefaultListableBeanFactory 接口
19. registry.registerBeanDefinition(beanName, definitionHolder.getBeanDefinition());
20. DefaultListableBeanFactory#registerBeanDefinition(String beanName, BeanDefinition beanDefinition)
//完成了 bean 定义的解析 过程 放入map
21. private final Map<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap(256);
elk