[英]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.