简体   繁体   English

Spring Boot中的属性顺序

[英]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: 读取属性的顺序如下:

  1. Command line arguments. 命令行参数。
  2. Java System properties ( System.getProperties() ). Java系统属性( System.getProperties() )。
  3. OS environment variables. 操作系统环境变量。
  4. JNDI attributes from java:comp/env 来自java:comp/env JNDI属性
  5. A RandomValuePropertySource that only has properties in random.* . 一个RandomValuePropertySource ,仅具有random.*属性。
  6. @PropertySource annotations on your @Configuration classes. @Configuration类上的@PropertySource批注。
  7. Application properties outside of your packaged jar ( application.properties including YAML and profile variants). 打包jar之外的应用程序属性(包括YAML和配置文件变体的application.properties )。
  8. Application properties packaged inside your jar ( application.properties including YAML and profile variants). 打包在jar中的应用程序属性(包括YAML和配置文件变体的application.properties )。
  9. Default properties (specified using 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM