classpath和classpath*的配置区别
2014-11-05 11:09
435 查看
在使用spring时,经常会看到类似 classpth:、classpath*: 这样的前缀,不管是加载spring xml配置文件还是其配置文件中加载资源文件都会看到这两种前缀配置,其实这两种前缀是有区别的,下面将举例详细解释。
[一]、测试项目准备
我们以spring中加载properties资源文件为例讲解,目录结构大致如下:
同时 在该项目的lib中添加一个测试的micmiu-test.jar包,jar包中的文件结构如下:
从准备的测试环境中我们可以看到在不同目录下的四个同名的application.properties资源文件。
[二]、测试代码:TestClassPath.java
[三]、测试结果
spring配置文件:applicationContext.xml 中两种不同的properties文件加载配置:
第一种:classpath:
这种配置下运行测试代码,日志信息中有关加载properties资源文件只有一条 如下:
第二种: classpath*:
这种配置下运行测试代码,日志信息中有关加载properties资源文件会有三条如下:
由此日志信息可知:
同名资源存在时,classpath: 只从第一个符合条件的classpath中加载资源,而classpath*: 会从所有的classpath中加载符合条件的资源
classpath*:需要遍历所有的classpath,效率肯定比不上classpath,因此在项目设计的初期就尽量规划好资源文件所在的路径,避免使用classpath*来加载
原:://www.micmiu.com/j2ee/spring/spring-classpath-start/
[一]、测试项目准备
我们以spring中加载properties资源文件为例讲解,目录结构大致如下:
src ├─main │ ├─filters │ │ │ ├─java │ │ └─com │ │ └─micmiu │ │ ├─demoweb │ │ │ │ .... │ │ │ │ │ │ │ └─utils │ │ │ │ │ └─modules │ │ │ ├─resources │ │ │ application.properties │ │ │ applicationContext-shiro.xml │ │ │ applicationContext.xml │ │ │ hibernate.cfg.xml │ │ │ log4j.properties │ │ │ spring[/u]-mvc.xml │ │ │ spring-view.xml │ │ │ └─webapp │ │ │ └─WEB-INF │ └─test ├─java │ └─com │ └─micmiu │ ├─demoweb │ │ TestOther.java │ └─resources application.properties
同时 在该项目的lib中添加一个测试的micmiu-test.jar包,jar包中的文件结构如下:
micmiu[/u]-test.jar │ application.properties │ ├─com │ └─micmiu │ └─test │ application.properties │ RunApp.class │ └─META-INF MANIFEST.MF
从准备的测试环境中我们可以看到在不同目录下的四个同名的application.properties资源文件。
[二]、测试代码:TestClassPath.java
1 | package com.micmiu.demoweb; |
2 |
3 | import org.springframework.context.ApplicationContext; |
4 | import org.springframework.context.support.ClassPathXmlApplicationContext; |
5 |
6 | /** |
7 | * |
8 | * @author <a href="http://www.micmiu.com">Michael Sun</a> |
9 | */ |
10 | public class TestClassPath { |
11 |
12 | /** |
13 | * @param args |
14 | */ |
15 | public static void main(String[] args) { |
16 | ApplicationContext ctx = new ClassPathXmlApplicationContext( |
17 | "classpath:/applicationContext.xml" ); |
18 | System.out.println(ctx.getClassLoader().getResource( "" ).getPath()); |
19 |
20 | } |
21 | } |
spring配置文件:applicationContext.xml 中两种不同的properties文件加载配置:
第一种:classpath:
<context:property-placeholder ignore-unresolvable="true" location="classpath:/application.properties" />
这种配置下运行测试代码,日志信息中有关加载properties资源文件只有一条 如下:
Loading properties file from class path resource[/u] [application.properties]
第二种: classpath*:
<context:property-placeholder ignore-unresolvable="true" location="classpath*:/application.properties" />
这种配置下运行测试代码,日志信息中有关加载properties资源文件会有三条如下:
Loading properties file[/u] from URL [file:/D:/workspace_sun/framework-dev/micmiu-demoweb/target[/u]/test-classes/application.properties] Loading properties file from URL [file:/D:/workspace_sun/framework-dev/micmiu[/u]-demoweb/target[/u]/classes/application.properties] Loading properties file from URL [jar:file:/D:/micmiu-test.jar!/application.properties[/u]]
由此日志信息可知:
同名资源存在时,classpath: 只从第一个符合条件的classpath中加载资源,而classpath*: 会从所有的classpath中加载符合条件的资源
classpath*:需要遍历所有的classpath,效率肯定比不上classpath,因此在项目设计的初期就尽量规划好资源文件所在的路径,避免使用classpath*来加载
原:://www.micmiu.com/j2ee/spring/spring-classpath-start/
相关文章推荐
- Spring加载配置文件时 classpath* 和 classpath的区别
- web.xml配置中classpath:与classpath*:的区别
- Spring配置中的"classpath:"与"classpath*:"的区别研究(转)
- spring【2】( web.xml配置中classpath:与classpath*:的区别)
- web.xml 配置中classpath: 与classpath*:的区别 web.xmlclasspath:classpath*: 首先 classpath是指 WEB-INF文件夹下的clas
- Spring配置中的classpath和classpath*的区别
- javaweb项目中web.xml配置classpath 和 classpath* 区别
- spring中classpath和classpath*的配置区别
- spring中classpath和classpath*的配置区别
- web.xml配置中classpath:与classpath*:的区别
- classpath和classpath*的配置区别
- spring中classpath和classpath*的配置区别
- spring中classpath和classpath*的配置区别
- spring配置文件中classpath与classpath* 的区别
- Spring配置中的"classpath:"与"classpath*:"的区别研究(转)
- spring中classpath和classpath*的配置区别
- spring配置中classpath和classpath*的区别
- web.xml 配置中“classpath” 与“classpath*”的区别
- spring中classpath和classpath*的配置区别
- classpath 和 classpath* 区别以及如何覆盖在配置文件中的Bean ID