groovy 模板引擎实现原理分析
groovy的SimpleTemplateEngine实现了模板功能,类似于jsp。那就分析groovy是如何实现模板的。
使用模板
看看SimpleTemplateEngine类
这儿做了三件事
1、创建了一个SimpleTemplate对象
2、解析模板,主要是把<%=exp%>转为groovy的内置表达式${exp},把非<%code%>转为调用out.print(内容)函数,<%code%>中的就是groovy代码了。这样就把整个模板解析为一段代码。如
3、用groovyShell获取一个Script对象
Script对象只一个支持普通groovy对象,利用了Groovy的特性
实现 getProperty(String property)方法,从参数绑定对象中获取属性,这样脚本中就能获取绑定参数。private static class SimpleTemplate implements Template { protected Script script; public Writable make() { return make(null); } public Writable make(final Map map) { return new Writable() { /** * Write the template document with the set binding applied to the writer. * * @see groovy.lang.Writable#writeTo(java.io.Writer) */ public Writer writeTo(Writer writer) { Binding binding; if (map == null) binding = new Binding(); else binding = new Binding(map); Script scriptObject = InvokerHelper.createScript(script.getClass(), binding); PrintWriter pw = new PrintWriter(writer); scriptObject.setProperty("out", pw); scriptObject.run(); pw.flush(); return writer; } /** * Convert the template and binding into a result String. * * @see java.lang.Object#toString() */ public String toString() { StringWriter sw = new StringWriter(); writeTo(sw); return sw.toString(); } }; }}
很清楚了,调用make方法,创建一个Script对象,绑定参数binding = new Binding(map)。
创建一个PrintWriter,绑定为out参数,而模板解析的代码中的out.print(内容)就有着落了。
所以:
Groovy的模板是通过编译,生成Java类,然后调用方法实现的使用模板机制注意要缓存Script对象或Template对象,否则每次调用都会编译生成一个新的Java类,导致内存溢出/泄露