java Class.getResource和ClassLoader.getResource

来源:互联网 发布:网络搬砖项目 编辑:程序博客网 时间:2024/05/17 03:09
Class.getResourceAsStream() 会指定要加载的资源路径与当前类所在包的路径一致。
例如你写了一个MyTest类在包com.test.mycode 下,那么MyTest.class.getResourceAsStream("name") 会在com.test.mycode包下查找相应的资源。
如果这个name是以 '/' 开头的,那么就会从classpath的根路径下开始查找。

ClassLoader.getResourceAsStream()  无论要查找的资源前面是否带'/' 都会从classpath的根路径下查找。

所以: 
MyTest.getClassLoader().getResourceAsStream("name") 和 

MyTest.getClassLoader().getResourceAsStream("/name") 的效果是一样的。
                                                                                  
顺便提下JAVA中类的加载器:

一共有三种加载器

bootstrap classloader :负责加载JAVA核心类( jre 下lib和class目录中的内容)

extension classloader :负责加载JAVA扩展类(jre 下lib/ext 目录中的内容)

system classloader :负责加载应用指定的类 (环境变量classpath中配置的内容)

一个类的加载顺序也是按上面的排列来的,这样就能保证系统的类能先加载。 

与此同时用户也可以自己定义ClassLoader,用来加载特殊的资源。

这里就涉及到 Class.getClassLoader()  和  Thread.currentThread.getContextClassLoader()的区别。

举一个简单的例子:

   假如某天JAVA给我们提供了一个叫 StartCamera 的类用来启动电脑的标准摄像头,并将这个类打包在一个jar中。

   正常情况下,我们要启动摄像头时只需将这个jar配置到classpath中。系统启动时system classloader会将这个类加载到应用中。

   但因为摄像头的生产厂家不一样,针对新的设备会有多个不同的StartCamera实现,在应用中我们不知道实际的用户会用到哪种。于是我们就自定义了一个ClassLoader,用来针对具体的设备类型加载相应的StartCamera类。

   这样一来就出现:优先加载我们定义的类,加载不到的情况下再加载系统的。 这样的需求,是系统默认的父委托加载机制无法满足的。

   Thread.currentThread.getContextClassLoader() 就是这样产生的。 我们使用Thread.currentThread.setContextClassLoader() 可以为当前线程指定相应的ClassLoader,然后用get的方式来获取。

[java] view plain copy
  1. package com.jiepu.docbuildermanager;  
  2.   
  3. //http://www.cnblogs.com/yejg1212/p/3270152.html  
  4. public class TestMain {  
  5.       
  6.     //记得URLDecoder.decode(url.getPath(), "utf-8");  
  7.     public static void main(String[] args) {  
  8.           
  9.           
  10.         // 当前类(class)所在的包目录  
  11.         System.out.println(TestMain.class.getResource(""));  
  12.         // class path根目录  
  13.         System.out.println(TestMain.class.getResource("/"));  
  14.           
  15.         // TestMain.class在<bin>/testpackage包中  
  16.         // 2.properties  在<bin>/testpackage包中  
  17.         System.out.println(TestMain.class.getResource("2.properties"));  
  18.           
  19.         // TestMain.class在<bin>/testpackage包中  
  20.         // 3.properties  在<bin>/testpackage.subpackage包中  
  21.         System.out.println(TestMain.class.getResource("subpackage/3.properties"));  
  22.           
  23.         // TestMain.class在<bin>/testpackage包中  
  24.         // 1.properties  在bin目录(class根目录)  
  25.         System.out.println(TestMain.class.getResource("/1.properties"));  
  26.   
  27.           
  28.         TestMain t = new TestMain();  
  29.         System.out.println(t.getClass());  
  30.         System.out.println(t.getClass().getClassLoader());  
  31.         System.out.println(t.getClass().getClassLoader().getResource(""));  
  32.         System.out.println(t.getClass().getClassLoader().getResource("/"));//null  
  33.                     
  34.           
  35.     }  
  36. }  

[java] view plain copy
  1. public static String getProjectPath() throws Exception {  
  2.   
  3.     URL url = GlobalTool.class.getProtectionDomain().getCodeSource()  
  4.             .getLocation();  
  5.     String filePath = URLDecoder.decode(url.getPath(), "utf-8");  
  6.     if (filePath.endsWith(".jar")) {  
  7.         filePath = filePath.substring(0, filePath.lastIndexOf("/") + 1);  
  8.     }  
  9.     File file = new File(filePath);  
  10.     filePath = file.getAbsolutePath();  
  11.     //System.out.println(ClassLoader.getSystemClassLoader().getResource(""));  
  12.     return filePath;  
  13. }  
  14.   
  15. //http://www.cnblogs.com/yejg1212/p/3270152.html     
  16. public static String getRealPathByClass() throws Exception {  
  17.     //GlobalTool.class.getResource("/")  
  18.     String realPath = Thread.currentThread().getClass().getResource("/")  
  19.             .getPath();  
  20.   
  21.     realPath = URLDecoder.decode(realPath, "utf-8");  
  22.     System.out.println(realPath);  
  23.     return realPath;  
  24. }  
  25.   
  26. public static String getRealPathByClassLoader() {  
  27.     // Thread.currentThread().getClass().getClassLoader().getResource("")=null  
  28.     String realPath = GlobalTool.class.getClassLoader().getResource("")  
  29.             .getPath();  
  30.     try {  
  31.         realPath = URLDecoder.decode(realPath, "utf-8");  
  32.         System.out.println(realPath);  
  33.         if (realPath.endsWith("!/config.properties")) {  
  34.             realPath = realPath.substring(0, realPath.lastIndexOf("!/config.properties"));  
  35.         }  
  36.         if (realPath.startsWith("file:/")) {  
  37.             realPath = realPath.substring("file:/".length());  
  38.         }  
  39.     } catch (Exception e) {  
  40.         e.printStackTrace();  
  41.     }  
  42.     return realPath;  

原创粉丝点击