在应用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. Careercup - Google面试题 - 4877486110277632

    2014-05-08 05:16 题目链接 原题: Given a circle with N defined points and a point M outside the circle, fin ...

  2. android activity之间传递返回值

    activity A,跳转至 Activity B ,A传参数user_name给B,然后B再返回修改后的参数user_name给A 首先A传user_name给B Intent input_B = ...

  3. The code of method _jspService(HttpServletRequest, HttpServletResponse) is exceeding the 65535 bytes limit

    如果你是通过搜索来到本文的,相信你应该是遇到了如下的错误 The code of method _jspService(HttpServletRequest, HttpServletResponse) ...

  4. 重启nginx后丢失nginx.pid解决

    /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

  5. 社区O2O,才是未来10年移动互联网最赚钱的项目

    原文:http://blog.sina.com.cn/s/blog_70e76a920102uyoi.html 8月12日  上海  晴 从深圳回来后,一直和郭老师探讨一个问题:新媒体营销未来最大的市 ...

  6. 四大机器学习降维算法:PCA、LDA、LLE、Laplacian Eigenmaps

    四大机器学习降维算法:PCA.LDA.LLE.Laplacian Eigenmaps 机器学习领域中所谓的降维就是指采用某种映射方法,将原高维空间中的数据点映射到低维度的空间中.降维的本质是学习一个映 ...

  7. imageNamed 与 imageWithContentsOfFile的区别

    如题,是不是大家为了方便都这样加载图片啊 myImage = [UIImage imageNamed:@"icon.png"];那么小心了这种方法在一些图片很少,或者图片很小的程序 ...

  8. asp.net @reqister指令

    @register指令通过声明将自定义 ASP.NET 服务器控件添加到页或用户控件中. 1.@register 指令有两种用法如下 <%@ Register tagprefix="t ...

  9. 【面试题043】n个骰子的点数

    [面试题043]n个骰子的点数 题目:     把n个骰子扔在地上,所有骰子朝上一面的点数之和为s, 输入n,打印出s的所有可能的值出现的概率.   n个骰子的总点数,最小为n,最大为6n,根据排列组 ...

  10. 由浅入深了解Thrift之服务模型和序列化机制

    一.Thrift介绍 Thrift是一个软件框架,用来进行可扩展且跨语言的服务的开发.它结合了功能强大的软件堆栈和代码生成引擎.其允许你定义一个简单的定义文件中的数据类型和服务接口.以作为输入文件,编 ...