关于Spring加载classpath与classpath*

来源:互联网 发布:A.join() js 编辑:程序博客网 时间:2024/06/05 15:21
原址:http://blog.csdn.net/truong/article/details/26979365

1.无论是classpath还是classpath*都可以加载整个classpath下(包括jar包里面)的资源文件。
2.classpath只会返回第一个匹配的资源,查找路径是优先在项目中存在资源文件,再查找jar包。
3.文件名字包含通配符资源(如果spring-*.xml,spring*.xml),   如果根目录为"", classpath加载不到任何资源, 而classpath*则可以加载到classpath中可以匹配的目录中的资源,但是不能加载到jar包中的资源
   
      第1,2点比较好表理解,大家可以自行测试,第三点表述有点绕,举个例,现在有资源文件结构如下:


classpath:notice*.txt                                                               加载不到资源
classpath*:notice*.txt                                                            加载到resource根目录下notice.txt
classpath:META-INF/notice*.txt                                          加载到META-INF下的一个资源(classpath是加载到匹配的第一个资源,就算删除classpath下的notice.txt,他仍然可以                                                                                                  加载jar包中的notice.txt)
classpath:META-*/notice*.txt                                              加载不到任何资源
classpath*:META-INF/notice*.txt                                        加载到classpath以及所有jar包中META-INF目录下以notice开头的txt文件
classpath*:META-*/notice*.txt                                             只能加载到classpath下 META-INF目录的notice.txt
0 0
原创粉丝点击