本文介绍了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/ 中创建了一个新文件.使用 Class 中的方法时,我可以使用 getClass().getResource() 找到部署时存在的文件,但是在尝试获取新创建的文件时,我收到了一个空对象.浏览目录清楚地表明新文件在那里.文件名前面带有正斜杠,如/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".

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

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?

来自 APIClass.getResource()

查找资源有一个名字.的规则搜索与某个关联的资源给定的类由定义类的类加载器.这个方法委托给这个对象的类加载器.如果这个对象是由引导类加载器加载,该方法委托给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.

我注意到上面的内容似乎不再成立,原因是我正在加载的文件现在被烘焙到一个 jar 中并部署到依赖的 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() 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!