Jasper 报告 - 如何更改 xpath 执行器工厂

Jasper Report - How to change xpath executer factory

我正在检查以下问题 JasperReports fillReport too slow and resource consuming,我尝试应用经过验证的答案,但在我的报告生成过程中没有发现任何变化。

基本上我想做的是更改 JasperFillManager 的 xpath 执行器工厂以使用 Jaxen 而不是 Xalan,但我似乎不知道应该在我的代码中放置以下行的位置

    DefaultJasperReportsContext context = DefaultJasperReportsContext.getInstance();
    JRPropertiesUtil.getInstance(context).setProperty("net.sf.jasperreports.xpath.executer.factory",
                                                      "net.sf.jasperreports.engine.util.xml.JaxenXPathExecuterFactory");

我的代码块如下所示

private JasperPrint getJasperPrintInstance(JasperReport report) throws JRException, NamingException, SQLException {
    JasperPrint jasperPrint = null;
    DefaultJasperReportsContext context = DefaultJasperReportsContext.getInstance();
    JRPropertiesUtil.getInstance(context).setProperty("net.sf.jasperreports.xpath.executer.factory",
                                                      "net.sf.jasperreports.engine.util.xml.JaxenXPathExecuterFactory");
    if (dataSource == null) {
        jasperPrint = JasperFillManager.fillReport(report, this.reportParams, connection);
    } else {
        jasperPrint =
            JasperFillManager.fillReport(report, this.reportParams,
                                         new JRBeanCollectionDataSource(getDataSource()));
    }

    return jasperPrint;
}

即使我将 "net.sf.jasperreports.engine.util.xml.JaxenXPathExecuterFactory" 更改为其他任何内容,也没有任何变化。

你能告诉我我做错了什么吗?

发生这种情况是因为您没有使用新创建的 context。您需要用它创建一个 JasperFillManager 实例:

JasperFillManager jasperFillManager = JasperFillManager.getInstance(context);

并将其与其实例方法一起使用,而不是像这样的 class 实例方法:

jasperPrint = jasperFillManager.fill(report, this.reportParams, connection);