使用 spring-boot 在库中设置默认属性 [英] Set default properties in a library with spring-boot

查看:41
本文介绍了使用 spring-boot 在库中设置默认属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 spring-boot 有许多不同的服务.我想设置一些通用的配置,但允许服务拥有自己的属性并根据需要覆盖它们.示例属性包括 spring.show_banner、管理 url 等.

I have many different services using spring-boot. I'd like to set up some configuration that is common for each, but allow the services to have their own properties and override them if they want. Example properties include spring.show_banner, management url ones, etc.

我该怎么做?如果我有以下几点:

How can I do this? If I have the following:

  • service-common 与 src/main/resources/application.yml 和默认属性
  • 带有 src/main/resources/application.yml 的 service1 和它自己的属性

我希望它们与优先的 service1 版本合并.相反,似乎只使用了在类路径中找到的第一个.

I'd like them to be merged with the service1 version taking precedence. Instead, it seems that only the first one found on the classpath is used.

(或者,使用@Configuration 类会更好,但我不确定它们是否可用于定义许多属性)

(Alternatively, using @Configuration classes would be even better, but I'm not sure they can be used to define many of the properties)

推荐答案

有几个选项可供您使用,均基于 考虑属性源的顺序.

There are several options available to you, all based on the order in which property sources are considered.

如果您的公共库负责创建 SpringApplication,它可以使用 setDefaultProperties.这些值可以被您的服务的 application.properties 覆盖.

If your common library is responsible for creating the SpringApplication it can use setDefaultProperties. These values can be overridden by your services' application.properties.

或者,您的库可以在其 @Configuration 类之一上使用 @PropertySource 来配置,例如,library.properties 作为来源.同样,这些属性可以在您的服务的 application.properties 中被覆盖.

Alternatively, your library could use @PropertySource on one of its @Configuration classes to configure, for example, library.properties as a source. Again, these properties could then be overriden in your services' application.properties.

这篇关于使用 spring-boot 在库中设置默认属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆