我已经使用DefaultConfigurationBuilder尝试集成我的属性和xml文件,如下面的代码:
DefaultConfigurationBuilder dcfb = new DefaultConfigurationBuilder();
File file = new File(filePath);
System.out.println(file.getName());
System.out.println(file.getAbsolutePath());
dcfb.setFile(new File(filePath));
config = dcfb.getConfiguration(true);
xml文件如下所示,其中application.properties是属性文件:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<properties fileName="application.properties"/>
</configuration>
<PropertyList>
<Property>
<Name>CommandTimeout</Name>
<Value>60</Value>
<Description>Setting the timeout(in seconds)</Description>
<DefaultValue></DefaultValue>
</Property>
<Property>
<Name>Address</Name>
<Value>${server.IP}</Value>
<Description>ip:port</Description>
<DefaultValue></DefaultValue>
</Property>
</PropertyList>
但是在调用getConfiguration(true);
时我得到了异常org.apache.commons.configuration.ConfigurationException:
org.apache.commons.configuration.ConfigurationRuntimeException: org.apache.commons.configuration.ConfigurationRuntimeException: org.apache.commons.configuration.ConfigurationRuntimeException: Bean class is not specified!
at org.apache.commons.configuration.DefaultConfigurationBuilder.createConfigurationAt(DefaultConfigurationBuilder.java:860)
at org.apache.commons.configuration.DefaultConfigurationBuilder.initCombinedConfiguration(DefaultConfigurationBuilder.java:719)
at org.apache.commons.configuration.DefaultConfigurationBuilder.getConfiguration(DefaultConfigurationBuilder.java:622)
at manage.utility.XMLProperty.<init>(XMLProperty.java:32)
at manage.utility.WebMapLicenseKeyListener.contextInitialized(WebMapLicenseKeyListener.java:38)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4729)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5167)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1409)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1399)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.apache.commons.configuration.ConfigurationRuntimeException: org.apache.commons.configuration.ConfigurationRuntimeException: org.apache.commons.configuration.ConfigurationRuntimeException: Bean class is not specified!
at org.apache.commons.configuration.beanutils.BeanHelper.createBean(BeanHelper.java:395)
at org.apache.commons.configuration.beanutils.BeanHelper.createBean(BeanHelper.java:412)
at org.apache.commons.configuration.beanutils.BeanHelper.createBean(BeanHelper.java:426)
at org.apache.commons.configuration.DefaultConfigurationBuilder.createConfigurationAt(DefaultConfigurationBuilder.java:855)
... 13 more
Caused by: org.apache.commons.configuration.ConfigurationRuntimeException: org.apache.commons.configuration.ConfigurationRuntimeException: Bean class is not specified!
at org.apache.commons.configuration.beanutils.BeanHelper.createBean(BeanHelper.java:395)
at org.apache.commons.configuration.beanutils.BeanHelper.createBean(BeanHelper.java:412)
at org.apache.commons.configuration.beanutils.BeanHelper.initBean(BeanHelper.java:226)
at org.apache.commons.configuration.beanutils.DefaultBeanFactory.initBeanInstance(DefaultBeanFactory.java:108)
at org.apache.commons.configuration.DefaultConfigurationBuilder$ConfigurationBuilderProvider.initBeanInstance(DefaultConfigurationBuilder.java:1763)
at org.apache.commons.configuration.beanutils.DefaultBeanFactory.createBean(DefaultBeanFactory.java:64)
at org.apache.commons.configuration.DefaultConfigurationBuilder$ConfigurationProvider.getConfiguration(DefaultConfigurationBuilder.java:1066)
at org.apache.commons.configuration.DefaultConfigurationBuilder$ConfigurationBuilderProvider.getConfiguration(DefaultConfigurationBuilder.java:1729)
at org.apache.commons.configuration.DefaultConfigurationBuilder$ConfigurationBeanFactory.createBean(DefaultConfigurationBuilder.java:1321)
at org.apache.commons.configuration.beanutils.BeanHelper.createBean(BeanHelper.java:390)
... 16 more
Caused by: org.apache.commons.configuration.ConfigurationRuntimeException: Bean class is not specified!
at org.apache.commons.configuration.beanutils.BeanHelper.fetchBeanClass(BeanHelper.java:484)
at org.apache.commons.configuration.beanutils.BeanHelper.createBean(BeanHelper.java:390)
... 25 more
感谢任何帮助。
答案 0 :(得分:0)
尝试在每个Property标记中添加属性config-class,用于指定映射配置的类:
<Property config-class="FullQulifiesNameOfPropertyClass">