首页  

java资源加载方式比较     所属分类 java 浏览量 1503
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" }

上一篇     下一篇
Mysql锁机制

mysql连接信息查看

Java锁机制

java number 比较陷阱

手机4G信号突然变成2G信号

JVM性能分析利器 JMC 和 JFR