ClassLoader.getResource()与getResources()

1、getResource 与 getResources

加载类路径资源文件,Java通过类加载器提供便捷的方法,分别介绍

    public static void demo () throws IOException{
        // 当前类加载器
        ClassLoader loader = Test.class.getClassLoader();
        
        // getResource与getResources 都是【加载当前类加载器以及父类加载器所在路径的资源文件】
        
        /**
         * 1.getResource
         * 加载当前类加载器以及父类加载器所在路径的资源文件
         * 将遇到的第一个资源文件直接返回!!!
         * 比如当前工程类路径有conf/demo.properties文件,引入的第三方jar包也有这个文件
         * 返回的是当前工程下的这个资源文件
         */
        URL url = loader.getResource("conf/demo.properties");
        
        /**
         * 2.getResources 
         * 加载当前类加载器以及父类加载器所在路径的资源文件
         * 将遇到的所有资源文件全部返回!
         * 比如当前工程类路径有conf/demo.properties文件,引入的第三方jar包也有这个文件
         * 则将这些文件全部返回
         */
        Enumeration<URL> enumeration = loader.getResources("conf/demo.properties");
        // 打印出所有同名的资源文件
        while (enumeration.hasMoreElements()) {
            URL url1 = enumeration.nextElement();
            System.out.println("file=" + url1.getFile());
        }
        
        // 3.直接将资源转化成流InputStream
        InputStream in1 = loader.getResourceAsStream("conf/demo.properties");
        System.out.println(in1 == null);
        
        // 4.也可以根据获取的URL直接打开流
        InputStream in2 = url.openStream();
        System.out.println(in2 == null);
    }


2、源代码

   /**
     * getResource源代码:递归调用的
     */
    public URL getResource(String name) {
        URL url;
        // 初始parent 肯定是 ExtClassLoader,且不为空
        // 因为当前类加载器就是系统类加载器,即 AppClassLoader
        // 他的父亲就是 ExtClassLoader
        if (parent != null) {
            // 递归调用:一旦再次进入递归方法,parent肯定就变成BootClassLoader
            // 且一定为null,因为引导类加载器是C语言的native本地方法实现,在Java环境获取不到
            url = parent.getResource(name);
        } else {
            url = getBootstrapResource(name);
        }
        if (url == null) {
            url = findResource(name);
        }
        return url;
    }

    /**
     * getResources 的源代码:也是递归调用
     */
    public Enumeration<URL> getResources(String name) throws IOException {
        Enumeration[] tmp = new Enumeration[2];
        if (parent != null) {
            tmp[0] = parent.getResources(name);
        } else {
            tmp[0] = getBootstrapResources(name);
        }
        tmp[1] = findResources(name);

        return new CompoundEnumeration<>(tmp);
    }

3、Spring配置文件中常出现的 classpath 与 classpath*

这两者的区别,本质上就是 getResource 和 getResources 的区别,也有网友总结如下

         (1)classpath和classpath*都可以加载整个classpath下(包括jar包里面)的资源文件。
         (2)classpath只会返回第一个匹配的资源,查找路径是优先在项目中存在资源文件,再查找jar包。
         (3)文件名包含通配符(如spring-*.xml,spring*.xml), 如果根目录为"",classpath加载不到任何资源, 而classpath*则可以
         (4)classpath中可以匹配的目录中的资源,但是不能加载到jar包中的资源


4、读取Properties资源文件的工具类


明白了 getResource 和 getResources 的原理,再来读取各种资源文件就比较得心应手

此处使用了弱引用的WeakHashMap作为缓存,一定程度上避免重复加载资源文件


package com.yli.util.resouce;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.WeakHashMap;
import java.util.logging.Logger;

/**
 * 读取 properties 资源文件
 * 
 * @author yli
 */
public class PropertiesUtil {

    private static ClassLoader loader = PropertiesUtil.class.getClassLoader();
    private static WeakHashMap<String, Properties> cache = new WeakHashMap<String, Properties>();
    private static Logger log = Logger.getLogger(PropertiesUtil.class.getName());

    public static Properties getProperties(String file) {
        Properties p = cache.get(file);
        if (null != p) {
            return p;
        }
        try {
            InputStream inStream = loader.getResourceAsStream(file);
            p = new Properties();
            p.load(inStream);
            cache.put(file, p);
        } catch (IOException e) {
            log.warning(e.getMessage());
        }
        return p;
    }

    public static String get(String file, String key) {
        Properties p = getProperties(file);
        if (null == p) {
            log.warning("**** 读取资源文件失败 ****");
            return null;
        } else {
            return p.getProperty(key);
        }
    }
    
}




  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值