简体   繁体   English

getClass()。getClassLoader()。getResourceAsStream()正在缓存资源

[英]getClass().getClassLoader().getResourceAsStream() is caching the resource

I have a resource (velocity template) which I'd like to be able to swap during development. 我有一个资源(速度模板),我希望能够在开发过程中交换。 However, 然而,

getClass().getClassLoader().getResourceAsStream() 

seems to cache the template. 似乎缓存模板。 Is there a way to disable this besides using a file loader instead of the class loader? 除了使用文件加载器而不是类加载器之外,有没有办法禁用它?

To avoid caching you can use: 为避免缓存,您可以使用:

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

It would be equal to using URLResourceLoader for Velocity instead of ClasspathResourceLoader I suppose. 它等于使用Velocity的URLResourceLoader而不是我想的ClasspathResourceLoader I would just go with a file loader. 我会选择一个文件加载器。

See if something like this helps (exception handling omitted): 看看这样的东西是否有帮助(省略了异常处理):

URL res = getClass().getClassLoader().getResource(resName);
if (res != null) {
    URLConnection resConn = res.openConnection();
    resConn.setUseCaches(false);
    InputStream in = resConn.getInputStream();
}

Another thing to watch out for (besides the caching mentioned in the other answers) is that your IDE or build system might move your resources to your build directory and put that on the class path. 另外需要注意的是(除了在其他答案中提到的缓存),您的IDE或构建系统可能会将您的资源移动到您的构建目录并将其放在类路径上。 So the file you are editing in your source directory is not the file that is being served. 因此,您在源目录中编辑的文件不是正在提供的文件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 getClass().getClassLoader().getResourceAsStream() 总是返回 null,资源在 application.jar 中 - getClass().getClassLoader().getResourceAsStream() always returns null, resource is in application.jar getClass()。getClassLoader()。getResourceAsStream引发NullPointerException - getClass().getClassLoader().getResourceAsStream is throwing a NullPointerException java jar getClass().getClassLoader().getResourceAsStream 返回空指针异常 - java jar getClass().getClassLoader().getResourceAsStream return nullpointerexception Java OutputStream 等价于 getClass().getClassLoader().getResourceAsStream() - Java OutputStream equivalent to getClass().getClassLoader().getResourceAsStream() this.getClass()。getClassLoader()。getResourceAsStream始终返回null - this.getClass().getClassLoader().getResourceAsStream always returning null this.getClass()。getClassLoader()。getResourceAsStream()在RCP产品中不起作用 - this.getClass().getClassLoader().getResourceAsStream()is not working in RCP product 如何将getClass.getClassLoader()。getResourceAsStream()值传递给FileOutputStream - How to pass getClass.getClassLoader().getResourceAsStream() value to FileOutputStream getClassLoader().getResourceAsStream(resource) 在 java 中返回 null 11 - getClassLoader().getResourceAsStream(resource) return null in java 11 Java getClassLoader()。getResourceAsStream(文件名) - Java getClassLoader().getResourceAsStream(filename) this.getClass()。getClassLoader()和ClassLoader - this.getClass().getClassLoader() and ClassLoader
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM