java Class.getResource和ClassLoader.getResource

本文详细解析了Java类加载器的工作原理及如何使用类加载器加载资源,包括Bootstrap、Extension、System类加载器的区别,以及如何通过Class.getResourceAsStream()和getClass().getClassLoader()方法加载不同路径的资源。
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的方式来获取。


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
                  
        
    }
}

    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;
    }







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值