英文:
Spring Boot not working with dependent application.properties
问题
我正在使用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
的方式来配置我的资源?
英文:
I'm working with the 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:
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
).
But it doesn't. However, it works in case of the same properties in my main app main-app/src/main/resources/application.properties
.
Is it problem with the property overriding in Spring Boot? Should i configure my resources via something like maven-resources-plugin
?
答案1
得分: 2
当从类路径加载application.properties
时,首先加载类路径上的第一个文件,忽略类路径上的其他文件。在你的情况下,位于main-app/src/main/resources/application.properties
的文件将在some-starter
的JAR文件中的application.properties
之前出现在类路径上。
正如其名称所示,application.properties
用于配置你的应用程序,不应该在starter中使用。你应该在你的应用程序中配置所有属性,或者你可以更新你的starter,包含一个通过spring.factories
注册的EnvironmentPostProcessor
,并将一些默认属性添加到Environment
中。
英文:
When application.properties
is loaded from the classpath, the first one on the classpath is loaded and any others on the classpath are ignored. 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
.
As its name suggests, application.properties
is intended for configuring your application and it shouldn't be used in a starter. 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
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论