有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java XmlBeanFactory无法作为占位符加载属性文件?

我需要在Jasper报告中加载客户字体,所以我使用字体扩展。问题是,由于遗留问题,我希望从本地计算机而不是类路径加载字体文件。因此,我将配置为folliwing

文件jasperreports\u扩展名。性质

net.sf.jasperreports.extension.registry.factory.fonts=net.sf.jasperreports.extensions.SpringExtensionsRegistryFactory

net.sf.jasperreports.extension.fonts.spring.beans.resource=META-INF/fonts/fonts.xml

#extra properties for font path
font.path=C:/Windows/Fonts 

和字体。xml

<?xml version="1.0" encoding="UTF-8"?>

<beans:beans 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.3.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd">

<beans:bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">    
    <beans:property name="locations" value="classpath:jasperreports_extension.properties">  <!--reads config.properties file-->  
    </beans:property>
</beans:bean>  
<beans:bean id="Verdana" class="net.sf.jasperreports.engine.fonts.SimpleFontFamily">

    <beans:property name="name" value="Verdana"/>

    <beans:property name="normal" value="file:${font.path}/VERDANA.TTF"/>

    <beans:property name="bold" value="file:${font.path}/VERDANAB.TTF"/>

    <beans:property name="italic" value="file:${font.path}/VERDANAI.TTF"/>

    <beans:property name="boldItalic" value="file:${font.path}/VERDANAZ.TTF"/>

    <beans:property name="pdfEncoding" value="Identity-H"/>

    <beans:property name="pdfEmbedded" value="true"/>
</beans:bean>

但XmlBeanFactory(在SpringExtensionsRegistryFactory.java中)似乎无法使用PlaceHoder加载属性,或者我遗漏了什么

错误日志:

org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (4) are:
PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'normal' threw exception; nested exception is net.sf.jasperreports.engine.JRRuntimeException: net.sf.jasperreports.engine.JRException: Error opening input stream from URL : file:${font.path}/VERDANA.TTF
PropertyAccessException 2: org.springframework.beans.MethodInvocationException: Property 'bold' threw exception; nested exception is net.sf.jasperreports.engine.JRRuntimeException: net.sf.jasperreports.engine.JRException: Error opening input stream from URL : file:${font.path}/VERDANAB.TTF
PropertyAccessException 3: org.springframework.beans.MethodInvocationException: Property 'italic' threw exception; nested exception is net.sf.jasperreports.engine.JRRuntimeException: net.sf.jasperreports.engine.JRException: Error opening input stream from URL : file:${font.path}/VERDANAI.TTF
PropertyAccessException 4: org.springframework.beans.MethodInvocationException: Property 'boldItalic' threw exception; nested exception is net.sf.jasperreports.engine.JRRuntimeException: net.sf.jasperreports.engine.JRException: Error opening input stream from URL : file:${font.path}/VERDANAZ.TTF
    org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:102)
    org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:58)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1393)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1118)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)

解决方案

在Himanshu的指导下,我创建了SpringExtensionsRegistryFactory的一个子类。java,并将其用作jasperreports_扩展中的工厂类。财产。它是一种魅力

public class SpringExtensionsRegistryFactory extends
        net.sf.jasperreports.extensions.SpringExtensionsRegistryFactory {
    protected ListableBeanFactory getBeanFactory(String registryId,
            JRPropertiesMap properties)
    {
        ListableBeanFactory factory = super.getBeanFactory(registryId, properties);
        PropertyPlaceholderConfigurer cfg = (PropertyPlaceholderConfigurer)factory.getBean("propertyConfigurer");
        cfg.postProcessBeanFactory((XmlBeanFactory)factory);
        return factory;
    }
}

共 (1) 个答案

  1. # 1 楼答案

    根据spring文档

    bean factory后处理器是一个实现org的java类。springframework。豆。工厂配置。BeanFactory后处理器接口。它可以手动执行(对于BeanFactory)或自动执行(对于ApplicationContext),以便在构建整个BeanFactory后将某种更改应用于整个BeanFactory。Spring包括许多预先存在的bean factory后处理器,如下文所述的PropertyResourceConfigure和PropertyPlaceHolderConfigure,以及BeanNameAutoProxyCreator,对于以事务方式包装其他bean或使用任何其他类型的代理非常有用,如本手册后面所述

    单向使用ApplicationContext代替XMLBeanFactory

    另一种方式调用后处理器,例如:

    XmlBeanFactory factory = new XmlBeanFactory(new FileSystemResource("beans.xml"));
    // create placeholderconfigurer to bring in some property
    // values from a Properties file
    PropertyPlaceholderConfigurer cfg = factory.getBean("<bean id of the property configurer>")
    // now actually do the replacement
    cfg.postProcessBeanFactory(factory);