在.xml文件中读取.properties文件

时间:2014-11-17 10:14:07

标签: java xml spring

我在类路径中有appcontext.xml文件。我在appcontext.xml中使用config.properties文件来读取数据库属性。属性文件存储在src / main / config / core / config.properties结构中,我在xml文件中使用它,

<property name="locations">
   <list>
        <value>${PF_CORE_HOME}/config/core/config.properties</value>
    </list>
</property>

但它会出错,

org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: class path resource [C:/prov-eng-2/prov-eng/provfwk/pf-core/src/main/config/core/config.properties] cannot be opened because it does not exist
    at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:89) ~[spring-beans-4.0.3.RELEASE.jar:4.0.3.RELEASE]
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:265) ~[spring-context-4.0.3.RELEASE.jar:4.0.3.RELEASE]
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:162) ~[spring-context-4.0.3.RELEASE.jar:4.0.3.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:609) ~[spring-context-4.0.3.RELEASE.jar:4.0.3.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464) ~[spring-context-4.0.3.RELEASE.jar:4.0.3.RELEASE]
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139) ~[spring-context-4.0.3.RELEASE.jar:4.0.3.RELEASE]
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83) ~[spring-context-4.0.3.RELEASE.jar:4.0.3.RELEASE]
    at com.gsl.pf.core.AdaptorManager.<init>(AdaptorManager.java:28) ~[pf-core-0.1.0-SNAPSHOT.jar:na]
    at com.gsl.pf.core.AdaptorManager.getInstance(AdaptorManager.java:34) ~[pf-core-0.1.0-SNAPSHOT.jar:na]
    at com.gsl.pf.fwk.FrameworkLoader.initialize(FrameworkLoader.java:35) ~[pf-fwk-0.1.0-SNAPSHOT.jar:na]
    at com.gsl.pf.scimsvr.app.Loader.initialize(Loader.java:32) ~[classes/:na]
    at com.gsl.pf.scimsvr.app.ScimServer.start(ScimServer.java:62) [classes/:na]
    at com.gsl.pf.scimsvr.app.ScimServer.main(ScimServer.java:47) [classes/:na]
Caused by: java.io.FileNotFoundException: class path resource [C:/prov-eng-2/prov-eng/provfwk/pf-core/src/main/config/core/config.properties] cannot be opened because it does not exist
    at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:158) ~[spring-core-4.0.3.RELEASE.jar:4.0.3.RELEASE]
    at org.springframework.core.io.support.EncodedResource.getInputStream(EncodedResource.java:143) ~[spring-core-4.0.3.RELEASE.jar:4.0.3.RELEASE]
    at org.springframework.core.io.support.PropertiesLoaderUtils.fillProperties(PropertiesLoaderUtils.java:98) ~[spring-core-4.0.3.RELEASE.jar:4.0.3.RELEASE]
    at org.springframework.core.io.support.PropertiesLoaderSupport.loadProperties(PropertiesLoaderSupport.java:175) ~[spring-core-4.0.3.RELEASE.jar:4.0.3.RELEASE]
    at org.springframework.core.io.support.PropertiesLoaderSupport.mergeProperties(PropertiesLoaderSupport.java:156) ~[spring-core-4.0.3.RELEASE.jar:4.0.3.RELEASE]
    at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:80) ~[spring-beans-4.0.3.RELEASE.jar:4.0.3.RELEASE]
    ... 12 common frames omitted

还有其他方法可以在.xml中读取.property文件。

1 个答案:

答案 0 :(得分:0)

如果你正确地这样做,这种方式就有效。

这是你的问题:

Caused by: java.io.FileNotFoundException: class path resource [C:/prov-eng-2/prov-eng/provfwk/pf-core/src/main/config/core/config.properties] cannot be opened because it does not exist

您正在从CLASSPATH读取此文件,但该路径不是它的一部分。

您的CLASSPATH中不包含/ src。我将.properties文件放在/ resources目录中并将其打包到CLASSPATH中(例如/ classes下的相对路径)。