转载自跳刀的兔子   http://www.cnblogs.com/shipengzhi/articles/3029872.html

加载文件顺序

情形一:使用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配置文件的方式(BeanFactory和ApplicationContext区别)

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

  2. spring加载xml的六种方式

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

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

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

  4. Spring加载XML配置文件

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

  5. spring加载xml

    加载文件顺序 情形一:使用classpath加载且不含通配符 这是最简单的情形,Spring默认会使用当前线程的ClassLoader的getResource方法获取资源的URL,如果无法获得当前线程 ...

  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. setTimeout和setInterval和单线程

    我们知道,js是单线程执行的(单线程j就是说在程序执行时,所走的程序路径按照连续顺序排下来,前面的必须处理好,后面的才会执行).所以其实setTimeout和setInterval所谓的"异 ...

  2. 基于Java使用Snmp4j进行监控与采集(snmptrap、snmpwalk、snmpget)

    之前有在弄监控服务器这块的工作,今天来整体总结下.因为有些服务器(路由器.交换机等都是基于snmp协议的)必须使用snmp协议去监控采集和接收信息,所以必须去了解snmp相关内容,以及如何在基于jav ...

  3. as 与 is

    在存储过程(PROCEDURE)和函数(FUNCTION)中没有区别:在视图(VIEW)中只能用AS:在游标(CURSOR)中只能用IS.

  4. Python 项目实践一(外星人入侵小游戏)第二篇

    接着上次的继续学习. 一 创建一个设置类 每次给游戏添加新功能时,通常也将引入一些新设置.下面来编写一个名为settings的模块,其中包含一个名为Settings的类,用于将所有设置存储在一个地方, ...

  5. Struts2中validate数据校验的两种常用方法

    本文主要介绍Struts2中validate数据校验的两种方法及Struts2常用校验器.  1.Action中的validate()方法 Struts2提供了一个Validateable接口,这个接 ...

  6. [转]压缩感知重构算法之分段正交匹配追踪(StOMP)

    分段正交匹配追踪(StagewiseOMP)或者翻译为逐步正交匹配追踪,它是OMP另一种改进算法,每次迭代可以选择多个原子.此算法的输入参数中没有信号稀疏度K,因此相比于ROMP及CoSaMP有独到的 ...

  7. effective java学习笔记之不可实例化的类

    在没有显式声明一个类的构造方法时,编译器会生成默认的无参构造方法,在设计工具类时,我们通常将方法设置成静态方法,以类名.方法名的形式调用,此时这个类就没有必要创建实例,我们知道抽象类不可以被实例化,但 ...

  8. mybatis的#{}占位符和${}拼接符的区别

    #{}占位符:占位 如果传入的是基本类型,那么#{}中的变量名称可以随意写 如果传入的参数是pojo类型,那么#{}中的变量名称必须是pojo中的属性.属性.属性- ${}拼接符:字符串原样拼接 如果 ...

  9. Intellij 解除(去除)SVN关联

    方法一:通过Intellj 来处理 1. 安装插件 打开Intellij的setting(ctrl+alt+s),选择plugins,在右边搜索框输入"SVN",搜索.选择&quo ...

  10. 前端构建之gulp与常用插件(转载)

    原博主:幻天芒 原文地址:http://www.cnblogs.com/humin/p/4337442.html gulp是什么? http://gulpjs.com/ 相信你会明白的! 与著名的构建 ...