天天看点

java resource ClassPathResource

spring 中定义了资源接口,部分类关系如下:

java resource ClassPathResource

这个类包含加载的path 和类加载器classloader  还有class<?>  用来加载path 的类对象  三个属性

1 构造函数:初始化 path 和classloader 

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());
}      
public static ClassLoader getDefaultClassLoader() {
   ClassLoader cl = null;
   try {
      cl = Thread.currentThread().getContextClassLoader();
   }
   catch (Throwable ex) {
      // Cannot access thread context ClassLoader - falling back...
   }
   if (cl == null) {
      // No thread context class loader -> use class loader of this class.
      cl = ClassUtils.class.getClassLoader();
      if (cl == null) {
         // getClassLoader() returning null indicates the bootstrap ClassLoader
         try {
            cl = ClassLoader.getSystemClassLoader();
         }
         catch (Throwable ex) {
            // Cannot access system ClassLoader - oh well, maybe the caller can live with null...
         }
      }
   }
   return cl;
}      

2 getClassLoader 实现:

public final ClassLoader getClassLoader() {
   return (this.clazz != null ? this.clazz.getClassLoader() : this.classLoader);
}      

3 exists 实现逻辑:

public boolean exists() {
   return (resolveURL() != null);
}      
protected URL resolveURL() {
   if (this.clazz != null) {
      return this.clazz.getResource(this.path);
   }
   else if (this.classLoader != null) {
      return this.classLoader.getResource(this.path);
   }
   else {
      return ClassLoader.getSystemResource(this.path);
   }
}      
Class.getResource 实现:
public java.net.URL getResource(String name) {
    name = resolveName(name);
    ClassLoader cl = getClassLoader0();
    if (cl==null) {
        // A system class.
        return ClassLoader.getSystemResource(name);
    }
    return cl.getResource(name);
}      

ClassLoader.getResource 实现:

public URL getResource(String name) {
    URL url;
    if (parent != null) {
        url = parent.getResource(name);
    } else {
        url = getBootstrapResource(name);
    }
    if (url == null) {
        url = findResource(name);
    }
    return url;
}      
public static URL getSystemResource(String name) {
    ClassLoader system = getSystemClassLoader();
    if (system == null) {
        return getBootstrapResource(name);
    }
    return system.getResource(name);
}      

4 getInputStream 实现

public InputStream getInputStream() throws IOException {
   InputStream is;
   if (this.clazz != null) {
      is = this.clazz.getResourceAsStream(this.path);
   }
   else if (this.classLoader != null) {
      is = this.classLoader.getResourceAsStream(this.path);
   }
   else {
      is = ClassLoader.getSystemResourceAsStream(this.path);
   }
   if (is == null) {
      throw new FileNotFoundException(getDescription() + " cannot be opened because it does not exist");
   }
   return is;
}      

public URL getURL() throws IOException {
   URL url = resolveURL();
   if (url == null) {
      throw new FileNotFoundException(getDescription() + " cannot be resolved to URL because it does not exist");
   }
   return url;
}
      

public Resource createRelative(String relativePath) {
   String pathToUse = StringUtils.applyRelativePath(this.path, relativePath);
   return (this.clazz != null ? new ClassPathResource(pathToUse, this.clazz) :
         new ClassPathResource(pathToUse, this.classLoader));
}
      

继续阅读