Spring源码-AbstractXmlApplicationContext-源码解读


/**
 * Convenient base class for {@link org.springframework.context.ApplicationContext}			实用 ApplicationContext 实现
 * implementations, drawing configuration from XML documents containing bean definitions	凭借 XmlBeanDefinitionReader 从包含 bean definitions 的 xml 文档中拉取配置
 * understood by an {@link org.springframework.beans.factory.xml.XmlBeanDefinitionReader}.
 *
 * <p>Subclasses just have to implement the {@link #getConfigResources} and/or				子类仅需要实现 getConfigResources 或 getConfigLocations 方法
 * the {@link #getConfigLocations} method. Furthermore, they might override					此外他们可能重写 getResourceByPath ,以特定环境的方式去解析相对路径
 * the {@link #getResourceByPath} hook to interpret relative paths in an
 * environment-specific fashion, and/or {@link #getResourcePatternResolver}					或者 getResourcePatternResolver 用于扩展解析方式
 * for extended pattern resolution.
 *
 * @author Rod Johnson
 * @author Juergen Hoeller
 * @see #getConfigResources
 * @see #getConfigLocations
 * @see org.springframework.beans.factory.xml.XmlBeanDefinitionReader
 */
public abstract class AbstractXmlApplicationContext extends AbstractRefreshableConfigApplicationContext {

	private boolean validating = true;


	/**
	 * Create a new AbstractXmlApplicationContext with no parent.
	 */
	public AbstractXmlApplicationContext() {
	}

	/**
	 * Create a new AbstractXmlApplicationContext with the given parent context.
	 * @param parent the parent context
	 */
	public AbstractXmlApplicationContext(@Nullable ApplicationContext parent) {
		super(parent);
	}


	/**
	 * Set whether to use XML validation. Default is {@code true}. 设置是否验证 xml ,默认是 true
	 */
	public void setValidating(boolean validating) {
		this.validating = validating;
	}


	/**
	 * Loads the bean definitions via an XmlBeanDefinitionReader.			经过一个 XmlBeanDefinitionReader 加载 bean definitions
	 * @see org.springframework.beans.factory.xml.XmlBeanDefinitionReader
	 * @see #initBeanDefinitionReader
	 * @see #loadBeanDefinitions
	 */
	@Override
	protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException {
		// Create a new XmlBeanDefinitionReader for the given BeanFactory.
		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);
		loadBeanDefinitions(beanDefinitionReader);
	}

	/**
	 * Initialize the bean definition reader used for loading the bean			初始化这个 context 用于加载 bean definitions 的 bean definition reader
	 * definitions of this context. Default implementation is empty.			默认实现为空
	 * <p>Can be overridden in subclasses, e.g. for turning off XML validation	能够被子类重写,例如用于关闭 xml 验证或者使用一个不同的 XmlBeanDefinitionParser implementation. 实现
	 * or using a different XmlBeanDefinitionParser implementation.
	 * @param reader the bean definition reader used by this context
	 * @see org.springframework.beans.factory.xml.XmlBeanDefinitionReader#setDocumentReaderClass
	 */
	protected void initBeanDefinitionReader(XmlBeanDefinitionReader reader) {
		reader.setValidating(this.validating);
	}

	/**
	 * Load the bean definitions with the given XmlBeanDefinitionReader.					使用给定的 XmlBeanDefinitionReader 加载 bean definitions
	 * <p>The lifecycle of the bean factory is handled by the {@link #refreshBeanFactory}	凭借着 refreshBeanFactory 方法处理 bean factory 的生命周期
	 * method; hence this method is just supposed to load and/or register bean definitions.	因此这个方法仅支持加载以及注册 register bean definitions
	 * @param reader the XmlBeanDefinitionReader to use
	 * @throws BeansException in case of bean registration errors
	 * @throws IOException if the required XML document isn't found
	 * @see #refreshBeanFactory
	 * @see #getConfigLocations
	 * @see #getResources
	 * @see #getResourcePatternResolver
	 */
	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);
		}
	}

	/**
	 * Return an array of Resource objects, referring to the XML bean definition	返回一个资源对象数组,引用构建这个 context 时的 XML bean definition 文件
	 * files that this context should be built with.
	 * <p>The default implementation returns {@code null}. Subclasses can override	默认的实现返回 null,子类能够重写它,提供预构建的资源对象,
	 * this to provide pre-built Resource objects rather than location Strings.		而不是位置字符串
	 * @return an array of Resource objects, or {@code null} if none
	 * @see #getConfigLocations()
	 */
	@Nullable
	protected Resource[] getConfigResources() {
		return null;
	}

}

主要方法

方法名介绍
Loads the bean definitions via an XmlBeanDefinitionReader.经过一个 XmlBeanDefinitionReader 加载 bean definitions
protected void initBeanDefinitionReader(XmlBeanDefinitionReader reader) {}初始化这个 context 用于加载 bean definitions 的 bean definition reader
protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws BeansException, IOException {}使用给定的 XmlBeanDefinitionReader 加载 bean definitions
protected Resource[] getConfigResources() {}返回一个资源对象数组,引用构建这个 context 时的 XML bean definition 文件
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值