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?
The java. lang. Class. getResourceAsStream() finds a resource with a given name.It returns a InputStream object or null if no resource with this name is found.
Class. getResource can take a "relative" resource name, which is treated relative to the class's package. Alternatively you can specify an "absolute" resource name by using a leading slash. Classloader resource paths are always deemed to be absolute.
getClassLoader() is the same as the class loader for its element type; if the element type is a primitive type, then the array class has no class loader. Applications implement subclasses of ClassLoader in order to extend the manner in which the Java virtual machine dynamically loads classes.
The getResource() method of java Class class is used to return the resources of the module in which this class exists. The value returned from this function exists in the form of the object of the URL class.
To avoid caching you can use:
getClass().getClassLoader().getResource().openStream()
It would be equal to using URLResourceLoader
for Velocity instead of ClasspathResourceLoader
I suppose. 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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With