[英]Why do I get this error with Velocity “Velocity is not initialized correctly.”?
我在擴展AbstractProcessor的注釋處理器中初始化速度引擎,如下所示:
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv)
{
String fqClassName = null;
String className = null;
String packageName = null;
Map<String, VariableElement> fields = new HashMap<String, VariableElement>();
Map<String, ExecutableElement> methods = new HashMap<String, ExecutableElement>();
...
if (fqClassName != null)
{
Properties props = new Properties();
URL url = this.getClass().getClassLoader().getResource("velocity.properties");
VelocityContext velocityContext = null;
Template template = null;
JavaFileObject javaFileObject;
Writer writer;
try
{
processingEnv.getMessager().printMessage(Kind.NOTE, "Before");
props.load(url.openStream());
processingEnv.getMessager().printMessage(Kind.NOTE, "Props: " + props);
VelocityEngine velocityEngine = new VelocityEngine();
velocityEngine.setProperty("resource.loader", "classpath");
velocityEngine.init();
...
} catch (Exception e1)
{
processingEnv.getMessager().printMessage(Kind.ERROR,
"Exception: " + e1.getMessage() + " : " + e1.getStackTrace());
}
}
return false;
}
調用init()
方法時會發生異常。
我將帶有Velocity jar文件的處理器導出為jar文件,並將eclipse配置為將其用作注釋處理器。
您正在將自定義屬性加載到props
變量中,但永遠不要將其傳遞給Velocity。
因此,您應該簡單地執行以下操作:
velocityEngine.init(props)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.