spring加载xml
加载文件顺序
情形一:使用classpath加载且不含通配符
这是最简单的情形,Spring默认会使用当前线程的ClassLoader的getResource方法获取资源的URL,如果无法获得当前线程的ClassLoader,Spring将使用加载类org.springframework.util.ClassUtils的ClassLoader。
1.当工程目录结构如图所示:
ApplicationContext context = new ClassPathXmlApplicationContext("conf/application-context.xml");
加载[conf/application-context.xml]
2.当工程目录结构如图所示:
即bin目录下只有.class文件,没有配置文件,依赖的conf.jar里有配置文件:
ApplicationContext context = new ClassPathXmlApplicationContext("conf/application-context.xml");
加载[conf/application-context.xml]
3. 当工程目录结构如图所示:
ApplicationContext context = new ClassPathXmlApplicationContext("conf/application-context.xml");
只会会加载bin/conf目录下的application-context.xml文件,不会加载jar包中的conf/application-context.xml。
情形二:使用classpath加载,包含通配符
Spring会通过使用路径中的非通配符部分先确定资源的大致位置,然后根据这个位置在确定具体的资源位置
1. 当工程目录结构如图所示:
ApplicationContext context = new ClassPathXmlApplicationContext("conf/**/*application-context.xml");
加载[admin-application-context.xml]
加载[application-context.xml]
2.当工程目录结构如图所示:
ApplicationContext context = new ClassPathXmlApplicationContext("conf/**/*application-context.xml");
加载conf/application-context.xml
加载conf/admin/admin-application-context.xml
3.当工程目录结构如图所示:
ApplicationContext context = new ClassPathXmlApplicationContext("conf/**/*application-context.xml");
bin/conf/application-context.xml文件和bin/conf/admin/admin-application-context.xml都会被加载,
但conf.jar文件中的配置文件并不会被加载。
情形三:使用classpath*前缀且不包含通配符
使用classpath*前缀可以获取所有与给定路径匹配的classpath资源,从而避免出现两个不同位置有相同名字的文件,Spring只加载其中一个的情况。
当工程目录结构如图所示:
这时使用
ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:conf/application-context.xml");
Spring将会加载bin目录下的application-context.xml文件和jar包里的application-context.xml文件。
情形四:使用classpath*前缀,包含通配符
当工程目录结构如图所示:
ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:conf/**/*application-context.xml");
conf目录下包括各级子目录中的所有配置文件,因此bin/conf/application-context.xml和 bin/conf/admin/admin-application-context.xml
以及jar包中的 conf/application-context.xml和 conf/admin/admin-application-context.xml都会被加载
Classpath*加载和Classpath加载区别
classpath*:的出现是为了从classpath和多个jar文件中加载相同的文件,classpath:只能加载找到的第一个文件。
classpath*载使用了classloader的getResources() 方法;
PathMatchingResourcePatternResolver类中,我们可以更清楚的了解其对的处理:如果是以classpath*开头,它会遍历classpath。
最终从文件加载的时候仍然是JAVA中常见的读取文件的方法:
如ClassPathResource得到inputstream的方法是利用class loader。
如ClassPathResource得到inputstream的方法是利用class loader。
public InputStream getInputStream() throws IOException {
InputStream is;
if (this.clazz != null) {
is = this.clazz.getResourceAsStream(this.path);
}
如FileSystemResource得到inputstream的方法是利用FileInputStream。
public InputStream getInputStream() throws IOException {
return new FileInputStream(this.file);
}
spring加载xml的更多相关文章
- spring加载xml的六种方式
因为目前正在从事一个项目,项目中一个需求就是所有的功能都是插件的形式装入系统,这就需要利用Spring去动态加载某一位置下的配置文件,所以就总结了下Spring中加载xml配置文件的方式,我总结的有6 ...
- Spring加载xml配置文件的方式 ApplicationContext
大家都知道Java读普通文件是通过Basic I/O 中的InputStream.OutStream.Reader.Writer 等实现的.在spring 框架中,它是怎样识别xml这个配置文件的呢? ...
- Spring加载XML配置文件
原创链接:http://www.cnblogs.com/yanqin/p/5282929.html(允许转载,但请注明原创链接) BeanFactory加载单个文件 当使用beanfactory去获取 ...
- Spring加载xml配置文件的方式(BeanFactory和ApplicationContext区别)
描述 大家都知道Java读普通文件是通过Basic I/O 中的InputStream.OutStream.Reader.Writer 等实现的.在spring 框架中,它是怎样识别xml这个配置文件 ...
- Spring加载XML机制
转载自跳刀的兔子 http://www.cnblogs.com/shipengzhi/articles/3029872.html 加载文件顺序 情形一:使用classpath加载且不含通配符 这是 ...
- Spring加载xml配置文件的方式
梳理Spring的流程 xml是最常见的spring 应用系统配置源.Spring中的几种容器都支持使用xml装配bean,包括: XmlBeanFactory,ClassPathXmlApplica ...
- (Spring加载xml时)org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'beans'.
ApplicationContext ctx = new ClassPathXmlApplicationContext("test.xml");报错 在启动Spring时,报以下错 ...
- Spring中加载xml配置文件的六种方式
Spring中加载xml配置文件的六种方式 博客分类: Spring&EJB XMLSpringWebBeanBlog 因为目前正在从事一个项目,项目中一个需求就是所有的功能都是插件的形式装 ...
- spring加载ApplicationContext.xml的四种方式
spring 中加载xml配置文件的方式,好像有4种, xml是最常见的spring 应用系统配置源.Spring中的几种容器都支持使用xml装配bean,包括: XmlBeanFactory , C ...
随机推荐
- mysql乱码以及Data too long for column全解(最完整实用版)
今天系统升级,开发.测试说本地环境.测试环境都没有问题,都用ssh client升的,演示环境报错了Data too long for column. 仔细检查了下,表字符集都是utf-8,目测长度肯 ...
- Vue自带的过滤器
gitHub地址:https://github.com/lily1010/vue_learn/tree/master/lesson05 一 过滤器写法 {{ message | Filter}} 二 ...
- javascript-this,call,apply,bind简述3
上节介绍了call()和apply()的用法,这节再讨论一下arguments参数和bind函数的用法以及函数柯里化就算是完结了. bind()函数 先看定义: bind()方法会创建一个函数的实例, ...
- 为网站添加ico图标
1.将目标图片装换为.ico格式 在线转换工具:http://www.bitbug.net/ 2.将装换后的图标放入项目文件中,一般命名为favicon 3.在项目文件的index页面中引入 < ...
- SharePoint 2013 创建搜索中心及搜索设置
本文没有太多深奥的东西,只是简单的搜索配置,如果你已经掌握请略过本文. 好了,进入内容简介,众所周知,搜索是SharePoint一大特性,下面,我们简单介绍下搜索中心的创建. 1.创建Search子网 ...
- Sharepoint学习笔记—习题系列--70-573习题解析 -(Q45-Q47)
Question45You create and deploy a custom Web Part.You add the Web Part to a page and receive a run-t ...
- C语言接口与实现实例
一个模块有两部分组成:接口和实现.接口指明模块要做什么,它声明了使用该模块的代码可用的标识符.类型和例程,实现指明模块是如何完成其接口声明的目标的,一个给定的模块通常只有一个接口,但是可能会有许多种实 ...
- 【读书笔记】iOS-Objective-C对C的扩展基础知识
一,Xcode的.m扩展名表示文件含有Objective-C代码,应由Objective-C编译器处理.C编译器处理名称以.c结尾的文件,而C++编译器处理.cpp文件.在Xcode中,所有这些编译工 ...
- 【原】IOS中KVO模式的解析与应用
最近老翁在项目中多处用到了KVO,深感这种模式的好处.现总结如下: 一.概述 KVO,即:Key-Value Observing,它提供一种机制,当指定的对象的属性被修改后,则对象就会接受到通知.简单 ...
- Dataset的基本操作
创建一个dateset并往其中加入列 DataSet dsClass = new DataSet(); DataTable dtClass = new DataTable("Class&qu ...