【问题标题】:Spring Boot cannot find properties file declared in external directorySpring Boot 找不到在外部目录中声明的属性文件
【发布时间】:2020-11-05 23:43:31
【问题描述】:

我在让 Spring Boot 应用程序在部署后获取属性文件时遇到问题。

我的目标是在 Jar 外部有一个配置文件夹,因此我在 Maven 构建中排除了配置:

<plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <configuration>
            <excludes>
                <exclude>*.*</exclude>
            </excludes>
        </configuration>
</plugin>

我有一个构建程序集配置为:

<assembly>
    <id>bin</id> 
    <formats>
        <format>zip</format>
    </formats>
    <fileSets>
        <fileSet>
            <directory>src/main/resources</directory>
            <outputDirectory>config</outputDirectory>
        </fileSet>
        <fileSet>
            <directory>src/main/bin</directory>
            <outputDirectory>bin</outputDirectory>
        </fileSet>
        <fileSet>
          <directory>target</directory>
          <outputDirectory>lib</outputDirectory>
         <includes>
           <include>jarname*.jar</include>
         </includes>
        </fileSet>
    </fileSets>
</assembly>

使用 config 文件夹中的配置文件和 lib 文件夹中的 jar 按预期创建我的文件夹结构

现在,当我从 lib 目录运行 jar 时,我收到一条错误消息,提示它找不到属性,这是我预期的,因为配置文件已从 Spring Boot 构建中排除:

java -jar jarname.jar

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

所以我按照documentation 中的建议将配置明确指向 jar:

java -jar jarname.jar --spring.config.location=file:C:\path-to-build\config\application.properties

但我仍然遇到同样的错误

【问题讨论】:

  • 试试这个; java -Dspring.config.location=/apps/xyzapp/properties/ -jar app-0.0.1-SNAPSHOT.jar

标签: spring spring-boot configuration


【解决方案1】:

你应该这样加载它

java -jar jarname.jar --spring.config.location=classpath:file:///C:/directory/jdbc.properties

【讨论】:

  • 谢谢,我也遇到了同样的问题
【解决方案2】:

所以这不是命令的问题,我的问题是我在其中一个 Java 类中明确引用了该文件:

@PropertySource("classpath:application.properties")

【讨论】:

    猜你喜欢
    • 2015-08-15
    • 2020-07-30
    • 2017-04-15
    • 1970-01-01
    • 2018-02-21
    • 2015-06-26
    • 2017-07-25
    • 2015-05-14
    相关资源
    最近更新 更多