加载文件顺序

情形一:使用classpath加载且不含通配符

这是最简单的情形,Spring默认会使用当前线程的ClassLoader的getResource方法获取资源的URL,如果无法获得当前线程的ClassLoaderSpring将使用加载类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的更多相关文章

  1. spring加载xml的六种方式

    因为目前正在从事一个项目,项目中一个需求就是所有的功能都是插件的形式装入系统,这就需要利用Spring去动态加载某一位置下的配置文件,所以就总结了下Spring中加载xml配置文件的方式,我总结的有6 ...

  2. Spring加载xml配置文件的方式 ApplicationContext

    大家都知道Java读普通文件是通过Basic I/O 中的InputStream.OutStream.Reader.Writer 等实现的.在spring 框架中,它是怎样识别xml这个配置文件的呢? ...

  3. Spring加载XML配置文件

    原创链接:http://www.cnblogs.com/yanqin/p/5282929.html(允许转载,但请注明原创链接) BeanFactory加载单个文件 当使用beanfactory去获取 ...

  4. Spring加载xml配置文件的方式(BeanFactory和ApplicationContext区别)

    描述 大家都知道Java读普通文件是通过Basic I/O 中的InputStream.OutStream.Reader.Writer 等实现的.在spring 框架中,它是怎样识别xml这个配置文件 ...

  5. Spring加载XML机制

    转载自跳刀的兔子   http://www.cnblogs.com/shipengzhi/articles/3029872.html 加载文件顺序 情形一:使用classpath加载且不含通配符 这是 ...

  6. Spring加载xml配置文件的方式

    梳理Spring的流程 xml是最常见的spring 应用系统配置源.Spring中的几种容器都支持使用xml装配bean,包括: XmlBeanFactory,ClassPathXmlApplica ...

  7. (Spring加载xml时)org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'beans'.

    ApplicationContext ctx = new ClassPathXmlApplicationContext("test.xml");报错 在启动Spring时,报以下错 ...

  8. Spring中加载xml配置文件的六种方式

    Spring中加载xml配置文件的六种方式 博客分类: Spring&EJB XMLSpringWebBeanBlog  因为目前正在从事一个项目,项目中一个需求就是所有的功能都是插件的形式装 ...

  9. spring加载ApplicationContext.xml的四种方式

    spring 中加载xml配置文件的方式,好像有4种, xml是最常见的spring 应用系统配置源.Spring中的几种容器都支持使用xml装配bean,包括: XmlBeanFactory , C ...

随机推荐

  1. sqlite3之基本操作(一)

    简单的介绍 SQLite数据库是一款非常小巧的嵌入式开源数据库软件,也就是说没有独立的维护进程,所有的维护都来自于程序本身.它是遵守ACID的关联式数据库管理系统,它的设计目标是嵌入式的,而且目前已经 ...

  2. Python基础(二),Day2

    常用的数据类型 int 整型 float 浮点 bool 布尔 string 字符串 列表的语法和用法 # 创建一个列表 list = [] #一个空列表 list = ['2323123','asd ...

  3. JSON-RPC轻量级远程调用协议介绍及使用

    这个项目能够帮助开发人员利用Java编程语言轻松实现JSON-RPC远程调用.jsonrpc4j使用Jackson类库实现Java对象与JSON对象之间的相互转换.jsonrpc4j包含一个JSON- ...

  4. Vue表单

    gitHub地址: https://github.com/lily1010/vue_learn/tree/master/lesson11 一 vue表单 实在是太简单了,直接来个例子 <!DOC ...

  5. How does Web Analytics works under sharePoint 2010

    [http://gokanx.wordpress.com/2013/06/15/how-does-web-analytics-works-under-sharepoint-2010/] You nee ...

  6. 自定义组件 -- android联系人

    在android开发中,常常有联系人页面,在这篇和大家分享一下项目中刚刚添加的联系人页面,代码直接从项目中提取出来,没有太多时间修改:使用 StickyListHeaders-master 开源项目及 ...

  7. JDBC编程 之 增删改查

    JDBC编程之数据增加,更改,查询,删除 package com.good.jdbc; import java.sql.Connection; import java.sql.DriverManage ...

  8. 关于Socket建立长连接遇到的bug信息

    下面是本人在Socket连接的开发中遇到的bug总结 1."远程服务器关闭了Socket长连接'的错误信息 2.关于"kCFStreamNetworkServiceTypeVoIP ...

  9. 《极客学院 --NSAttributedString 使用详解-4-UITextKit 简介》学习笔记(待处理)

    如果要在富文本中添加图片的话,用UITextKit才能实现. 什么是UITextKit:它就是处理富文本的框架. 什么时候使用UITextKit:比如要实现图文混搭的节目. 在gitHub中 http ...

  10. Jquery DataTables相关示例

    一.Jquery-DataTables DataTables 是jquery的一个开源的插件.它具有高度灵活的特性,基于渐进增强的基础,可以为任何表格添加交互.它特性如下: 提供分页,搜索和多列排序: ...