Spring源码学习--Spring配置解析文件ApplicationContext.xml(一)
2017-06-20 19:09
816 查看
接下来我们用多篇文件对Spring解析Bean配置文件ApplicationContext.xml的过程进行一系列的了解,了解Spring对我们在ApplicationContext.xml中注入的Bean的解析流程。
AbstractApplicationContext:中会初始化Bean容器
在obtainFreshBeanFactory中会初始化容器并返回初始化后的容器protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {
//初始化bean容器
refreshBeanFactory();
ConfigurableListableBeanFactory beanFactory = getBeanFactory();
if (logger.isDebugEnabled()) {
logger.debug("Bean factory for " + getDisplayName() + ": " + beanFactory);
}
return beanFactory;
}AbstractRefreshableApplicationContext类中:在refreshBeanFactory中会初始化容器,并加载配置文件loadBeanDefinitions
总结:简单来说在ApplicationContext中所做的操作是初始化了一个BeanFactory和XmlBeanDefinitionReader,其中XmlBeanDefinitionReader是用来解析Spring的xml配置文件的,接下来我们会用一篇博客介绍一下XmlBeanDefinitionReader解析Spring的配置文件xml相关的知识。
AbstractApplicationContext:中会初始化Bean容器
@Override public void refresh() throws BeansException, IllegalStateException { synchronized (this.startupShutdownMonitor) { ........... //获取bean容器 ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory(); ........... }
在obtainFreshBeanFactory中会初始化容器并返回初始化后的容器protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {
//初始化bean容器
refreshBeanFactory();
ConfigurableListableBeanFactory beanFactory = getBeanFactory();
if (logger.isDebugEnabled()) {
logger.debug("Bean factory for " + getDisplayName() + ": " + beanFactory);
}
return beanFactory;
}AbstractRefreshableApplicationContext类中:在refreshBeanFactory中会初始化容器,并加载配置文件loadBeanDefinitions
@Override protected final void refreshBeanFactory() throws BeansException { if (hasBeanFactory()) { destroyBeans(); closeBeanFactory(); } try { //创建Bean容器 DefaultListableBeanFactory beanFactory = createBeanFactory(); beanFactory.setSerializationId(getId()); customizeBeanFactory(beanFactory); //加载配置文件 loadBeanDefinitions(beanFactory); synchronized (this.beanFactoryMonitor) { this.beanFactory = beanFactory; } } catch (IOException ex) { throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex); } }接下来我们看一下loadBeanDefinitions所做的操作,其实现在AbstractXmlApplicationContext中,简单来说就是初始化了XmlBeanDefinitionReader,XmlBeanDefinitionReader是用来解析ApplicationContext.xml这个文件的。
@Override protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException { // Create a new XmlBeanDefinitionReader for the given BeanFactory. //创建ApplicationContext.xml文件的处理类XmlBeanDefinitionReader XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory); // Configure the bean definition reader with this context's // resource loading environment. beanDefinitionReader.setEnvironment(this.getEnvironment()); beanDefinitionReader.setResourceLoader(this); beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this)); // Allow a subclass to provide custom initialization of the reader, // then proceed with actually loading the bean definitions. initBeanDefinitionReader(beanDefinitionReader); //加载ApplicationContext.xml文件 loadBeanDefinitions(beanDefinitionReader); }接下来ApplicationContext.xml解析的工作是在XmlBeanDefinitionReader中
protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws BeansException, IOException { Resource[] configResources = getConfigResources();//获取配置文件路径 if (configResources != null) { reader.loadBeanDefinitions(configResources); } String[] configLocations = getConfigLocations(); if (configLocations != null) { reader.loadBeanDefinitions(configLocations); } }
总结:简单来说在ApplicationContext中所做的操作是初始化了一个BeanFactory和XmlBeanDefinitionReader,其中XmlBeanDefinitionReader是用来解析Spring的xml配置文件的,接下来我们会用一篇博客介绍一下XmlBeanDefinitionReader解析Spring的配置文件xml相关的知识。
相关文章推荐
- Spring的配置文件ApplicationContext.xml配置头文件解析
- 【Spring学习笔记-2】Myeclipse下第一个Spring程序-通过ClassPathXmlApplicationContext加载配置文件
- Spring基础学习教程(applicationContext.xml 配置文件 详解)-07
- spring源码解析之默认配置文件名/WEB-INF/applicationContext.xml
- spring学习笔记(3)——配置文件applicationContext.xml的加载
- spring源码解析之默认配置文件名/WEB-INF/applicationContext.xml
- Spring源码学习-容器初始化之FileSystemXmlApplicationContext(二)路径格式及解析方式(上) 推荐
- 在Spring的 applicationContext.xml文件中配置数据源
- Hibernate与Spring整合,配置文件applicationContext.xml
- SSH整合-关于Spring中applicationContext.xml文件对于事务的一些配置
- Spring添加事物支持 applicationContext.xml配置文件
- 读取applicationContext.xml(spring配置文件)文件
- Spring配置文件applicationContext.xml(1)基本配置
- Spring的applicationContext.xml中配置映射文件
- Spring的配置文件applicationContext.xml
- Hibernate SQL方言 (hibernate.dialect) Spring配置文件applicationContext.xml
- Spring的配置文件applicationContext.xml
- Spring配置文件applicationContext.xml的两种拆分方式
- spring+hibernate 配置文件(applicationContext.xml)实例
- applicationContext.xml import spring模块配置文件