【问题标题】:Spring Boot not working with dependent application.propertiesSpring Boot 不适用于依赖的 application.properties
【发布时间】:2020-11-28 00:42:51
【问题描述】:

我正在使用 Spring Boot 2.2.9.RELEASE。我有主应用程序和一些简单的启动器(仅使用 spring-actuator 功能)在其 some-starter/src/main/resources/application.properties 文件中有一些属性:

management.server.port=9000
management.server.ssl.enabled=false
management.endpoints.enabled-by-default=false
management.endpoint.health.enabled=true
management.endpoints.web.base-path=/
management.endpoints.web.path-mapping.health=health

我已将启动器导入到我的主应用程序中,并且我相信运行状况检查端点应该与端口 9000 和路径 /health 一起使用(类似于 localhost:9000/health)。

但事实并非如此。但是,它适用于我的主应用 main-app/src/main/resources/application.properties 中的相同属性。

Spring Boot 中的属性覆盖是否有问题?我应该通过maven-resources-plugin 之类的方式配置我的资源吗?

【问题讨论】:

    标签: java spring-boot spring-boot-actuator maven-resources-plugin spring-boot-starter


    【解决方案1】:

    application.properties 从类路径加载时,类路径中的第一个被加载,而类路径中的任何其他都被忽略。在您的情况下,main-app/src/main/resources/application.properties 中的文件将出现在some-starter 的 jar 中 application.properties 之前的类路径中。

    顾名思义,application.properties 用于配置您的应用程序,不应在启动程序中使用它。您应该配置应用程序中的所有属性,或者您可以更新您的启动器以包含通过spring.factories 注册的EnvironmentPostProcessor,并向Environment 添加一些默认属性。

    【讨论】:

    • 非常感谢您的回答!我要去检查EnvironmentPostProcessor。不过,如果我想在没有任何额外代码的情况下向我的启动器/库添加一些属性怎么办。我应该为属性文件使用其他名称还是可以合并主要和第 3 方属性?
    猜你喜欢
    • 2015-06-28
    • 2016-06-10
    • 2017-06-18
    • 2018-05-07
    • 1970-01-01
    • 2020-02-28
    • 2020-07-08
    • 2018-05-07
    • 2017-10-20
    相关资源
    最近更新 更多