Apache Velocity 2.0 如何编写自定义资源加载器?
Apache Velocity 2.0 how to write a custom resource loader?
API文档中有一个ResourceLoader
class:
我想实现自己的加载器,因为我需要从数据库加载模板,但是以上下文相关的方式(换句话说:不能使用DataSourceResourceLoader,我需要编写自定义代码到select 来自数据库的 "right" 模板)。
好像ResourceLoader
有一些抽象方法,而且我好像也可以通过实现这些抽象方法来写一个自定义加载器。但是我看不出有什么方法可以向引擎添加新的加载程序。没有 "addResourceLoader" 方法。该文档仅显示如何配置 Velocity 内置的加载器:
https://velocity.apache.org/engine/2.0/developer-guide.html#resource-loaders
主要问题:如何将自定义资源加载器添加到 VelocityEngine(或 VelocityContext?)
另一个问题:我想关闭所有内置加载器。特别是 WebappResourceLoader
默认情况下处于活动状态,并且代表我的特定应用程序中的安全风险。怎么做?
我无法回答如何实现您自己的资源加载器,但第二部分很简单:
创建 VelocityEngine 时,您可以传递定义资源加载器 "class path" 的属性
不同的类加载器由 属性 键的前缀标识。所以像:
Properties props = new Properties();
// Add a default class path resource loader - just an example
props.setProperty("cp.resource.loader.class", ClasspathResourceLoader.class.getName());
props.setProperty("cp.resource.loader.cache", "true);
// Add your own resource loader
props.setProperty("db.resource.loader.class", MyDBResourceLoader.class.getName());
props.setProperty("db.resource.loader.cache", "false");
// Define the "class path" for the loaders
// in this case first the "db" loader is asked for resources, if nothing is found the "cp" loader
props.setProperty(RuntimeConstants.RESOURCE_LOADER, "db,cp");
// Now create the engine
VelocityEngine engine = new VelocityEngine(props);
以上仅为该引擎定义了两个资源加载器,该引擎实例不会使用其他加载器。
您必须首先实现 ResourceLoader 接口(或现有资源加载器的子类),然后在 velocity.properties
:
中声明您的资源加载器
resource.loader = .... , my_loader, ...
my_loader.resource.loader.class = com.foo.MyResourceLoader
my_loader.resource.loader.some_property = some_value
... other properties...
请注意,配置属性语法在 2.1 版中发生了一些变化。虽然旧语法仍然有效,但如果您想避免在日志中出现弃用警告,则可以使用:
resource.loaders = .... , my_loader, ...
resource.loader.my_loader.class = com.foo.MyResourceLoader
resource.loader.m_loader.some_property = some_value
... other properties...
has ResourceLoader接口有四个抽象方法需要你提供:
void init(ExtProperties configuration)
其中 configuration
对象包含 some_property -> some_value
属性对
long getLastModified(Resource resource)
其中 returns 资源被修改后的秒数
Reader getResourceReader(String source, String encoding)
获取thre资源的实际内容
boolean isSourceModified(Resource resource)
API文档中有一个ResourceLoader
class:
我想实现自己的加载器,因为我需要从数据库加载模板,但是以上下文相关的方式(换句话说:不能使用DataSourceResourceLoader,我需要编写自定义代码到select 来自数据库的 "right" 模板)。
好像ResourceLoader
有一些抽象方法,而且我好像也可以通过实现这些抽象方法来写一个自定义加载器。但是我看不出有什么方法可以向引擎添加新的加载程序。没有 "addResourceLoader" 方法。该文档仅显示如何配置 Velocity 内置的加载器:
https://velocity.apache.org/engine/2.0/developer-guide.html#resource-loaders
主要问题:如何将自定义资源加载器添加到 VelocityEngine(或 VelocityContext?)
另一个问题:我想关闭所有内置加载器。特别是 WebappResourceLoader
默认情况下处于活动状态,并且代表我的特定应用程序中的安全风险。怎么做?
我无法回答如何实现您自己的资源加载器,但第二部分很简单:
创建 VelocityEngine 时,您可以传递定义资源加载器 "class path" 的属性
不同的类加载器由 属性 键的前缀标识。所以像:
Properties props = new Properties();
// Add a default class path resource loader - just an example
props.setProperty("cp.resource.loader.class", ClasspathResourceLoader.class.getName());
props.setProperty("cp.resource.loader.cache", "true);
// Add your own resource loader
props.setProperty("db.resource.loader.class", MyDBResourceLoader.class.getName());
props.setProperty("db.resource.loader.cache", "false");
// Define the "class path" for the loaders
// in this case first the "db" loader is asked for resources, if nothing is found the "cp" loader
props.setProperty(RuntimeConstants.RESOURCE_LOADER, "db,cp");
// Now create the engine
VelocityEngine engine = new VelocityEngine(props);
以上仅为该引擎定义了两个资源加载器,该引擎实例不会使用其他加载器。
您必须首先实现 ResourceLoader 接口(或现有资源加载器的子类),然后在 velocity.properties
:
resource.loader = .... , my_loader, ...
my_loader.resource.loader.class = com.foo.MyResourceLoader
my_loader.resource.loader.some_property = some_value
... other properties...
请注意,配置属性语法在 2.1 版中发生了一些变化。虽然旧语法仍然有效,但如果您想避免在日志中出现弃用警告,则可以使用:
resource.loaders = .... , my_loader, ...
resource.loader.my_loader.class = com.foo.MyResourceLoader
resource.loader.m_loader.some_property = some_value
... other properties...
has ResourceLoader接口有四个抽象方法需要你提供:
void init(ExtProperties configuration)
其中configuration
对象包含some_property -> some_value
属性对long getLastModified(Resource resource)
其中 returns 资源被修改后的秒数Reader getResourceReader(String source, String encoding)
获取thre资源的实际内容boolean isSourceModified(Resource resource)