- 在Servlet工程中,对通过classpath获取*.properties文件是常有的事,
-
但是他们究竟怎样个查找顺序,没太注意过,一直认为放到 /WEB-INF/classes/ 下即可,
-
但是如果自己要打一个jar包,并放到 /WEB-INF/lib/下,
-
如果jar包中包含配置文件, /WEB-INF/classes/下也包含,
-
代码是否会自动加载、按怎样的顺序加载?有点迷惑。
-
-
做了个小实验,然后google下文档,做个笔记先:
-
-
环境
-
J2SE 1.6
-
Tomcat6.0(安装在 E:/apache-tomcat-6.0.29)
-
-
工程结构:
-
testServletCp
-
|- WEB-INF
-
|- classes
-
| | aa.txt
-
| |- me
-
| |- test
-
| AAA.class
-
| 1.txt
-
|- lib
-
|- me.jar
-
| aa.txt
-
|- me
-
|- test
-
Test.class
-
1.txt
-
-
====== 使用ClassLoader加载资源
-
-
AAA.class.getClassLoader() == Test.class.getClassLoader() == Thread.currentThread().getContextClassLoader()
-
= true
-
-
-- 绝对路径
-
AAA.class.ClassLoader.getResource("/"))
-
= file:/E:/apache-tomcat-6.0.29/webapps/testServletCp/WEB-INF/classes/
-
-
AAA.class.ClassLoader.getResource("/aa.txt"))
-
= 如果 /WEB-INF/classes/aa.txt 存在,则为
-
file:/E:/apache-tomcat-6.0.29/webapps/testServletCp/WEB-INF/classes/aa.txt
-
= 否则为 null
-
-
-- 相对路径
-
AAA.class.ClassLoader.getResource("."))
-
= file:/E:/apache-tomcat-6.0.29/lib/
-
-
AAA.class.ClassLoader.getResource("aa.txt"))
-
= 如果 /WEB-INF/classes/aa.txt 存在,则为
-
file:/E:/apache-tomcat-6.0.29/webapps/testServletCp/WEB-INF/classes/aa.txt
-
= 否则为
-
jar:file:/E:/apache-tomcat-6.0.29/webapps/testServletCp/WEB-INF/lib/me.jar!/aa.txt
-
-
====== 通过具体某个类加载
-
-
Test.class.getResource("/"));
-
= file:/E:/apache-tomcat-6.0.29/webapps/testServletCp/WEB-INF/classes/
-
-
Test.class.getResource("."));
-
= null
-
-
Test.class.getResource("1.txt"));
-
= 如果 /WEB-INF/classes/me/test/1.txt 存在,则为
-
file:/E:/apache-tomcat-6.0.29/webapps/testServletCp/WEB-INF/classes/me/test/1.txt
-
= 否则为
-
jar:file:/E:/apache-tomcat-6.0.29/webapps/testServletCp/WEB-INF/lib/me.jar!/me/test/1.txt
-
-
--------------------------------------------------------------------------------
-
参考:
-
-
TOMCAT 6.X Class加载顺序
-
http://tomcat.apache.org/tomcat-6.0-doc/class-loader-howto.html
-
-
Bootstrap
-
|
-
System
-
|
-
Common
-
/ \
-
Webapp1 Webapp2 ...
-
-
-
* Bootstrap classes of your JVM
-
* System class loader classes (described above)
-
* $CATALINA_HOME/bin/bootstrap.jar
-
* $CATALINA_HOME/bin/tomcat-juli.jar
-
* /WEB-INF/classes of your web application
- * /WEB-INF/lib
阅读(910) | 评论(0) | 转发(0) |