Resource
 
 定义
 
 资源文件来自于classPath或者本地文件。
 
 
 
 
 
  /** * Interface for a resource descriptor that abstracts from the actual * type of underlying resource, such as a file or class path resource. * * <p>An InputStream can be opened for every resource if it exists in * physical form, but a URL or File handle can just be returned for * certain resources. The actual behavior is implementation-specific. * 
 
 基本方法
 
  基本方法的单元测试以及输出
 
 
  
  
   
/** * org.springframework.core.io.Resource test * Created by jinglongjun on 2017/3/30. */public class TestResource {    @Test    public void testResource() throws IOException {        Resource resource = new ClassPathResource("spring.xml");        System.out.println(resource.exists());        System.out.println(resource.getDescription());        System.out.println(resource.getURL().getPath());    }} 
  
 
  输出结果:
  
 
 
  
  
   
trueclass path resource [spring.xml]/Users/jinglongjun/programe/workspace/spring/target/test-classes/spring.xml 
  
 
   
  
 
 Resouce继承关系
 
 
 
  
  
 
  ClassPathResource
 
  通过给定的ClassLoader或者ClassPath来加载资源。
 
 
  
  
   
/**	 * Create a new {@code ClassPathResource} for {@code ClassLoader} usage.	 * A leading slash will be removed, as the ClassLoader resource access	 * methods will not accept it.	 * @param path the absolute path within the classpath	 * @param classLoader the class loader to load the resource with,	 * or {@code null} for the thread context class loader	 * @see ClassLoader#getResourceAsStream(String)	 */	public ClassPathResource(String path, ClassLoader classLoader) {		Assert.notNull(path, "Path must not be null");		String pathToUse = StringUtils.cleanPath(path);		if (pathToUse.startsWith("/")) {			pathToUse = pathToUse.substring(1);		}		this.path = pathToUse;		this.classLoader = (classLoader != null ? classLoader : ClassUtils.getDefaultClassLoader());	} 
  
 
   
  
 
 
  
                
        
    
                    转载请注明原文地址: https://ju.6miu.com/read-3101.html