API / Apache Velocity Interview questions
What is the role of the ResourceManager and ResourceLoader in Velocity?
ResourceLoader is the pluggable interface responsible for actually locating and reading a template's raw source, whether from the file system, the classpath, a JAR, a database, or a custom source you implement yourself. ResourceManager sits above it, coordinating one or more configured loaders and applying the caching policy so a resource isn't re-read and re-parsed unnecessarily.
Properties p = new Properties(); p.setProperty("resource.loader", "file, classpath"); p.setProperty("file.resource.loader.path", "/opt/app/templates"); p.setProperty("file.resource.loader.cache", "true"); p.setProperty("classpath.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); VelocityEngine engine = new VelocityEngine(p); engine.init();
Multiple loaders can be chained, as shown above, so the engine tries the file system first and falls back to the classpath. When a requested template isn't found by any configured loader, getTemplate() throws a ResourceNotFoundException.
More Related questions...