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 ...
随机推荐
- (旧)子数涵数·C语言——条件语句
首先,我们讲一下理论知识,在编程中有三种结构,分别是顺序结构.条件结构.循环结构,如果用流程图来表示的话就是: 那么在C语言中,如何灵活运用这三种结构呢?这就需要用到控制语句了. 而条件语句便是控制语 ...
- SharePoint 2013 调用WCF服务简单示例
内容比较简单,主要记录自己使用SharePoint 2013WCF服务遇到的小问题和小经验,分享给大家,希望能够给需要的人有所帮助.好吧,进入正题! 第一部分 SharePoint 2013调用自带W ...
- SQL 查询学生缺考情况
有三个表,如果学生缺考,那么在成绩表中就不存在这个学生的这门课程成绩的记录,写一段SQL语句,检索出每个学生缺考的科目.A 学生表(student)字段1 学生号(s_id)字段2 学生名(s_nam ...
- Android WelcomeActivity 启动画更换网络图片
1.运行效果 第一张是本地的启动图,第二张是网络启动图 2.用到的第三方jar包 Android-Universal-Image-Loader-master 不熟的请看 Andro ...
- Android 开关按钮切换,类似于iphone 效果,view实现
1.实现的效果 gitHub : https://github.com/zcweng/ToggleButton
- Redis介绍及常用命令
一 Redis介绍 Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API.从2010年3月15日起,Redis的开发 ...
- iOS开发网络篇—网络编程基础(一)
一.为什么要学习网络编程 1.简单说明 在移动互联网时代,移动应用的特征有: (1)几乎所有应用都需要用到网络,比如QQ.微博.网易新闻.优酷.百度地图 (2)只有通过网络跟外界进行数据交互.数据更新 ...
- java集合 之 set 集合
set集合可以存储多个对象,但并不会记住元素的存储顺序,也不允许集合中有重复元素(不同的set集合有不同的判断方法). 1.HashSet类 HashSet按照Hash算法存储集合中的元素,具有很好的 ...
- js检测浏览器型号
公司要求做内部统计,要求监控客服玩游戏使用的浏览器的型号,是火狐的.谷歌的.还是IE的等等. [code lang="javascript"] /**** * 目前识别范围 * M ...
- TCP & UDP & IP
TCP和UDP的区别 TCP UDP 是否连接 面向连接 面向非连接 应用场合 可靠的 不可靠的 速度 慢 快 传送数据 字节流 数据报 是否可用于广播 否 是 为什么UDP比TCP快 不需要连接 ...