【问题标题】:Caused by: java.io.FileNotFoundException: class path resource [application.properties] cannot be opened because it does not exist原因:java.io.FileNotFoundException:类路径资源[application.properties]无法打开,因为它不存在
【发布时间】:2019-09-05 08:05:50
【问题描述】:

我正在创建一个 Spring Boot Batch 应用程序。该批次从 postgres 加载数据并插入 MongoDB。我已经编写了代码,但是在运行 spring boot 应用程序时,它显示错误 application.properties 文件未找到。以下是错误sn-ps:

'Caused by: java.io.FileNotFoundException: class path resource [application.properties] cannot be opened because it does not exist'
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.example.batch.SpringBatchExample]; nested exception is java.io.FileNotFoundException: class path resource [application.properties] cannot be opened because it does not exist

下面是application.properties文件内容:

spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.database=Test_Data

#logging
logging.level.org.springframework.data=DEBUG
logging.level.=ERROR
logging.level.com.mastercard.refdata.*=DEBUG


#By default, Spring runs all the job as soon as it has started its context.
spring.batch.job.enabled=false

#Chunk Size to save data
spring.chunk.size=200

#Postgres DATASOURCE
spring.datasource.url=jdbc:postgresql://localhost:5432/Company-Test
spring.datasource.username=postgres
spring.datasource.password=test123
spring.datasource.driver-class-name=org.postgresql.Driver

请告诉我为什么会遇到这个问题??

【问题讨论】:

  • 请张贴您编写的代码,或一个最小的、可验证的代码示例。另外,请格式化您的问题,使其更具可读性。
  • 确保application.properties 文件在您的类路径中。如果你使用maven,应该在src/main/resources目录下。
  • 发布你的项目结构截图。

标签: spring spring-boot spring-batch


【解决方案1】:

application.properties 文件移动到资源目录。

【讨论】:

    【解决方案2】:
    1. 检查您的 application.properties 文件是否在资源目录中,如下图所示:

    2. 如果您将您的 application.properties 保存到其他文件夹(例如 config),如下图所示,则按照以下代码配置您的 pom.xml:

      <build>
      <resources>
          <resource>
              <directory>config</directory>
              <targetPath>${project.build.outputDirectory}</targetPath>
              <includes>
                  <include>application.properties</include>
              </includes>
          </resource>
        </resources>
      </build>    
      

    【讨论】:

    • 感谢您的提示。我已重命名属性文件,但忘记更改 pom 文件。你节省了我的时间;-)
    【解决方案3】:

    我的问题是一样的。您正在尝试连接项目中的多个数据库。

    在主应用程序运行中有一个注释,如:

    { { {@PropertySources({@PropertySource("/../resources/datasource_cfg.properties")})}
    

    它是应用程序运行时要解析的路径。也许你的路径不正确。

    【讨论】:

      【解决方案4】:

      对于固定路径,您可以使用:

      @PropertySource("file:${C:/Development/workspace/Projectname/resources/}/application.properties")

      【讨论】:

        【解决方案5】:
        Use this on configuration class
        
        @Configuration
        @PropertySource("classpath:application.properties")
        
        (OR)
        
        Use this in xml file
        
        <bean       class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
                <property name="locations">
                    <list>
                        <value>application.properties</value>
                    </list>
                </property>
            </bean>
        

        【讨论】:

          【解决方案6】:

          需要在Run Configuration的Bootstrap选项卡中配置config路径(projectBaseName\target\local\config),因为所有的properties文件都放在classpath下。

          【讨论】:

            【解决方案7】:

            将其标记为根资源。

            这是在 intellij 中的操作方式:

            【讨论】:

              【解决方案8】:

              配置你的 pom.xml 文件添加资源;

               <build>
                      <finalName>your-project-name</finalName>
                      <resources>
                          <resource>
                              <directory>src/main/resources</directory>
                              <filtering>true</filtering>
                              <includes>
                                  <include>**/*.properties</include>
                                  <include>**/*.yml</include>
                              </includes>
                          </resource>
                          <resource>
                              <directory>src/main/java</directory>
                              <filtering>true</filtering>
                              <includes>
                                  <include>**/*.properties</include>
                                  <include>**/*.yml</include>
                              </includes>
                              <excludes>
                                  <exclude>**/*.java</exclude>
                                  <exclude>**/*.class</exclude>
                              </excludes>
                          </resource>
                      </resources>
              </build>
              

              【讨论】:

                【解决方案9】:

                对我来说,解决方案是将库(在我的情况下是我的主项目的依赖项)重新添加到我正在处理的主项目的程序集部署中。由于某种原因,Eclipse 无法在其类路径中检测到此项目。

                https://i.stack.imgur.com/pyvTk.png

                【讨论】:

                  【解决方案10】:

                  而不是在@sqlgroup 使用的@sql 注释中使用classpath:filepath 文件:文件路径 它使 java 在整个文件夹中搜索类似路径的文件,对我来说就像一个魅力

                  【讨论】:

                    【解决方案11】:

                    作为记录,当您在多个 IDE 中打开项目时也会发生这种情况。例如,Eclipse 无法识别其他 IDE 的配置文件。

                    如果在 Eclipse 中发生这种情况,我们可以做的是转到 project -> "clean..." 选项。这将清理构建文件并重新构建项目。

                    【讨论】:

                      【解决方案12】:

                      我遇到了同样的问题,尽管所有配置都可以。很长一段时间后,我只是使用了 mvn clean package 命令,一切都很好。

                      【讨论】:

                      • 这也为我修复了它,而其他一切都没有! :)
                      猜你喜欢
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 2017-02-19
                      • 1970-01-01
                      • 2012-04-25
                      • 1970-01-01
                      • 2020-09-19
                      • 1970-01-01
                      相关资源
                      最近更新 更多