Class.getResource()和ClassLoader.getResource()有什么区别? [英] What is the difference between Class.getResource() and ClassLoader.getResource()?

查看:134
本文介绍了Class.getResource()和ClassLoader.getResource()有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道 Class.getResource() ClassLoader.getResource()之间有什么区别?

I wonder what the difference is between Class.getResource() and ClassLoader.getResource()?

编辑:我特别想知道文件/目录级别是否涉及任何缓存。如是否在类版本中缓存目录列表?

AFAIK以下内容基本上应该相同,但它们不是:

AFAIK the following should essentially do the same, but they are not:

getClass().getResource() 
getClass().getClassLoader().getResource()

在摆弄一些在 WEB-INF / classes / <中创建新文件的报告生成代码时,我发现了这一点/ code>来自该目录中的现有文件。当使用Class中的方法时,我可以使用 getClass()。getResource()找到部署时的文件,但是当我尝试获取新创建的文件时,我收到了一个null对象。浏览目录清楚地显示新文件在那里。文件名前面带有正斜杠,如/myFile.txt。

I discovered this when fiddling with some report generation code that creates a new file in WEB-INF/classes/ from an existing file in that directory. When using the method from Class, I could find files that were there at deployment using getClass().getResource(), but when trying to fetch the newly created file, I recieved a null object. Browsing the directory clearly shows that the new file is there. The filenames were prepended with a forward slash as in "/myFile.txt".

ClassLoader 版本 getResource()另一方面确实找到了生成的文件。根据这种经验,似乎存在某种目录列表的缓存。我是对的,如果是的话,这会记录在哪里?

The ClassLoader version of getResource() on the other hand did find the generated file. From this experience it seems that there is some kind of caching of the directory listing going on. Am I right, and if so, where is this documented?

来自上的/docs/api/java/lang/Class.html#getResource%28java.lang.String%29\">API docs Class.getResource()

From the API docs on Class.getResource()


查找具有给定名称的资源

搜索与
给定类相关联的资源的规则由
定义类的类加载器实现。
此方法委托给该对象的
类加载器。如果此对象是由bootstrap类加载器加载的

方法委托给
ClassLoader.getSystemResource(java.lang.String)。

Finds a resource with a given name. The rules for searching resources associated with a given class are implemented by the defining class loader of the class. This method delegates to this object's class loader. If this object was loaded by the bootstrap class loader, the method delegates to ClassLoader.getSystemResource(java.lang.String).

对我来说,这是Class.getResource真正调用它自己的类加载器的getResource()。这与执行 getClass()。getClassLoader()。getResource()相同。但显然不是。有人可以向我提供这方面的一些启示吗?

To me, this reads "Class.getResource is really calling its own classloader's getResource()". Which would be the same as doing getClass().getClassLoader().getResource(). But it is obviously not. Could someone please provide me with some illumination into this matter?

推荐答案

回答是否有任何缓存的问题。

To answer the question whether there is any caching going on.

我通过运行一个独立的Java应用程序进一步调查了这一点,该应用程序使用getResourceAsStream ClassLoader方法从磁盘连续加载文件。我能够编辑文件,并立即反映更改,即文件是在没有缓存的情况下从磁盘重新加载的。

I investigated this point further by running a stand-alone Java application that continuously loaded a file from disk using the getResourceAsStream ClassLoader method. I was able to edit the file, and the changes were reflected immediately, i.e., the file was reloaded from disk without caching.

但是:
我正在开发一个项目,其中包含几个相互依赖的maven模块和Web项目。我正在使用IntelliJ作为我的IDE来编译和运行Web项目。

However: I'm working on a project with several maven modules and web projects that have dependencies on each other. I'm using IntelliJ as my IDE to compile and run the web projects.

我注意到上面的内容似乎不再适用,原因是该文件是我正被加载现在被烘焙到一个罐子里并部署到依赖的web项目。尝试更改目标文件夹中的文件后,我才注意到这一点,但无济于事。这使得好像有缓存正在进行。

I noticed that the above seemed to no longer hold true, the reason being that the file that I was being loaded is now baked into a jar and deployed to the depending web project. I only noticed this after trying to change the file in my target folder, to no avail. This made it seem as though there was caching going on.

这篇关于Class.getResource()和ClassLoader.getResource()有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆