spring java.io.FileNotFoundException: 类路径资源 [timex-servlet.properties] 无法打开,因为它不存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10131812/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 05:04:51  来源:igfitidea点击:

java.io.FileNotFoundException: class path resource [timex-servlet.properties] cannot be opened because it does not exist

springfilenotfoundexceptionproperties-filetimex

提问by opike

I'm getting the following error message when I try to start a spring web application:

当我尝试启动 Spring Web 应用程序时收到以下错误消息:

2012-04-12 13:53:20,491 ERROR [org.springframework.web.servlet.DispatcherServlet] - 

Context initialization failed
org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: class path resource [timex-servlet.properties] cannot be opened because it does not exist
java.io.FileNotFoundException: class path resource [timex-servlet.properties] cannot be opened because it does not exist
    at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:137)
    at org.springframework.core.io.support.PropertiesLoaderSupport.loadProperties(PropertiesLoaderSupport.java:172)

I'm running Tomcat (version 6.x) through eclipse. I tried putting timex-servlet.properties in the following directories but to no avail:

我正在通过 Eclipse 运行 Tomcat(版本 6.x)。我尝试将 timex-servlet.properties 放在以下目录中,但无济于事:

WebContent\WEB-INF
WebContent\WEB-INF\classes
WebContent\

Here is the reference to timex-servlet.properties in timex-servlet.xml:

以下是 timex-servlet.xml 中对 timex-servlet.properties 的引用:

    <bean id="placeholderConfig"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location"
        value="timex-servlet.properties" />
</bean>

There are several SO threads dealing with the same message that say to put a classpath: in front of the properties file reference. So I tried the following, which also did not work:

有几个 SO 线程处理相同的消息,这些消息说要放置一个类路径:在属性文件引用的前面。所以我尝试了以下方法,但也不起作用:

        <bean id="placeholderConfig"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location"
        value="classpath:timex-servlet.properties" />
</bean>

回答by Chris White

ensure the file is placed in the /WEB-INF/classesfolder and use classpath:/timex-servlet.properties(note the slash after classpath:)

确保文件放在/WEB-INF/classes文件夹中并使用classpath:/timex-servlet.properties(注意类路径后的斜杠:)

回答by Sreesankar

If you do not want to move it under class and want to leave the properties file under WEB-INF/ try the following classpath:../file.properties

如果您不想将其移动到类下并希望将属性文件保留在 WEB-INF/ 下,请尝试以下类路径:../file.properties

回答by user8794280

Should not place your properties file in the same location where your source code and main method is defined.It should be place in the same location where your Configuration file is located(applicationContext.xml).

不应将您的属性文件放在定义源代码和主要方法的同一位置。它应该放在您的配置文件所在的同一位置 (applicationContext.xml)。

In my case my properties file name is sport.properties (extension of the file should be .properties) and using maven for the project -

在我的情况下,我的属性文件名是 sport.properties(文件的扩展名应该是 .properties)并在项目中使用 maven -

  1. Location of the properties file-> src->test->java->sport.properties
  1. 属性文件的位置-> src->test->java->sport.properties

PropertiesFilelocation

属性文件位置

回答by Vijay

For Spring projects, place the file under the src or src->somefolder->some.properties file.

对于 Spring 项目,将文件放在 src 或 src->somefolder->some.properties 文件下。

回答by OAM

In my case I was missing a cargo-maven2-plugin in my pom. Might help somebody who's stuck.

就我而言,我的 pom 中缺少一个 cargo-maven2-plugin。可能会帮助陷入困境的人。

回答by LexSav

Just describing my case here, maybe it'll help someone :) Used Maven and Spring MVC with pure Java configuration (no XML). Here is my steps to resolve the issue:

只是在这里描述我的情况,也许它会对某人有所帮助:) 使用纯 Java 配置(无 XML)的 Maven 和 Spring MVC。以下是我解决问题的步骤:

1) Place your properties file in src/main/resourcesfolder (create the last one manually if there is none).

1)将您的属性文件放在src/main/resources文件夹中(如果没有,请手动创建最后一个)。

Double check it, seriously! If you have the same level of attention as me, it can be a problem :)

2) Add to your Java configuration class the following annotation: @PropertySource("classpath:nameOfYourFile.properties")

3) Update your Maven project (in Eclipse: right-click on project -> Maven -> Update project).

And, of course, check that the name of your file matches the one in the annotation.
If anything else is OK with your project, then it should work.

仔细检查一下,认真的!如果你和我有同样的关注度,那可能是个问题:)

2) 在你的 Java 配置类中添加以下注释: @PropertySource("classpath:nameOfYourFile.properties")

3) 更新你的 Maven 项目(在 Eclipse 中:右键单击项目 -> Maven ->更新项目)。

当然,还要检查您的文件名称是否与注释中的名称匹配。
如果您的项目还有其他任何问题,那么它应该可以工作。