Spring源码学习--Spring配置解析文件ApplicationContext.xml(一)

版权声明:欢迎转载 https://blog.csdn.net/qq924862077/article/details/72887652

接下来我们用多篇文件对Spring解析Bean配置文件ApplicationContext.xml的过程进行一系列的了解,了解Spring对我们在ApplicationContext.xml中注入的Bean的解析流程。

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相关的知识。



阅读更多

没有更多推荐了,返回首页