spring - 由 : java. io.FileNotFoundException : class path resource [application. properties] 导致无法打开,因为它不存在

标签 spring spring-boot spring-batch

我正在创建一个 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

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

最佳答案

  • 检查您的 application.properties 文件是否在资源目录中,如下图所示:
    enter image description here
  • 如果您将 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>    
    

  • enter image description here

    关于spring - 由 : java. io.FileNotFoundException : class path resource [application. properties] 导致无法打开,因为它不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55687518/

    相关文章:

    java - 在 Java 11 中使用 Spring boot 2.0 时创建名称为 'entityManagerFactory' 的 bean 时出错

    java - 使用 JdbcTemplate 检索多行列按列数据的更好方法

    spring - Spring 中使用 STOMP 的多个房间

    Spring RestTemplate 收到 "401 Unauthorized"

    java - 事务中hibernate删除实体后仍然可以选择

    java - spring 批处理中的 OptimisticLockingFailureException

    java - 将 JobLaunchingGateway 与 spring-batch 和 spring-integration 一起使用抛出 DestinationResolutionException

    java - Vaadin Spring 中的 MVP 结构

    rest - 是否可以使用 REST 和 httpInvoker 在 Spring Boot 中远程公开 bean

    java - 在 Thymeleaf 和 Spring Boot 的嵌套类中使用枚举