You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Apache Velocity 2.0自定义ResourceLoader添加及内置加载器关闭方法问询

解决Velocity自定义ResourceLoader添加及内置加载器关闭问题

一、如何向VelocityEngine添加自定义ResourceLoader

VelocityEngine并没有提供直接的addResourceLoader方法,而是通过配置属性的方式来注册自定义加载器,步骤如下:

1. 实现自定义ResourceLoader类

首先你需要继承ResourceLoader抽象类,并重写所有抽象方法,核心是实现从数据库加载模板的逻辑:

import org.apache.velocity.runtime.resource.loader.ResourceLoader;
import org.apache.velocity.runtime.resource.Resource;
import org.apache.velocity.util.ExtendedProperties;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

public class DatabaseResourceLoader extends ResourceLoader {

    @Override
    public void init(ExtendedProperties configuration) {
        // 这里可以初始化数据库连接相关配置,比如从configuration读取数据库参数
        String dbUrl = configuration.getString("db.url");
        String dbUser = configuration.getString("db.user");
        // 初始化数据库连接池等操作
    }

    @Override
    public InputStream getResourceStream(String templateKey) throws ResourceNotFoundException {
        // 根据模板标识(templateKey)从数据库查询模板内容
        String templateContent = queryTemplateFromDatabase(templateKey);
        if (templateContent == null) {
            throw new ResourceNotFoundException("Template not found for key: " + templateKey);
        }
        return new ByteArrayInputStream(templateContent.getBytes(StandardCharsets.UTF_8));
    }

    @Override
    public boolean isSourceModified(Resource resource) {
        // 对比数据库中模板的最后修改时间与resource记录的时间,判断是否更新
        long dbLastModified = getTemplateLastModified(resource.getName());
        return dbLastModified != resource.getLastModified();
    }

    @Override
    public long getLastModified(Resource resource) {
        // 返回数据库中该模板的最后修改时间戳
        return getTemplateLastModified(resource.getName());
    }

    // 自定义数据库查询方法,根据业务实现
    private String queryTemplateFromDatabase(String templateKey) {
        // 这里写你的数据库查询逻辑,比如JDBC、MyBatis调用
        return "SELECT content FROM templates WHERE id = ?"; // 示例SQL
    }

    private long getTemplateLastModified(String templateKey) {
        // 查询数据库中模板的最后修改时间
        return 0L; // 替换为实际查询逻辑
    }
}

2. 通过配置注册自定义加载器

创建VelocityEngine实例时,通过设置属性指定你的自定义加载器:

import org.apache.velocity.app.VelocityEngine;

public class VelocitySetup {
    public static void main(String[] args) {
        VelocityEngine engine = new VelocityEngine();
        
        // 1. 指定启用的资源加载器名称(这里命名为db)
        engine.setProperty("resource.loader", "db");
        
        // 2. 关联自定义加载器的全类名
        engine.setProperty("db.resource.loader.class", "com.yourpackage.DatabaseResourceLoader");
        
        // 3. 可选:传递自定义加载器需要的配置参数(会传入init方法的configuration参数)
        engine.setProperty("db.resource.loader.db.url", "jdbc:mysql://localhost:3306/your_db");
        engine.setProperty("db.resource.loader.db.user", "root");
        
        // 初始化引擎
        engine.init();
        
        // 测试加载模板
        engine.getTemplate("user_profile_template"); // 会调用你的自定义加载器
    }
}

二、如何关闭所有内置加载器(包括WebappResourceLoader)

Velocity只会初始化你在resource.loader属性中明确指定的加载器,所以要关闭内置加载器,只需:

  1. 只配置你的自定义加载器:不要在resource.loader中加入任何内置加载器的名称(比如file、webapp、class等)
  2. 删除所有内置加载器的配置:确保没有设置类似file.resource.loader.class、webapp.resource.loader.class这类属性

比如上面的配置中,我们只指定了db加载器,那么Velocity就只会初始化DatabaseResourceLoader,不会启用任何内置加载器。

如果是Web环境下,要特别注意不要配置WebappResourceLoader相关属性,避免它被自动启用。如果之前的配置中有启用它,直接覆盖resource.loader属性为你的自定义加载器名称即可。

内容的提问来源于stack exchange,提问作者nagylzs

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 09:00:55