博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
BeanDefinition的Resource定位——2
阅读量:4616 次
发布时间:2019-06-09

本文共 5868 字,大约阅读时间需要 19 分钟。

1.FileSystemXmlApplicationContext的实现

 

1 public class FileSystemXmlApplicationContext extends AbstractXmlApplicationContext {  2   3     /**  4      * Create a new FileSystemXmlApplicationContext for bean-style configuration.  5      * @see #setConfigLocation  6      * @see #setConfigLocations  7      * @see #afterPropertiesSet()  8      */  9     public FileSystemXmlApplicationContext() { 10     } 11  12     /** 13      * Create a new FileSystemXmlApplicationContext for bean-style configuration. 14      * @param parent the parent context 15      * @see #setConfigLocation 16      * @see #setConfigLocations 17      * @see #afterPropertiesSet() 18      */ 19     public FileSystemXmlApplicationContext(ApplicationContext parent) { 20         super(parent); 21     } 22  23     /** 24      * 这个构造函数的configLocation包含的是BeanDefinition所在的文件路径 25      * Create a new FileSystemXmlApplicationContext, loading the definitions 26      * from the given XML file and automatically refreshing the context. 27      * @param configLocation file path 28      * @throws BeansException if context creation failed 29      */ 30     public FileSystemXmlApplicationContext(String configLocation) throws BeansException { 31         this(new String[] {configLocation}, true, null); 32     } 33  34     /** 35      * 这个构造函数允许configLocation包含多个BeanDefinition的文件路径 36      * Create a new FileSystemXmlApplicationContext, loading the definitions 37      * from the given XML files and automatically refreshing the context. 38      * @param configLocations array of file paths 39      * @throws BeansException if context creation failed 40      */ 41     public FileSystemXmlApplicationContext(String... configLocations) throws BeansException { 42         this(configLocations, true, null); 43     } 44  45     /** 46      * 这个构造函数在允许configLocation包含多个BeanDefinition的文件路径的同时,还允许指定 47      * 自己的双亲IoC容器 48      * Create a new FileSystemXmlApplicationContext with the given parent, 49      * loading the definitions from the given XML files and automatically 50      * refreshing the context. 51      * @param configLocations array of file paths 52      * @param parent the parent context 53      * @throws BeansException if context creation failed 54      */ 55     public FileSystemXmlApplicationContext(String[] configLocations, ApplicationContext parent) throws BeansException { 56         this(configLocations, true, parent); 57     } 58  59     /** 60      * Create a new FileSystemXmlApplicationContext, loading the definitions 61      * from the given XML files. 62      * @param configLocations array of file paths 63      * @param refresh whether to automatically refresh the context, 64      * loading all bean definitions and creating all singletons. 65      * Alternatively, call refresh manually after further configuring the context. 66      * @throws BeansException if context creation failed 67      * @see #refresh() 68      */ 69     public FileSystemXmlApplicationContext(String[] configLocations, boolean refresh) throws BeansException { 70         this(configLocations, refresh, null); 71     } 72  73     /** 74      * 在对象的初始化过程中,调用refresh函数载入BeanDefinition,这个refresh启动了 75      * BeanDefinition的载入过程,我们会在下面进行详细分析 76      * Create a new FileSystemXmlApplicationContext with the given parent, 77      * loading the definitions from the given XML files. 78      * @param configLocations array of file paths 79      * @param refresh whether to automatically refresh the context, 80      * loading all bean definitions and creating all singletons. 81      * Alternatively, call refresh manually after further configuring the context. 82      * @param parent the parent context 83      * @throws BeansException if context creation failed 84      * @see #refresh() 85      */ 86     public FileSystemXmlApplicationContext(String[] configLocations, boolean refresh, ApplicationContext parent) 87             throws BeansException { 88  89         super(parent); 90         setConfigLocations(configLocations); 91         if (refresh) { 92             refresh(); 93         } 94     } 95  96  97     /** 98      * 这是应用于文件系统中Resource的实现,通过构造一个FileSystemResource来得到一个在文件 99      * 系统中定位的BeanDefinition100      * 这个getResourceByPath是在BeanDefinitionReader的loadBeanDefinition中被调用的101      * loadBeanDfinition采用了模板模式,具体的定位实现实际上是由各个子类来完成的102      *103      *104      * Resolve resource paths as file system paths.105      * 

Note: Even if a given path starts with a slash, it will get106 * interpreted as relative to the current VM working directory.107 * This is consistent with the semantics in a Servlet container.108 * @param path path to the resource109 * @return Resource handle110 * @see org.springframework.web.context.support.XmlWebApplicationContext#getResourceByPath111 */112 @Override113 protected Resource getResourceByPath(String path) {114 if (path != null && path.startsWith("/")) {115 path = path.substring(1);116 }117 return new FileSystemResource(path);118 }119 120 }

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所示。

图1 getResourceByPath的调用关系

 

 图2 getResourceByPath的调用过程

转载于:https://www.cnblogs.com/duffy/p/3978750.html

你可能感兴趣的文章
Finding LCM LightOJ - 1215 (水题)
查看>>
python生成器
查看>>
PowerDesigner Constraint name uniqueness 错误
查看>>
系统子系统_GPRS子系统流程图
查看>>
Ubuntu 下搭建SVN服务器
查看>>
css3转换
查看>>
将字符串中不同字符的个数打印出来
查看>>
java第三次上机
查看>>
android Javah生成JNI头文件
查看>>
npm创建react项目
查看>>
关于u32中查找和定位最后到bit Number of 1 Bits
查看>>
sql数据库查询
查看>>
云计算技能图谱
查看>>
委托、Lambda表达式和事件
查看>>
typecho模板制作代码收集
查看>>
Python学习笔记4:集合方法
查看>>
elasticsearch的监控脚本
查看>>
你还在为使用P/Invoke时,写不出win32 api对应的C#声明而犯愁吗?
查看>>
msbuild property metadata会overwrite msbuild task中的properties
查看>>
python系列前期笔记
查看>>