public class FreeMarkerConfigurationFactory
extends java.lang.Object
The optional "configLocation" property sets the location of a FreeMarker
properties file, within the current application. FreeMarker properties can be
overridden via "freemarkerSettings". All of these properties will be set by
calling FreeMarker's Configuration.setSettings()
method and are
subject to constraints set by FreeMarker.
The "freemarkerVariables" property can be used to specify a Map of
shared variables that will be applied to the Configuration via the
setAllSharedVariables()
method. Like setSettings()
,
these entries are subject to FreeMarker constraints.
The simplest way to use this class is to specify a "templateLoaderPath"; FreeMarker does not need any further configuration then.
Note: Spring's FreeMarker support requires FreeMarker 2.3 or higher.
setConfigLocation(org.springframework.core.io.Resource)
,
setFreemarkerSettings(java.util.Properties)
,
setFreemarkerVariables(java.util.Map<java.lang.String, java.lang.Object>)
,
setTemplateLoaderPath(java.lang.String)
,
createConfiguration()
,
FreeMarkerConfigurationFactoryBean
,
FreeMarkerConfigurer
,
freemarker.template.Configuration
Modifier and Type | Field and Description |
---|---|
private Resource |
configLocation |
private java.lang.String |
defaultEncoding |
private java.util.Properties |
freemarkerSettings |
private java.util.Map<java.lang.String,java.lang.Object> |
freemarkerVariables |
protected Log |
logger |
private java.util.List<TemplateLoader> |
postTemplateLoaders |
private boolean |
preferFileSystemAccess |
private java.util.List<TemplateLoader> |
preTemplateLoaders |
private ResourceLoader |
resourceLoader |
private java.lang.String[] |
templateLoaderPaths |
private java.util.List<TemplateLoader> |
templateLoaders |
Constructor and Description |
---|
FreeMarkerConfigurationFactory() |
Modifier and Type | Method and Description |
---|---|
Configuration |
createConfiguration()
Prepare the FreeMarker Configuration and return it.
|
protected TemplateLoader |
getAggregateTemplateLoader(java.util.List<TemplateLoader> templateLoaders)
Return a TemplateLoader based on the given TemplateLoader list.
|
protected ResourceLoader |
getResourceLoader()
Return the Spring ResourceLoader to use for loading FreeMarker template files.
|
protected TemplateLoader |
getTemplateLoaderForPath(java.lang.String templateLoaderPath)
Determine a FreeMarker TemplateLoader for the given path.
|
protected boolean |
isPreferFileSystemAccess()
Return whether to prefer file system access for template loading.
|
protected Configuration |
newConfiguration()
Return a new Configuration object.
|
protected void |
postProcessConfiguration(Configuration config)
To be overridden by subclasses that want to to perform custom
post-processing of the Configuration object after this factory
performed its default initialization.
|
protected void |
postProcessTemplateLoaders(java.util.List<TemplateLoader> templateLoaders)
To be overridden by subclasses that want to to register custom
TemplateLoader instances after this factory created its default
template loaders.
|
void |
setConfigLocation(Resource resource)
Set the location of the FreeMarker config file.
|
void |
setDefaultEncoding(java.lang.String defaultEncoding)
Set the default encoding for the FreeMarker configuration.
|
void |
setFreemarkerSettings(java.util.Properties settings)
Set properties that contain well-known FreeMarker keys which will be
passed to FreeMarker's
Configuration.setSettings method. |
void |
setFreemarkerVariables(java.util.Map<java.lang.String,java.lang.Object> variables)
Set a Map that contains well-known FreeMarker objects which will be passed
to FreeMarker's
Configuration.setAllSharedVariables() method. |
void |
setPostTemplateLoaders(TemplateLoader... postTemplateLoaders)
Set a List of
TemplateLoader s that will be used to search
for templates. |
void |
setPreferFileSystemAccess(boolean preferFileSystemAccess)
Set whether to prefer file system access for template loading.
|
void |
setPreTemplateLoaders(TemplateLoader... preTemplateLoaders)
Set a List of
TemplateLoader s that will be used to search
for templates. |
void |
setResourceLoader(ResourceLoader resourceLoader)
Set the Spring ResourceLoader to use for loading FreeMarker template files.
|
void |
setTemplateLoaderPath(java.lang.String templateLoaderPath)
Set the Freemarker template loader path via a Spring resource location.
|
void |
setTemplateLoaderPaths(java.lang.String... templateLoaderPaths)
Set multiple Freemarker template loader paths via Spring resource locations.
|
void |
setTemplateLoaders(TemplateLoader... templateLoaders)
Deprecated.
as of Spring 2.0.1, in favor of the "preTemplateLoaders"
and "postTemplateLoaders" properties
|
protected final Log logger
private Resource configLocation
private java.util.Properties freemarkerSettings
private java.util.Map<java.lang.String,java.lang.Object> freemarkerVariables
private java.lang.String defaultEncoding
private final java.util.List<TemplateLoader> templateLoaders
private java.util.List<TemplateLoader> preTemplateLoaders
private java.util.List<TemplateLoader> postTemplateLoaders
private java.lang.String[] templateLoaderPaths
private ResourceLoader resourceLoader
private boolean preferFileSystemAccess
public void setConfigLocation(Resource resource)
public void setFreemarkerSettings(java.util.Properties settings)
Configuration.setSettings
method.freemarker.template.Configuration#setSettings
public void setFreemarkerVariables(java.util.Map<java.lang.String,java.lang.Object> variables)
Configuration.setAllSharedVariables()
method.freemarker.template.Configuration#setAllSharedVariables
public void setDefaultEncoding(java.lang.String defaultEncoding)
Used for template rendering unless there is an explicit encoding specified for the rendering process (for example, on Spring's FreeMarkerView).
freemarker.template.Configuration#setDefaultEncoding
,
FreeMarkerView.setEncoding(java.lang.String)
@Deprecated public void setTemplateLoaders(TemplateLoader... templateLoaders)
TemplateLoader
s that will be used to search
for templates. For example, one or more custom loaders such as database
loaders could be configured and injected here.public void setPreTemplateLoaders(TemplateLoader... preTemplateLoaders)
TemplateLoader
s that will be used to search
for templates. For example, one or more custom loaders such as database
loaders could be configured and injected here.
The TemplateLoaders
specified here will be
registered before the default template loaders that this factory
registers (such as loaders for specified "templateLoaderPaths" or any
loaders registered in postProcessTemplateLoaders(java.util.List<TemplateLoader>)
).
public void setPostTemplateLoaders(TemplateLoader... postTemplateLoaders)
TemplateLoader
s that will be used to search
for templates. For example, one or more custom loaders such as database
loaders can be configured.
The TemplateLoaders
specified here will be
registered after the default template loaders that this factory
registers (such as loaders for specified "templateLoaderPaths" or any
loaders registered in postProcessTemplateLoaders(java.util.List<TemplateLoader>)
).
public void setTemplateLoaderPath(java.lang.String templateLoaderPath)
public void setTemplateLoaderPaths(java.lang.String... templateLoaderPaths)
When populated via a String, standard URLs like "file:" and "classpath:" pseudo URLs are supported, as understood by ResourceEditor. Allows for relative paths when running in an ApplicationContext.
Will define a path for the default FreeMarker template loader.
If a specified resource cannot be resolved to a java.io.File
,
a generic SpringTemplateLoader will be used, without modification detection.
To enforce the use of SpringTemplateLoader, i.e. to not resolve a path as file system resource in any case, turn off the "preferFileSystemAccess" flag. See the latter's javadoc for details.
If you wish to specify your own list of TemplateLoaders, do not set this
property and instead use setTemplateLoaders(List templateLoaders)
ResourceEditor
,
ResourceLoader.getResource(java.lang.String)
,
freemarker.template.Configuration#setDirectoryForTemplateLoading
,
SpringTemplateLoader
,
setTemplateLoaders(TemplateLoader...)
public void setResourceLoader(ResourceLoader resourceLoader)
DefaultResourceLoader
protected ResourceLoader getResourceLoader()
public void setPreferFileSystemAccess(boolean preferFileSystemAccess)
If this is enabled, FreeMarkerConfigurationFactory will try to resolve the specified "templateLoaderPath" as file system resource (which will work for expanded class path resources and ServletContext resources too).
Default is "true". Turn this off to always load via SpringTemplateLoader (i.e. as stream, without hot detection of template changes), which might be necessary if some of your templates reside in an expanded classes directory while others reside in jar files.
protected boolean isPreferFileSystemAccess()
public Configuration createConfiguration() throws java.io.IOException, TemplateException
java.io.IOException
- if the config file wasn't foundTemplateException
- on FreeMarker initialization failureprotected Configuration newConfiguration() throws java.io.IOException, TemplateException
Called by createConfiguration()
.
java.io.IOException
- if a config file wasn't foundTemplateException
- on FreeMarker initialization failurecreateConfiguration()
protected TemplateLoader getTemplateLoaderForPath(java.lang.String templateLoaderPath)
Default implementation creates either a FileTemplateLoader or a SpringTemplateLoader.
templateLoaderPath
- the path to load templates fromfreemarker.cache.FileTemplateLoader
,
SpringTemplateLoader
protected void postProcessTemplateLoaders(java.util.List<TemplateLoader> templateLoaders)
Called by createConfiguration()
. Note that specified
"postTemplateLoaders" will be registered after any loaders
registered by this callback; as a consequence, they are are not
included in the given List.
templateLoaders
- the current List of TemplateLoader instances,
to be modified by a subclasscreateConfiguration()
,
setPostTemplateLoaders(TemplateLoader...)
protected TemplateLoader getAggregateTemplateLoader(java.util.List<TemplateLoader> templateLoaders)
templateLoaders
- the final List of TemplateLoader instancesprotected void postProcessConfiguration(Configuration config) throws java.io.IOException, TemplateException
Called by createConfiguration()
.
config
- the current Configuration objectjava.io.IOException
- if a config file wasn't foundTemplateException
- on FreeMarker initialization failurecreateConfiguration()