zoukankan      html  css  js  c++  java
  • java资源加载方式比较

    String url = "xxx";
    Map map = new HashMap();
    URL u = ClassLoader.getSystemResource(url);
    map.put("ClassLoader.getSystemResource",u);
    		
    u = WebUtil.class.getResource(url);
    map.put("WebUtil.class.getResource",u);
    		
    u = WebUtil.class.getClassLoader().getResource(url);
    map.put("WebUtil.class.getClassLoader().getResource",u);
    		
    u = Thread.currentThread().getContextClassLoader().getResource(url);
    map.put("Thread.currentThread().getContextClassLoader().getResource",u);
    
    
    推荐使用  
    Thread.currentThread().getContextClassLoader().getResource
    url 不要 以 / 开头 
    使用 ContextClassLoader  都能加载到资源  (除了 /java/lang/String.class )
    
    ContextClassLoader  上下文加载器   web应用等(自定义类加载器) 加载资源
    
    ClassLoader.getSystemResource 加载不到 web应用加载器加载的资源

    各种资源路径加载情况
    
    
    
    log4j.properties
    {
        "WebUtil.class.getClassLoader().getResource": "file:/Users/dugang/fun/hellocode/project/benchmark/target/classes/log4j.properties",
        "Thread.currentThread().getContextClassLoader().getResource": "file:/Users/dugang/fun/hellocode/project/benchmark/target/classes/log4j.properties"
    }
    
    /log4j.properties
    {
        "WebUtil.class.getClassLoader().getResource": "file:/Users/dugang/fun/hellocode/project/benchmark/target/classes/log4j.properties",
        "WebUtil.class.getResource": "file:/Users/dugang/fun/hellocode/project/benchmark/target/classes/log4j.properties",
        "Thread.currentThread().getContextClassLoader().getResource": "file:/Users/dugang/fun/hellocode/project/benchmark/target/classes/log4j.properties"
    }
    	
    
    java/lang/String.class
    {
        "WebUtil.class.getClassLoader().getResource": "jar:file:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/rt.jar!/java/lang/String.class",
        "Thread.currentThread().getContextClassLoader().getResource": "jar:file:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/rt.jar!/java/lang/String.class",
        "ClassLoader.getSystemResource": "jar:file:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/rt.jar!/java/lang/String.class"
    }
    
    /java/lang/String.class
    {
        "WebUtil.class.getResource": "jar:file:/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/rt.jar!/java/lang/String.class"
    }
    
    com/google/common/math/DoubleUtils.class
    {
        "WebUtil.class.getClassLoader().getResource": "jar:file:/Users/dugang/m2/com/google/guava/guava/16.0.1/guava-16.0.1.jar!/com/google/common/math/DoubleUtils.class",
        "Thread.currentThread().getContextClassLoader().getResource": "jar:file:/Users/dugang/m2/com/google/guava/guava/16.0.1/guava-16.0.1.jar!/com/google/common/math/DoubleUtils.class"
    }
    
    /com/google/common/math/DoubleUtils.class
    {
        "WebUtil.class.getClassLoader().getResource": "jar:file:/Users/dugang/m2/com/google/guava/guava/16.0.1/guava-16.0.1.jar!/com/google/common/math/DoubleUtils.class",
        "WebUtil.class.getResource": "jar:file:/Users/dugang/m2/com/google/guava/guava/16.0.1/guava-16.0.1.jar!/com/google/common/math/DoubleUtils.class",
        "Thread.currentThread().getContextClassLoader().getResource": "jar:file:/Users/dugang/m2/com/google/guava/guava/16.0.1/guava-16.0.1.jar!/com/google/common/math/DoubleUtils.class"
    }
    
    dyyx/util/WebUtil.class
    {
        "WebUtil.class.getClassLoader().getResource": "file:/Users/dugang/fun/hellocode/project/benchmark/target/classes/dyyx/util/WebUtil.class",
        "Thread.currentThread().getContextClassLoader().getResource": "file:/Users/dugang/fun/hellocode/project/benchmark/target/classes/dyyx/util/WebUtil.class"
    }
    
    
    /dyyx/util/WebUtil.class
        "WebUtil.class.getClassLoader().getResource": "file:/Users/dugang/fun/hellocode/project/benchmark/target/classes/dyyx/util/WebUtil.class",
        "WebUtil.class.getResource": "file:/Users/dugang/fun/hellocode/project/benchmark/target/classes/dyyx/util/WebUtil.class",
        "Thread.currentThread().getContextClassLoader().getResource": "file:/Users/dugang/fun/hellocode/project/benchmark/target/classes/dyyx/util/WebUtil.class"
    }
  • 相关阅读:
    互斥锁Mutex与信号量Semaphore的区别
    c/c++强制类型转换
    c++中的隐藏、重载、覆盖(重写)
    运算符重载详解
    类的大小
    C++ static、const和static const 以及它们的初始化
    一种隐蔽性较高的Java ConcurrentModificationException异常场景
    Java编码常见的Log日志打印问题
    Java编程常见缺陷汇总(一)
    Java字符串连接的多种实现方法及效率对比
  • 原文地址:https://www.cnblogs.com/daomeidan/p/12556312.html
Copyright © 2011-2022 走看看