原因:java.io.FileNotFoundException:类路径资源\[application.properties\]无法打开,因为它不存在

新手上路,请多包涵

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

 '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

请让我知道为什么我会遇到这个问题??

原文由 escort 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 2.1k
1 个回答
  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>

在此处输入图像描述

原文由 Abdur Rahman 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题