重新加载由 getResourceAsStream 加载的资源 [英] Reloading resources loaded by getResourceAsStream

查看:31
本文介绍了重新加载由 getResourceAsStream 加载的资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

遵循最佳实践,我使用 Thread.currentThread().getContextClassLoader().getResourceAsStream 在 Web 应用程序中加载资源(如文本文件或 xml 文件),而不是通过文件 API.

Following best practices, I'm using Thread.currentThread().getContextClassLoader().getResourceAsStream to load resources in a web application (like text files or xml files), instead of going through the file API.

然而,这有一个缺点,如果磁盘上的资源发生变化,对 getResourceAsStream 的后续调用会无限期地返回旧版本.

However, this has the disadvantage that if the resource changes on disk, a following call to getResourceAsStream keeps returning the old version indefinitely.

不过,我希望它选择新版本.在我的调试器中,我看到在 classLoader 中有一个名为 resourceEntries 的简单 HashMap.使用反射,我已经能够删除特定条目,这似乎有效.

I would like it to pick up the new version though. In my debugger I see there's a simple HashMap called resourceEntries in the classLoader. Using reflection I've been able to remove a specific entry and this seems to work.

然而这种方法很脆弱.

有没有更标准的方法来做到这一点?

Is there a more standard way to do this?

推荐答案

除了 kschneid 的回答可能确实适用于 Tomcat 之外,我还想补充一点,对于 JBoss AS 5+,它似乎已经可以工作而无需任何特殊技巧.

In addition to kschneid's answer which might work for Tomcat indeed, I wanted to add that for JBoss AS 5+ it already seems to work without needing any special tricks.

资源缓存可能是特定于类加载器的.JBoss AS 要么不缓存,要么足够聪明,可以看到磁盘上的资源发生了变化.

Caching of resources is probably class loader specific. The JBoss AS one either doesn't cache or is smart enough to see that the resource on disk has changed.

这篇关于重新加载由 getResourceAsStream 加载的资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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