【问题标题】:java.io.FileNotFoundException: class path resource [timex-servlet.properties] cannot be opened because it does not existjava.io.FileNotFoundException:类路径资源 [timex-servlet.properties] 无法打开,因为它不存在
【发布时间】:2012-04-25 06:50:49
【问题描述】:

我在尝试启动 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)

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

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

这里是timex-servlet.xml中对timex-servlet.properties的引用:

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

有几个 SO 线程处理相同的消息,表示将类路径:放在属性文件引用的前面。所以我尝试了以下方法,但也没有用:

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

【问题讨论】:

  • WEB-INF\classes 是 WAR 文件中的正确位置。 classpath:file.properties 是指示文件将位于此处的正确方法。我只能建议 Eclipse 构建您的 WAR 可能与您预期的不同。它可以与独立的 Tomcat 一起使用吗?

标签: spring filenotfoundexception properties-file timex


【解决方案1】:

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

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

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

PropertiesFilelocation

【讨论】:

    【解决方案2】:

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

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

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

    2) 将以下注释添加到您的 Java 配置类: @PropertySource("classpath:nameOfYourFile.properties")

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

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

    【讨论】:

      【解决方案3】:

      在我的情况下,我的 pom.xml 中缺少一个 cargo-maven2-plugin。可能会帮助陷入困境的人。

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          【解决方案5】:

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

          【讨论】:

            【解决方案6】:

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

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2017-02-19
              • 1970-01-01
              • 2019-09-05
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2014-08-27
              相关资源
              最近更新 更多