BeanDefinition的Resource定位——2

时间:2021-06-21 01:00:36

1.FileSystemXmlApplicationContext的实现

 public class FileSystemXmlApplicationContext extends AbstractXmlApplicationContext {

     /**
* Create a new FileSystemXmlApplicationContext for bean-style configuration.
* @see #setConfigLocation
* @see #setConfigLocations
* @see #afterPropertiesSet()
*/
public FileSystemXmlApplicationContext() {
} /**
* Create a new FileSystemXmlApplicationContext for bean-style configuration.
* @param parent the parent context
* @see #setConfigLocation
* @see #setConfigLocations
* @see #afterPropertiesSet()
*/
public FileSystemXmlApplicationContext(ApplicationContext parent) {
super(parent);
} /**
* 这个构造函数的configLocation包含的是BeanDefinition所在的文件路径
* Create a new FileSystemXmlApplicationContext, loading the definitions
* from the given XML file and automatically refreshing the context.
* @param configLocation file path
* @throws BeansException if context creation failed
*/
public FileSystemXmlApplicationContext(String configLocation) throws BeansException {
this(new String[] {configLocation}, true, null);
} /**
* 这个构造函数允许configLocation包含多个BeanDefinition的文件路径
* Create a new FileSystemXmlApplicationContext, loading the definitions
* from the given XML files and automatically refreshing the context.
* @param configLocations array of file paths
* @throws BeansException if context creation failed
*/
public FileSystemXmlApplicationContext(String... configLocations) throws BeansException {
this(configLocations, true, null);
} /**
* 这个构造函数在允许configLocation包含多个BeanDefinition的文件路径的同时,还允许指定
* 自己的双亲IoC容器
* Create a new FileSystemXmlApplicationContext with the given parent,
* loading the definitions from the given XML files and automatically
* refreshing the context.
* @param configLocations array of file paths
* @param parent the parent context
* @throws BeansException if context creation failed
*/
public FileSystemXmlApplicationContext(String[] configLocations, ApplicationContext parent) throws BeansException {
this(configLocations, true, parent);
} /**
* Create a new FileSystemXmlApplicationContext, loading the definitions
* from the given XML files.
* @param configLocations array of file paths
* @param refresh whether to automatically refresh the context,
* loading all bean definitions and creating all singletons.
* Alternatively, call refresh manually after further configuring the context.
* @throws BeansException if context creation failed
* @see #refresh()
*/
public FileSystemXmlApplicationContext(String[] configLocations, boolean refresh) throws BeansException {
this(configLocations, refresh, null);
} /**
* 在对象的初始化过程中,调用refresh函数载入BeanDefinition,这个refresh启动了
* BeanDefinition的载入过程,我们会在下面进行详细分析
* Create a new FileSystemXmlApplicationContext with the given parent,
* loading the definitions from the given XML files.
* @param configLocations array of file paths
* @param refresh whether to automatically refresh the context,
* loading all bean definitions and creating all singletons.
* Alternatively, call refresh manually after further configuring the context.
* @param parent the parent context
* @throws BeansException if context creation failed
* @see #refresh()
*/
public FileSystemXmlApplicationContext(String[] configLocations, boolean refresh, ApplicationContext parent)
throws BeansException { super(parent);
setConfigLocations(configLocations);
if (refresh) {
refresh();
}
} /**
* 这是应用于文件系统中Resource的实现,通过构造一个FileSystemResource来得到一个在文件
* 系统中定位的BeanDefinition
* 这个getResourceByPath是在BeanDefinitionReader的loadBeanDefinition中被调用的
* loadBeanDfinition采用了模板模式,具体的定位实现实际上是由各个子类来完成的
*
*
* Resolve resource paths as file system paths.
* <p>Note: Even if a given path starts with a slash, it will get
* interpreted as relative to the current VM working directory.
* This is consistent with the semantics in a Servlet container.
* @param path path to the resource
* @return Resource handle
* @see org.springframework.web.context.support.XmlWebApplicationContext#getResourceByPath
*/
@Override
protected Resource getResourceByPath(String path) {
if (path != null && path.startsWith("/")) {
path = path.substring(1);
}
return new FileSystemResource(path);
} }

FileSystemXmlApplicationContext的实现

2.在FileSystemXmlApplicationContext中,我们可以看到在构造函数中,实现了对configuration进行处理的功能,让所有配置在文件系统中的,以XML文件方式存在的BeanDefinition都能够得到有效的处理,比如,实现了getResourceByPath方法,这个方法是一个模板方法,是为读取Resource服务的。

3.对于IoC容器功能的实现,这里没有涉及,因为它继承了AbstractXmlApplicationContext,关于IoC容器功能相关的实现,都是在FileSystemXmlApplicationContext中完成的,但是在构造函数中通过refresh来启动IoC容器的初始化,这个refresh方法非常重要,也是我们以后分析容器初始化过程实现的一个重要入口

4.注意:FileSystemXmlApplicationContext是一个支持XML定义BeanDefinitionApplicationContext,并且可以指定以文件形式的BeanDefinition的读入这些文件可以使用文件路径URL定义来表示。在测试环境独立应用环境中,这个ApplicationContext是非常有用的。

5.根据图1的调用关系分析,我们可以清楚地看到整个BeanDefinition资源定位的过程。这个对BeanDefinition资源定位的过程,最初是由refresh来触发的,这个refresh的调用是在FileSystemXmlBeanFactory构造函数中启动的,大致的调用过程如图2所示。

BeanDefinition的Resource定位——2

图1 getResourceByPath的调用关系

BeanDefinition的Resource定位——2

图2 getResourceByPath的调用过程