转载自跳刀的兔子   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. Redis在centos6.4上的最详细图文安装教程

    准备工作:一个redis3.0.0的安装包,没有可以点击下面的链接下载 https://pan.baidu.com/s/1kU5Ez2J 工具 安装环境   centos6.4 好了开始进入正题 输入 ...

  2. 【垃圾回收】Java内存回收实践经验 防止内存报警

    jdk6和7服务器端(-server) 默认的新生代的垃圾回收器为:PS Scavenge,老年代默认的垃圾回收器为:PS MarkSweep 目前项目使用了jdk7,tomcat7,经常出现内存堆使 ...

  3. The authenticity of host 'github.com (192.30.253.113)' can't be established.

    在初始化git之后(git init),同时在github建立好仓库之后,本地也新增了ssh kye(ssh-keygen -t rsa -C 'mail address'),同时也在本地新增了远程仓 ...

  4. 将DLL文件直接封装进exe执行文件中(C#)

    前言:由于项目需要,需制作一个注册机,将个人PC的MAC值和硬盘序列号与软件进行绑定,由于笔者的C++不是很好,所以采用C#进行开发.但在采用C#的时候,获取硬盘的MAC值和序列号的时候又不是很准确, ...

  5. 17. ZooKeeper常见的分布式系统任务——屏障

    以后几节中主要介绍以下内容: 如何执行领导者选举,组员管理和两阶段提交协议等常见的分布式系统任务 如何实现一些分布式数据结构,如屏障(barrier),锁(lock)和队列(queue) 这一章中概述 ...

  6. PHP开发中需要注意几点事项,新手少走弯路必备知识

    这篇文章主要介绍了PHP开发需要注意的几点事项总结,非常详细,需要的朋友可以参考下.新手多看看避免走弯路. 1.使用内嵌的HTML代码,而不是PHP的echo语句. 因为PHP是一门嵌入式Web编程语 ...

  7. AbstractFactory抽象工厂模式

    #include <iostream> using namespace std; class ProductAbase { public: ProductAbase(){} virtual ...

  8. MobileNets总结

    Google在2017年上半年发表了一篇关于可以运行在手机等移动设备上的神经网络结构--MobileNets.MobileNets是基于深度可分离卷积(depthwise separable conv ...

  9. Shiro学习(一)总体介绍

    1.1  简介 Apache Shiro是Java的一个安全框架.目前,使用Apache Shiro的人越来越多,因为它相当简单,对比Spring Security,可能没有Spring Securi ...

  10. 【转】Spring 中三种Bean配置方式比较

    今天被问到Spring中Bean的配置方式,很尴尬,只想到了基于XML的配置方式,其他的一时想不起来了,看来Spring的内容还没有完全梳理清楚,见到一篇不错的文章,就先转过来了. 以前Java框架基 ...