gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 07:17:02 31 4
gpt4 key购买 nike

我需要在 Jasper 报表中加载客户字体,所以我使用了字体扩展。问题是由于遗留问题,我想从本地机器而不是类路径加载字体文件。所以,我配置如下

文件 jasperreports_extension.properties

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

和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 中)无法使用 place hoder 加载属性,或者我错过了什么?

错误日志:

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_extension.properties 中的工厂类。它很有魅力。

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;
}
}

最佳答案

根据 spring 文档,

bean 工厂后处理器是一个实现 org.springframework.beans.factory.config.BeanFactoryPostProcessor 接口(interface)的 java 类。它是手动执行的(在 BeanFactory 的情况下)或自动执行的(在 ApplicationContext 的情况下),以便在构建 BeanFactory 后将某种更改应用于整个 BeanFactory。 Spring 包括许多预先存在的 bean 工厂后处理器,例如 PropertyResourceConfigurer 和 PropertyPlaceHolderConfigurer,都在下面描述,以及 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);

关于java - XmlBeanFactory 无法加载属性文件作为占位符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16460153/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com