在应用Spring的工程中,使用class path的方式加载配置文件应该是最常用的做法,然而对大部分人来说,刚开始使用Spring时,几乎都碰到过加载配置文件失败的情况,除了配置上的错误外,很多时候是因为配置文件的路径和程序中指定的加载路径不一致,从而导致配置文件找不到,或是加载了错误地方的配置文件。本文将就Spring如何从class path中加载配置文件做一些简要的分析。

classpath:与classpath*:的区别在于,前者只会从第一个classpath中加载,而后者会从所有的classpath中加载

如果要加载的资源,不在当前ClassLoader的路径里,那么用classpath:前缀是找不到的,这种情况下就需要使用classpath*:前缀

另一种情况下,在多个classpath中存在同名资源,都需要加载,那么用classpath:只会加载第一个,这种情况下也需要用classpath*:前缀

可想而知,用classpath*:需要遍历所有的classpath,所以加载速度是很慢的,因此,在规划的时候,应该尽可能规划好资源文件所在的路径,尽量避免使用classpath*

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

这是最简单的情形,Spring默认会使用当前线程的ClassLoader的getResource方法获取资源的URL,如果无法获得当前线程的ClassLoaderSpring将使用加载类org.springframework.util.ClassUtils的ClassLoader。

1.当工程目录结构如图所示:

 

即配置文件放在bin目录中的conf文件夹里,这时使用

ApplicationContext context =

new ClassPathXmlApplicationContext("conf/application-context.xml");来创建ApplicationContext对象的话,Spring将加载bin/conf目录下的application-context.xml文件。Spring启动时的输出显示为:

Loading XML bean definitions from

class path resource [conf/application-context.xml]

 
2.当工程目录结构如图所示:
即bin目录下只有.class文件,没有配置文件,同时在工程属性的Java Build Path->Libraries里导入conf.jar文件,jar文件结构如图所示:

这时使用

ApplicationContext context =

new ClassPathXmlApplicationContext("conf/application-context.xml");来创建ApplicationContext对象的话,Spring将加载conf.jar文件中conf目录下的application-context.xml文件。Spring启动时的输出显示为:

Loading XML bean definitions from

class path resource [conf/application-context.xml]

 
3. 当工程目录结构如图所示:
即配置文件放在bin目录中的conf文件夹里,同时在工程属性的Java Build Path->Libraries里导入conf.jar文件,jar文件结构如图所示:

这时使用

ApplicationContext context =

new ClassPathXmlApplicationContext("conf/application-context.xml");来创建ApplicationContext对象的话,由于没有使用classpath*前缀,Spring只会加载一个application-context.xml文件。在eclipse中将会加载bin/conf目录下的application-context.xml文件,而jar包中的conf/application-context.xml并不会被加载,Spring启动时的输出显示为:

Loading XML bean definitions from

class path resource [conf/application-context.xml]
    
    
 
 

情形二:使用classpath加载,包含通配符

碰到通配符的情况时,Spring会通过使用路径中的非通配符部分先确定资源的大致位置,然后根据这个位置在确定具体的资源位置,结合下面给出的几种情况可以更好地理解Spring的这种工作方式

1. 当工程目录结构如图所示:

即配置文件放在bin目录中的conf文件夹里,这时使用

ApplicationContext context = new

ClassPathXmlApplicationContext("conf/**/*application-context.xml");

来创建ApplicationContext对象的话,Spring首先会通过路径中的非通配符部分即conf,先确定conf的路径,即bin/conf目录,然后从该目录下加载配置文件,由于使用了/**/的方式,表明要加载conf目录下包括各级子目录中的所有配置文件,因此bin/conf/application-context.xml文件和

bin/conf/admin/admin-application-context.xml都会被加载,Spring启动时的输出显示为:

Loading XML bean definitions from file

[D:\myworkspace\spring-study\bin\conf\admin\admin-application-context.xml]

Loading XML bean definitions from file

[D:\myworkspace\spring-study\bin\conf\application-context.xml]

2.当工程目录结构如图所示:
即bin目录下只有.class文件,没有配置文件,同时在工程属性的Java Build Path->Libraries里导入conf.jar文件,jar文件结构如图所示:

这时使用

ApplicationContext context = new

ClassPathXmlApplicationContext("conf/**/*application-context.xml");来创建ApplicationContext对象的话,Spring首先会通过路径中的非通配符部分即conf,先确定conf的路径,即conf.jar中的conf目录,然后从该目录下加载配置文件,由于使用了/**/的方式,表明要加载conf目录下包括各级子目录中的所有配置文件,因此conf/application-context.xml文件和

conf/admin/admin-application-context.xml都会被加载,Spring启动时的输出显示为:

Loading XML bean definitions from class path resource

[conf/admin/admin-application-context.xml]

Loading XML bean definitions from class path resource

[conf/application-context.xml]

3.当工程目录结构如图所示:
即配置文件放在bin目录中的conf文件夹里,同时在工程属性的Java Build Path->Libraries里导入conf.jar文件,jar文件结构如图所示:

这时使用

ApplicationContext context = new

ClassPathXmlApplicationContext("conf/**/*application-context.xml");来创建ApplicationContext对象的话,Spring首先会通过路径中的非通配符部分即conf,先确定conf的路径,在eclipse中是bin/conf目录,然后从该目录下加载配置文件,由于使用了/**/的方式,表明要加载conf目录下包括各级子目录中的所有配置文件,因此bin/conf/application-context.xml文件和

bin/conf/admin/admin-application-context.xml都会被加载,但conf.jar文件中的配置文件并不会被加载,Spring启动时的输出显示为:

Loading XML bean definitions from file

[D:\myworkspace\spring-study\bin\conf\admin\admin-application-context.xml]

Loading XML bean definitions from file

[D:\myworkspace\spring-study\bin\conf\application-context.xml]
  
  

情形三:使用classpath*前缀且不包含通配符

使用classpath*前缀可以获取所有与给定路径匹配的classpath资源,从而避免出现两个不同位置有相同名字的文件,Spring只加载其中一个的情况。

当工程目录结构如图所示:
即配置文件放在bin目录中的conf文件夹里,同时在工程属性的Java Build Path->Libraries里导入conf.jar文件,jar文件结构如图所示:

这时使用

ApplicationContext context = new

ClassPathXmlApplicationContext("classpath*:conf/application-context.xml");来创建ApplicationContext对象的话, Spring将会加载bin目录下的application-context.xml文件和jar包里的application-context.xml文件,Spring启动时的输出显示为:

Loading XML bean definitions from URL

[file:/D:/myworkspace/spring-study/bin/conf/application-context.xml]

Loading XML bean definitions from URL

[jar:file:/D:/myworkspace/conf1.jar!/conf/application-context.xml]

情形四:使用classpath*前缀,包含通配符

当工程目录结构如图所示:

即配置文件放在bin目录中的conf文件夹里,同时在工程属性的Java Build Path->Libraries里导入conf.jar文件,jar文件结构如图所示:

这时使用

ApplicationContext context = new

ClassPathXmlApplicationContext("classpath*:conf/**/*application-context.xml");来创建ApplicationContext对象的话,Spring首先会通过路径中的非通配符部分即conf,先确定conf的路径,由于使用了classpaht*前缀,因此bin目录下的conf和jar包里的conf都会被加载,同时由于使用了/**/的方式,表明要加载conf目录下包括各级子目录中的所有配置文件,因此bin/conf/application-context.xml和

bin/conf/admin/admin-application-context.xml以及jar包中的

conf/application-context.xml和

conf/admin/admin-application-context.xml都会被加载,Spring启动时的输出显示为:

Loading XML bean definitions from file

[D:\myworkspace\spring-study\bin\conf\admin\admin-application-context.xml]

Loading XML bean definitions from file

[D:\myworkspace\spring-study\bin\conf\application-context.xml]

Loading XML bean definitions from URL

[jar:file:/D:/myworkspace/conf1.jar!/conf/admin/admin-application-context.xml]

Loading XML bean definitions from URL

[jar:file:/D:/myworkspace/conf1.jar!/conf/application-context.xml]

特别注意:

如果工程目录如图所示:
即配置文件直接放在bin目录中,同时在工程属性的Java Build Path->Libraries里导入conf.jar文件,jar

spring路径通配符的更多相关文章

  1. spring的路径通配符

    Spring提供了强大的Ant模式通配符匹配,从同一个路径能匹配一批资源. Ant路径通配符支持"?"."*"."**",注意通配符匹配不包 ...

  2. spring 路径配置通配符是如何实现的

    在spring的配置文件中.经常看见类似这样的配置路径: classpath:/com/module/**/*sql.xml 系统会根据配置路径自动加载符合路径规则的xml文件. Spring还提供了 ...

  3. spring3: 4.4 使用路径通配符加载Resource

    4.4.1  使用路径通配符加载Resource 前面介绍的资源路径都是非常简单的一个路径匹配一个资源,Spring还提供了一种更强大的Ant模式通配符匹配,从能一个路径匹配一批资源. Ant路径通配 ...

  4. Java使用路径通配符加载Resource与profiles配置使用

    序言 Spring提供了一种强大的Ant模式通配符匹配,能从一个路径匹配一批资源. Ant路径通配符 Ant路径通配符支持“?”.“*”.“**”,注意通配符匹配不包括目录分隔符“/”: “?”:匹配 ...

  5. Spring中通配符

    一.加载路径中的通配符:?(匹配单个字符),*(匹配除/外任意字符).**/(匹配任意多个目录) classpath:app-Beans.xml 说明:无通配符,必须完全匹配   classpath: ...

  6. Spring中通配符问题

    一.加载路径中的通配符 (1)?(匹配单个字符) (2)*(匹配除/外任意字符) (3)**/(匹配任意多个目录) 示例: (1)classpath:app-Beans.xml 说明:无通配符,必须完 ...

  7. Spring中通配符(转)

    一.加载路径中的通配符:?(匹配单个字符),*(匹配除/外任意字符).**/(匹配任意多个目录) classpath:app-Beans.xml 说明:无通配符,必须完全匹配   classpath: ...

  8. Spring:通配符的匹配很全面, 但无法找到元素 XXXXX' 的声明

    问题:配置Spring的时候容易发生如题的这样一个经常性的错误,错误如下(以context为例) org.springframework.beans.factory.xml.XmlBeanDefini ...

  9. Spring @CrossOrigin 通配符 解决跨域问题

    @CrossOrigin 通配符 解决跨域问题 痛点: 对很多api接口需要 开放H5 Ajax跨域请求支持 由于环境多套域名不同,而CrossOrigin 原生只支持* 或者具体域名的跨域支持 所以 ...

随机推荐

  1. htaccess 探秘

    .htaccess访问控制(Allow/Deny) 1. 验证是否支持.htaccess 在目录下新建一个.htaccess 文件,随笔输入一串字符(毫无意义),看看什么反应,如果是500错误,说明目 ...

  2. 【BZOJ】【1015】 【JSOI2008】星球大战starwar

    并查集/时光倒流 删点维护连通块个数比较难处理,所以我们就逆序来做,先处理最后状态下有多少连通块,再依次加入被删的点,这样就变删点为加点,利用并查集即可维护连通块个数. /************** ...

  3. Leetcode#89 Gray Code

    原题地址 二进制码 -> 格雷码:从最右边起,依次与左边相邻位异或,最左边一位不变.例如: 二进制: 1 0 0 1 1 1 0 |\|\|\|\|\|\| 格雷码: 1 1 0 1 0 0 1 ...

  4. MongoDB 基础

    1. 安装 mongodb-win32-x86_64-2008plus-2.6.12-signed.msi,下载地址 https://www.mongodb.com/download-center#c ...

  5. HDU1005Number Sequence(找规律)

    Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  6. VSS

    A deleted file of the same name already exists in this VSS project. Do you want to recover the delet ...

  7. redis集群部署之codis 维护脚本

    搞了几天redis cluster codis 的部署安装,测试,架构优化,配合研发应用整合,这里记一些心得! 背景需求: 之前多个业务都在应用到redis库,各业务独立占用主从两台服务器,硬件资源利 ...

  8. 常用正则表达式(匹配URL/email/number)

    var URL_REGEXP = /^(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/ ...

  9. 表单很多数据项录入的时候,提交controller发生异常,数据回显。

    1.添加的情况(Model传递Form Data) request.getSession().setAttribute("car", car); //抛出异常的时候,数据回显. 2 ...

  10. 去“IOE”

    所谓去“IOE”,是对去“IBM.Oracle.EMC”的简称,三者均为海外IT巨头,其中IBM代表硬件以及整体解决方案服务商,Oracle代表数据库,EMC代表数据存储.去“IOE”策略更广泛的理解 ...