如何解决在使用Spring加载.properties文件时找不到文件?

时间:2019-02-08 16:28:44

标签: java spring

使用Spring读取migration.properties时,出现“ FileNotFoundException”。

<context:property-placeholder location="classpath:/config/migration.properties" />

文件目录: 迁移-> src->配置

堆栈跟踪:

`Caused by: java.io.FileNotFoundException: class path resource [config/migration.properties] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:158)
at org.springframework.core.io.support.EncodedResource.getInputStream(EncodedResource.java:143)
at org.springframework.core.io.support.PropertiesLoaderUtils.fillProperties(PropertiesLoaderUtils.java:98)
at org.springframework.core.io.support.PropertiesLoaderSupport.loadProperties(PropertiesLoaderSupport.java:175)
at org.springframework.core.io.support.PropertiesLoaderSupport.mergeProperties(PropertiesLoaderSupport.java:156)
at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.postProcessBeanFactory(PropertySourcesPlaceholderConfigurer.java:142)
... 8 more`

2 个答案:

答案 0 :(得分:0)

看起来像位于类路径中的migration.properties。您可以使用 file:/// 给出绝对路径并加载它。

答案 1 :(得分:-1)

 Caused by: java.io.FileNotFoundException:

您的文件可能不在您认为的位置。 我建议您打开文件夹,然后仔细检查文件的路径。我经常在Mac上只需单击“复制文件路径”即可使自己更轻松。

您在此处描述的路径: 迁移-> src->配置 将会是

 /migration/src/config/migration.properties 

如果文件名是migration.properties

另一个问题可能是/ Migration / src / config文件夹需要位于您的类路径中。 (原来是问题所在。)