2013-05-09 87 views
2

我需要在Jasper報告中加載客戶字體,所以我使用字體擴展。問題是我想從本地機器加載字體文件,而不是由於遺留問題導致的類路徑。所以,我配置爲folliwingXmlBeanFactory無法加載屬性文件作爲佔位符?

文件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)不能使用地方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) 

SOLUTIONS

以下人士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; 
    } 
} 

回答

4

作爲每彈簧文檔,

bean工廠後處理器是一個Java類,它實現org.springframework.beans.factory.config.BeanFactoryPostProcessor接口。它是手動執行的(在BeanFactory的情況下)或自動執行(在ApplicationContext的情況下),在構造完成後將某種類型的更改應用於整個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); 
+0

我做你建議,但仍然沒有讀出的屬性。 – bnguyen82 2013-05-09 11:21:20

+0

查看已更新的答案 – 2013-05-09 11:32:31

+0

非常感謝Himanshu。它適用於我。 – bnguyen82 2013-05-09 11:54:22