您的位置:首页 > 编程语言 > Java开发

Spring中使用classpath加载配置文件浅析 2

2011-11-01 19:45 621 查看
情形四:使用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文件结构如图所示:



这时使用
ApplicationContext context = new

ClassPathXmlApplicationContext("classpath*:**/*application-context.xml");来创建ApplicationContext对象的话,Spring只会加载
bin/application-context.xml和bin/admin/admin-application-context.xml,
而jar包中的配置文件并不会被加载。这是因为Spring使用class
path加载配置文件时需要借助JDK的ClassLoader.getResources(String
name)方法,而该方法有一个局限:当传入的参数为空字符串时,即我们本意是想从根目录获取文件,这时JDK只会返回存在于文件系统中的资源,而在jar包中的资源并不会被返回。

我们在eclipse中写个简单的测试类就能很容易看到这点:
ClassLoader loader = Thread.currentThread().getContextClassLoader();
Enumeration resources = loader.getResources("");
while (resources.hasMoreElements()) {
URL url = (URL)resources.nextElement();
System.out.println(url);
}
运行测试类后,输出结果为:
file:/D:/myworkspace/spring-study/bin/
file:/D:/ProgramFiles/eclipse3.2/configuration/org.eclipse.osgi/bundles/47/1/.cp/
可以看到,获得的资源路径中并不包含jar包中的路径,因此jar包中的配置文件自然不能被Spring加载了。

Spring中使用classpath加载配置文件浅析

参考资料:
Spring Framework开发参考手册(中文翻译版)
http://www.redsaga.com/spring_ref/2.0/html/

java.lang.ClassLoader的API文档
http://java.sun.com/j2se/1.5.0/docs/api/

org.springframework.core.io.support.PathMatchingResourcePatternResolver的API文档
http://static.springframework.org/spring/docs/2.0.x/api/index.html

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: