由于找不到类路径资源[application.properties],导致java.io.FileNotFoundException异常。

16

我正在创建一个Spring Boot批处理应用程序。这个批处理从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
请告诉我为什么我会遇到这个问题?

请发布您编写的代码,或者提供一个最小化、可验证的代码示例。 此外,请对您的问题进行格式化,以便更易读。 - SimonC
1
请确保 application.properties 文件在您的类路径中。如果您使用的是Maven,则应该位于 src/main/resources 目录中。 - Mahmoud Ben Hassine
1
请发布您的项目结构截图。 - Sangam Belose
19个回答

17

我曾遇到相同的问题,尽管所有配置都正确。不过经过一段时间后,我只是使用了mvn clean package命令,结果一切正常。


1
这对我也起作用了,而其他所有方法都没有! :) - elegon
这个建议帮助我摆脱了一个类似的错误。谢谢。 - koushick

11
  1. Check that your application.properties file is in the resources directory as picture shown below: enter image description here

  2. If you keep your application.properties to other folder (e.g config) as shown in below picture then configure your pom.xml following code:

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

enter image description here


1
谢谢你的提示。我已经重命名了属性文件,但忘记在pom文件中更改。你节省了我的时间;-) - vkrams

2
如果您使用的是IntelliJ Idea:
如果您将类路径声明为以下内容:
@PropertySource("classpath:sport.properties")
public class SportConfig {
 //some code
}

那么你应该将你的属性文件添加到资源目录中。

2
将其标记为根资源。 以下是在Intellij中的操作步骤:

enter image description here


2

配置你的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>

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

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

https://istack.dev59.com/pyvTk.webp


1
你需要在运行配置的 Bootstrap 选项卡中配置 config 路径(projectBaseName\target\local\config),因为所有属性文件都放置在类路径中。您可以按照下面的图片进行操作

0

enter image description here

属性文件位置:src/student-info.properties

我成功地导入了如下内容。

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">

    <context:property-placeholder location="file:src/student-info.properties"/>

    <bean id="student" class="com.Student">
        <property name="name" value="${student.name}"/>
        <property name="hobby" value="${student.interestedCourse}"/>
        <property name="interestedCourse" value="${student.hobby}"/>
    </bean>

</beans>

0
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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接