API / Apache Velocity Interview questions
Describe the role of the VelocityEngine class?
VelocityEngine is the main entry point for embedding Velocity in a Java application. You configure it with properties (resource loader paths, logging, macro libraries), initialize it once, then use it repeatedly to look up and render templates.
VelocityEngine engine = new VelocityEngine(); engine.setProperty("resource.loader", "file"); engine.setProperty("file.resource.loader.path", "/templates"); engine.init(); Template template = engine.getTemplate("welcome.vm"); StringWriter sw = new StringWriter(); template.merge(context, sw);
Internally it wraps a RuntimeInstance, which holds the parser pool, the directive registry, and the resource cache. Older code sometimes uses the static Velocity singleton class instead, which wraps a single shared engine instance, but constructing your own VelocityEngine is preferred when you need multiple independently configured instances.
More Related questions...