java Class.getResource和ClassLoader.getResource

xiaoxiao2021-02-27  177

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 package com.jiepu.docbuildermanager;    //http://www.cnblogs.com/yejg1212/p/3270152.html  public class TestMain {            //记得URLDecoder.decode(url.getPath(), "utf-8");      public static void main(String[] args) {                              // 当前类(class)所在的包目录          System.out.println(TestMain.class.getResource(""));          // class path根目录          System.out.println(TestMain.class.getResource("/"));                    // TestMain.class在<bin>/testpackage包中          // 2.properties  在<bin>/testpackage包中          System.out.println(TestMain.class.getResource("2.properties"));                    // TestMain.class在<bin>/testpackage包中          // 3.properties  在<bin>/testpackage.subpackage包中          System.out.println(TestMain.class.getResource("subpackage/3.properties"));                    // TestMain.class在<bin>/testpackage包中          // 1.properties  在bin目录(class根目录)          System.out.println(TestMain.class.getResource("/1.properties"));                      TestMain t = new TestMain();          System.out.println(t.getClass());          System.out.println(t.getClass().getClassLoader());          System.out.println(t.getClass().getClassLoader().getResource(""));          System.out.println(t.getClass().getClassLoader().getResource("/"));//null                                    }  }   [java] view plain copy public static String getProjectPath() throws Exception {        URL url = GlobalTool.class.getProtectionDomain().getCodeSource()              .getLocation();      String filePath = URLDecoder.decode(url.getPath(), "utf-8");      if (filePath.endsWith(".jar")) {          filePath = filePath.substring(0, filePath.lastIndexOf("/") + 1);      }      File file = new File(filePath);      filePath = file.getAbsolutePath();      //System.out.println(ClassLoader.getSystemClassLoader().getResource(""));      return filePath;  }    //http://www.cnblogs.com/yejg1212/p/3270152.html     public static String getRealPathByClass() throws Exception {      //GlobalTool.class.getResource("/")      String realPath = Thread.currentThread().getClass().getResource("/")              .getPath();        realPath = URLDecoder.decode(realPath, "utf-8");      System.out.println(realPath);      return realPath;  }    public static String getRealPathByClassLoader() {      // Thread.currentThread().getClass().getClassLoader().getResource("")=null      String realPath = GlobalTool.class.getClassLoader().getResource("")              .getPath();      try {          realPath = URLDecoder.decode(realPath, "utf-8");          System.out.println(realPath);          if (realPath.endsWith("!/config.properties")) {              realPath = realPath.substring(0, realPath.lastIndexOf("!/config.properties"));          }          if (realPath.startsWith("file:/")) {              realPath = realPath.substring("file:/".length());          }      } catch (Exception e) {          e.printStackTrace();      }      return realPath;  } 
转载请注明原文地址: https://www.6miu.com/read-15489.html

最新回复(0)