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

Java 在.xml文件中读取.properties文件,java,xml,spring,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&

类路径中有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文件。

如果操作正确,这种方法可以工作

这是你的问题:

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
您正在从类路径读取此文件,但该路径不是它的一部分


在类路径中不包括/src。我会将该.properties文件放在/resources目录中,并将其打包到您的类路径中,例如/classes下的相对路径。

无法加载属性;嵌套异常为java.io.FileNotFoundException:无法打开类路径资源[C:/prov-eng-2/prov-eng/provfwk/pf-core/src/main/config/core/config.properties],因为它不存在。是否读取了异常?如果文件复制到类路径,则类路径:/core/config.properties。