“Spring Boot在依赖的application.properties文件中无法正常工作”

huangapple go评论72阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年8月8日 00:30:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/63305836.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定