freemarker的配置简介

栏目:FreeMarker框架 作者:admin 日期:2015-05-19 评论:0 点击: 2,221 次

在freemarker启动的过程中,参与配置功能的类主要有四个:Configurable,Configuration,Template和Environment。下面给大家简单介绍一下这里类的特点,内容主要是出自freemarker的源码,而且这些英文比较简单,稍微耐心一点就可以读懂的。

Configurable简介

This is a common superclass of {@link freemarker.template.Configuration},{@link freemarker.template.Template}, and {@link Environment} classes.
It provides settings that are common to each of them. FreeMarker uses a three-level setting hierarchy - the return value of every setting getter method on <code>Configurable</code> objects inherits its value from its parent <code>Configurable</code> object, unless explicitly overridden by a call to a corresponding setter method on the object itself. The parent of an
<code>Environment</code> object is a <code>Template</code> object, the parent of a <code>Template</code> object is a <code>Configuration</code> object.

Configuration简介

The main entry point into the FreeMarker API; encapsulates the configuration settings of FreeMarker, also serves as a central template-loading and caching service.
This class is meant to be used in a singleton pattern. That is, you create an instance of this at the beginning of the application life-cycle, set its {@link #setSetting(String, String) configuration settings} there (either with the setter methods like {@link #setTemplateLoader(TemplateLoader)} or by loading a {@code .properties} file), and then use that single instance everywhere in your application. Frequently re-creating {@link Configuration} is a typical and grave mistake from performance standpoint, as the {@link Configuration} holds the template cache, and often also the class introspection cache, which then will be lost. (Note that, naturally,having multiple long-lived instances,like one per component that internally uses FreeMarker is fine.)  
The basic usage pattern is like:
// Where the application is initialized; in general you do this ONLY ONCE in the application life-cycle!
Configuration cfg = new Configuration(VERSION_X_Y_Z));
// Where X, Y, Z enables the not-100%-backward-compatible fixes introduced in
// FreeMarker version X.Y.Z  and earlier (see {@link #Configuration(Version)}).
cfg.setSomeSetting(...);
cfg.setOtherSetting(...);

Template简介

Stores an already parsed template, ready to be processed (rendered) for unlimited times, possibly from multiple threads.
Typically, you will use {@link Configuration#getTemplate(String)} to create/get {@link Template} objects, so you don't construct them directly. But you can also construct a template from a {@link Reader} or a {@link String} that contains the template source code. But then it's important to know that while the resulting {@link Template} is efficient for later processing, creating a new {@link Template} itself is relatively expensive. So try to re-use {@link Template} objects if possible.{@link Configuration#getTemplate(String)} does that (caching {@link Template}-s) for you, but the constructor of course doesn't, so it's up to you to solve then.
Objects of this class meant to be handled as immutable and thus thread-safe. However, it has some setter methods for changing FreeMarker settings. Those must not be used while the template is being processed, or if the template object is already accessible from multiple threads.

Environment简介

Object that represents the runtime environment during template processing. For every invocation of a <tt>Template.process()</tt> method, a new instance
of this object is created, and then discarded when <tt>process()</tt> returns.
This object stores the set of temporary variables created by the template,the value of settings set by the template, the reference to the data model root,etc. Everything that is needed to fulfill the template processing job.
Data models that need to access the Environment object that represents the template processing on the current thread can use the {@link #getCurrentEnvironment()} method.
If you need to modify or read this object before or after the process call, use {@link Template#createProcessingEnvironment(Object rootMap, Writer out, ObjectWrapper wrapper)}

声明: 本文由金丝燕网原创编译,转载请保留链接: freemarker的配置简介,更多交流请加群:278721352

freemarker的配置简介:等您坐沙发呢!

发表评论