[英]Property order in Spring Boot
I want to write my own Spring Boot starter and let it inherit from another starter (let's say spring-boot-starter-batch). 我想编写自己的Spring Boot启动程序,并使其从另一个启动程序继承(比如spring-boot-starter-batch)。 Now I want to set one of spring-boot-starter-batch properties to a default that differs from spring-boot-starter-batch's default (for example
spring.batch.job.enabled=false
instead of true). 现在,我想将spring-boot-starter-batch属性之一设置为与spring-boot-starter-batch的默认值不同的默认值(例如
spring.batch.job.enabled=false
而不是true)。 The user of my starter shall still be able to override it via application.properties
. 我的入门用户仍然可以通过
application.properties
覆盖它。
That's not really possible, right? 那真的不可能,对吧? The order for reading properties is the following:
读取属性的顺序如下:
System.getProperties()
). System.getProperties()
)。 java:comp/env
java:comp/env
JNDI属性 RandomValuePropertySource
that only has properties in random.*
. RandomValuePropertySource
,仅具有random.*
属性。 @PropertySource
annotations on your @Configuration
classes. @Configuration
类上的@PropertySource
批注。 application.properties
including YAML and profile variants). application.properties
)。 application.properties
including YAML and profile variants). application.properties
)。 SpringApplication.setDefaultProperties
). SpringApplication.setDefaultProperties
指定)。 So if I use @PropertySource
in my starter to set the property, the user of my starter won't be able to override it via application.properties
. 因此,如果我在启动器中使用
@PropertySource
设置属性,则启动器的用户将无法通过application.properties
覆盖它。 Wouldn't it make sense to change the order and set @PropertySource
at number 8? 更改顺序并将
@PropertySource
设置为数字8是否合理?
Or is there a way to achieve what I want? 还是有办法实现我想要的?
The order of @PropertySource
is definitely up for debate. @PropertySource
的顺序肯定@PropertySource
辩论。 Before that debate though you should be able to add your own property sources in a listener or initializer (like the existing config file listener). 在辩论之前,尽管您应该能够在侦听器或初始化器中添加自己的属性源(例如现有的配置文件侦听器)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.