spring路径通配符
在应用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
,如果无法获得当前线程的
ClassLoader
,
Spring
将使用加载类
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]


这时使用
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]


这时使用
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
情形二:使用classpath加载,包含通配符
碰到通配符的情况时,Spring会通过使用路径中的非通配符部分先确定资源的大致位置,然后根据这个位置在确定具体的资源位置,结合下面给出的几种情况可以更好地理解Spring的这种工作方式

即配置文件放在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]


这时使用
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]


这时使用
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
情形三:使用classpath*前缀且不包含通配符
使用classpath*前缀可以获取所有与给定路径匹配的classpath资源,从而避免出现两个不同位置有相同名字的文件,Spring只加载其中一个的情况。


这时使用
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*前缀,包含通配符
当工程目录结构如图所示:


这时使用
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]
特别注意:

spring路径通配符的更多相关文章
- spring的路径通配符
Spring提供了强大的Ant模式通配符匹配,从同一个路径能匹配一批资源. Ant路径通配符支持"?"."*"."**",注意通配符匹配不包 ...
- spring 路径配置通配符是如何实现的
在spring的配置文件中.经常看见类似这样的配置路径: classpath:/com/module/**/*sql.xml 系统会根据配置路径自动加载符合路径规则的xml文件. Spring还提供了 ...
- spring3: 4.4 使用路径通配符加载Resource
4.4.1 使用路径通配符加载Resource 前面介绍的资源路径都是非常简单的一个路径匹配一个资源,Spring还提供了一种更强大的Ant模式通配符匹配,从能一个路径匹配一批资源. Ant路径通配 ...
- Java使用路径通配符加载Resource与profiles配置使用
序言 Spring提供了一种强大的Ant模式通配符匹配,能从一个路径匹配一批资源. Ant路径通配符 Ant路径通配符支持“?”.“*”.“**”,注意通配符匹配不包括目录分隔符“/”: “?”:匹配 ...
- Spring中通配符
一.加载路径中的通配符:?(匹配单个字符),*(匹配除/外任意字符).**/(匹配任意多个目录) classpath:app-Beans.xml 说明:无通配符,必须完全匹配 classpath: ...
- Spring中通配符问题
一.加载路径中的通配符 (1)?(匹配单个字符) (2)*(匹配除/外任意字符) (3)**/(匹配任意多个目录) 示例: (1)classpath:app-Beans.xml 说明:无通配符,必须完 ...
- Spring中通配符(转)
一.加载路径中的通配符:?(匹配单个字符),*(匹配除/外任意字符).**/(匹配任意多个目录) classpath:app-Beans.xml 说明:无通配符,必须完全匹配 classpath: ...
- Spring:通配符的匹配很全面, 但无法找到元素 XXXXX' 的声明
问题:配置Spring的时候容易发生如题的这样一个经常性的错误,错误如下(以context为例) org.springframework.beans.factory.xml.XmlBeanDefini ...
- Spring @CrossOrigin 通配符 解决跨域问题
@CrossOrigin 通配符 解决跨域问题 痛点: 对很多api接口需要 开放H5 Ajax跨域请求支持 由于环境多套域名不同,而CrossOrigin 原生只支持* 或者具体域名的跨域支持 所以 ...
随机推荐
- 开发中可能会用到的几个 jQuery 小提示和技巧(转)
今天,我们将分享一些很有用的技巧和窍门给 jQuery 开发人员.jQuery 是最好的 JavaScript 库之一,用于简化动画,事件处理,支持 Ajax 和 HTML 的客户端脚本.网络中有大量 ...
- 细究UTF-8,GB2312及ISO-8859-1区别
各个国家和地区所制定的不同 ANSI 编码标准中,都只规定了各自语言所需的“字符”.比如:汉字标准(GB2312)中没有规定韩国语字符怎样存储.这些 ANSI 编码标准所规定的内容包含两层含义:1. ...
- Workaround for Markdown blogging platform that to use LaTeX
Here is a solution to solve the lack of LaTeX support: by using Codecogs API. For instance, if you n ...
- navicat for mysql 注册码
PremiumSoft Navicat for MySQL Enterprise Edition v8.0.27 姓名(Name):3ddown.com 组织(Organization):3ddown ...
- ngcordova 监控网络制式改变
ngcordova 监控网络制式改变 keywords cordova,phonegap,ionic,network,网络制式 API参考 http://ngcordova.com/docs/plug ...
- hdu 1301 Jungle Roads 最小生成树
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1301 The Head Elder of the tropical island of Lagrish ...
- Leetcode#71 Simplify Path
原题地址 用栈保存化简后的路径.把原始路径根据"/"切分成若干小段,然后依次遍历 若当前小段是"..",弹栈 若当前小段是".",什么也不做 ...
- 一个perfect 的解决 阴影拉伸的方法 shadow map strech
因为在场景中做了,有的物体产生阴影比如人物,有的物体不产生阴影比如地面,这样在地面凹下去的地方,悬崖,池塘边,就会有阴影的拉伸. 实际上, 没办法上传图片.... L是光源 A 点(人物身上)产生阴影 ...
- topcoder 642
A:直接拆开字符串写就好了. 今天的题目比较容易一些: B:题目大意: 求最少的翻转次数,每次翻转i是对应 y%i==0都会翻转. 球所有翻转为off状态的最小次数: 从最小idx开始,依次做就好了, ...
- LCIS(m*n) 最长公共上升子序列
详见:http://wenku.baidu.com/view/3e78f223aaea998fcc220ea0n3的: for(int i=1;i<=n;i++) for ...