首页   快速返回

spring资源加载机制     所属分类 spring
classpath与classpath* 的区别

classpath   只加载第一个
classpath*  加载所有匹配的资源

ResourceLoader
org.springframework.core.io.ResourceLoader


public interface ResourceLoader {      
        // Pseudo URL prefix for loading from the class path: "classpath:"  
        String CLASSPATH_URL_PREFIX = ResourceUtils.CLASSPATH_URL_PREFIX;  
        Resource getResource(String location);   
        ClassLoader getClassLoader();      
}  
    
public interface ResourcePatternResolver extends ResourceLoader {  
        String CLASSPATH_ALL_URL_PREFIX = "classpath*:";   
        Resource[] getResources(String locationPattern) throws IOException;     
} 

Pseudo URL prefix for all matching resources from the class path: "classpath*:" 
This differs from ResourceLoader's classpath URL prefix in that it retrieves all matching resources for a given name (e.g. "/beans.xml"), 
for example in the root of all deployed JAR files. 


classpath*:
retrieves all matching resources for a given name


ResourceLoader提供 classpath下单资源文件的载入,ResourcePatternResolver提供多资源文件的载入。


资源路径判断
是否以classpath*开头
是否包含通配符

以classpath开头,创建ClassPathResource
否则以URL的方式加载资源,创建UrlResource
路径包含通配符 递归处理


classpath*:/aaa/bbb/spring-*.xml
classpath*:/aaa/*/spring-*.xml


classpath:spring-servlet.xml
无通配符,必须完全匹配
 
classpath:spring-servlet?.xml
匹配一个字符,例如 spring-servlet1.xml  spring-servlet2.xml
 
classpath:config/*/spring-servlet.xml
匹配零个或多个字符串(只针对名称,不匹配目录分隔符等)
config/a/spring-servlet.xml  config/b/spring-servlet.xml  

classpath:config/**/spring-servlet.xml
匹配路径中的零个或多个目录,例如:config/a/ab/abc/spring-servlet.xml,同时也能匹配 config/spring-servlet.xml
 
classpath:**/*.xml
类路径中以.xml结尾的配置文件,但重复的文件名只加载其中一个,视加载顺序决定

 
classpath*:config/**/*.xml
classpath*:**/*.xml
classpath* 表示加载多个资源文件,重名也会被加载,包括jar包里的重复xml文件


 
ApplicationContext
Resource[] rs = ctx.getResources(pattern);


String res = "application.properties";
Enumeration urls = ResourceTest.class.getClassLoader().getResources(res);
while(urls.hasMoreElements()){
    System.out.println(urls.nextElement().toString());
}


file:/Users/dugang/fun/hellocode/project/springtest/target/classes/application.properties
jar:file:/Users/dugang/fun/hellocode/project/springtest/lib/my.jar!/application.properties


直接使用 PathMatchingResourcePatternResolver


import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

public class ResourceLoaderTest {

	public static void main(String[] args) throws Exception {
		System.out.println("ResourceLoaderTest test " + System.currentTimeMillis());

		PathMatchingResourcePatternResolver rpr = new PathMatchingResourcePatternResolver();
		String name = "classpath:application.properties";
		Resource res = rpr.getResource(name);

		System.out.println(res.getURL());

		try {
			name = "classpath*:application.properties";
			res = rpr.getResource(name);
			System.out.println(res.getURL());
		} catch (Exception e) {
			// java.io.FileNotFoundException: class path resource
			// [classpath*:application.properties]
			System.out.println(e);
		}

		name = "classpath*:application.properties";
		Resource[] resarr = rpr.getResources(name);
		for (Resource item : resarr) {
			System.out.println(item.getURL());
		}

	}

}


file:/Users/dugang/fun/hellocode/project/springtest/target/classes/application.properties
java.io.FileNotFoundException: class path resource [classpath*:application.properties] cannot be resolved to URL because it does not exist
file:/Users/dugang/fun/hellocode/project/springtest/target/classes/application.properties
jar:file:/Users/dugang/fun/hellocode/project/springtest/lib/my.jar!/application.properties



注意 使用   getResource("classpath*:application.properties")  加载不到资源 !!!

上一篇     下一篇
maven中央仓库及镜像设置

一条shell命令生成目录树结构

使用jar命令生成jar包

spring组件扫描

spring实战之javaconfig

jar包冲突的处理套路