最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

spring boot - Specify default properties at the profile level - Stack Overflow

programmeradmin1浏览0评论

I'm trying to set up some default properties across multiple Spring Boot projects, where the projects will be using my library as a bundled jar file.

I see there's a spring.config.additional-location setting but it loads those properties after the ones in application-(profile).yml which is the opposite of what I want; I want to specify defaults-(profile).yml in my project and have all of the other projects inherit those as their defaults with the ability to override them in application.yml (or application.properties, or application-(profile).yml, or from the command line, etc.). It's imperative that all of the default Spring Boot property loading works the same way it does now with the exception of being able to load these additional profile-specific defaults.

How can I do this? Neither Google nor ChatGPT were of any help.

EDIT: In case it helps, my library is Spring Boot enabled (or whatever the term is): It automatically configures beans that all of my clients use.

I'm trying to set up some default properties across multiple Spring Boot projects, where the projects will be using my library as a bundled jar file.

I see there's a spring.config.additional-location setting but it loads those properties after the ones in application-(profile).yml which is the opposite of what I want; I want to specify defaults-(profile).yml in my project and have all of the other projects inherit those as their defaults with the ability to override them in application.yml (or application.properties, or application-(profile).yml, or from the command line, etc.). It's imperative that all of the default Spring Boot property loading works the same way it does now with the exception of being able to load these additional profile-specific defaults.

How can I do this? Neither Google nor ChatGPT were of any help.

EDIT: In case it helps, my library is Spring Boot enabled (or whatever the term is): It automatically configures beans that all of my clients use.

Share edited Mar 7 at 13:21 Blake M. asked Mar 6 at 21:08 Blake M.Blake M. 1241 silver badge11 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Here is how I would do it:

  1. In your library define a property file - e.g. default-properties.yml:
some-property=foo
  1. In your client projects (that have that library as a dependency, import the property source):
@PropertySource("classpath:default-properties.yml")
@SpringBootApplication
public class FooApplication {}

now if your client project doesn't define the property some-property its default value is foo, if your client project does define it - it will get overwritten.


This works because @PropertySource has a lower order of evaluation that the properties files, therefore property files can overwrite it - see externalized configuration

发布评论

评论列表(0)

  1. 暂无评论