简体   繁体   English

Spring 引导无法使用依赖的应用程序。属性

[英]Spring Boot not working with dependent application.properties

I'm working with the Spring Boot 2.2.9.RELEASE.我正在使用 Spring Boot 2.2.9.RELEASE。 I have the main app and some plain starter (which just uses spring-actuator functionality) with some properties in its some-starter/src/main/resources/application.properties file:我在其some-starter/src/main/resources/application.properties文件中有主应用程序和一些简单的启动器(仅使用spring-actuator功能)和一些属性:

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

I've imported the starter into my main app and I believe that the healthcheck endpoint should work with the port 9000 and with the path /health (smth like that localhost:9000/health ).我已经将启动器导入到我的主应用程序中,并且我相信 healthcheck 端点应该与端口9000和路径/health一起使用(就像localhost:9000/health一样)。

But it doesn't.但事实并非如此。 However, it works in case of the same properties in my main app main-app/src/main/resources/application.properties .但是,它适用于我的主应用程序main-app/src/main/resources/application.properties中的相同属性。

Is it problem with the property overriding in Spring Boot? Spring Boot 中的属性覆盖是否有问题? Should i configure my resources via something like maven-resources-plugin ?我应该通过maven-resources-plugin类的东西配置我的资源吗?

When application.properties is loaded from the classpath, the first one on the classpath is loaded and any others on the classpath are ignored.当从类路径加载application.properties时,将加载类路径上的第一个,而忽略类路径上的任何其他属性。 In your case, the file in main-app/src/main/resources/application.properties will appear on the classpath before the application.properties in the jar of some-starter .在您的情况下, main-app/src/main/resources/application.properties中的文件将出现在some-starter的 jar 中的application.properties之前的类路径中。

As its name suggests, application.properties is intended for configuring your application and it shouldn't be used in a starter.顾名思义, application.properties旨在配置您的应用程序,不应在启动程序中使用它。 You should either configure all of the properties in your application, or you could update your starter to include an EnvironmentPostProcessor that is registered via spring.factories and adds some default properties to the Environment .您应该配置应用程序中的所有属性,或者您可以更新启动器以包含通过spring.factories注册的EnvironmentPostProcessor并向Environment添加一些默认属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM