2011年4月17日 星期日

[Configuration] classpath with Spring

* J2EE default classpath:
- /WEB-INF/classes

* Spring 默认会使用当前线程的 ClassLoader 的 getResource 方法获取资源的 URL,如果无法获得当前线程的ClassLoader,Spring 将使用加载类 org.springframework.util.ClassUtils 的 ClassLoader。

* "classpath*:"
  • 是為了從多個 jar 文件中加載相同的文件。
  • 使用 classpath* 前缀可以获取所有与给定路径匹配的 classpath 资源,从而避免出现两个不同位置有相同名字的文件,Spring 只加载其中一个的情况。
  • You know you could actually have these context files loaded by different class loaders but in same application. 
    • Ex: EAR loading contexts in war and META-INF jars in separate class loaders. As Long as these are in the same VM, You can do a multi classloader resource.
  • ex: "classpath*:META-INF/*-beans.xml": a ClassLoader.getResources() call is used on the last non-wildcard path segment to get all the matching resources in the class loader hierarchy, and then off each resource the same PathMatcher resoltion strategy described above is used for the wildcard subpath.

* "classpath:"
- 只能加載找到的第一個文件。


* Reference
- Spring加载resource时classpath*:与classpath:的区别
- Chapter 4. Resources
- classpath*: making your Modular Spring Resources
- Spring中使用classpath加载配置文件浅析 **
- Spring中使用classpath加载配置文件浅析(续

沒有留言:

張貼留言