码迷,mamicode.com
首页 > 编程语言 > 详细

java资源加载方式比较

时间:2020-03-24 01:12:28      阅读:82      评论:0      收藏:0      [点我收藏+]

标签:web   使用   loader   ext   classes   resource   方式   library   情况   

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

java资源加载方式比较

标签:web   使用   loader   ext   classes   resource   方式   library   情况   

原文地址:https://www.cnblogs.com/daomeidan/p/12556312.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!