简体   繁体   English

Spring启动应用程序配置问题

[英]Spring boot application configuration question

I have following structure of my application 我有以下应用程序的结构

----
   | 
   |____SpringBootApplicationA
        |
        |
        |___module-1
        |___module-2

Each module has its own configuration, For example, module-1 is library that talks to mysql, it has its configuration (connection string, username, password,etc...) 每个模块都有自己的配置,例如,module-1是与mysql对话的库,它有自己的配置(连接字符串,用户名,密码等......)

Now I want to represent this configuration in the form of Java POJO in module-1. 现在我想在模块1中以Java POJO的形式表示此配置。 application.yml and a bean that read configuration and sets the values. application.yml和一个读取配置并设置值的bean。

For example 例如

class Module1Config {

  private String key1;
  private String key2;

  // getters & setters
}

application.yml at module-1 模块-1处的application.yml

key1: val1
key2: val2

now as a consumer of module-1, SpringBootApplicationA will receive module-1's configuration which is what set as default by module-1. 现在作为module-1的使用者, SpringBootApplicationA将接收module-1的配置,该配置由module-1设置为default。

On the consumer side it will have application configuration like this 在消费者方面,它将具有这样的应用程序配置

someKey1: someVal1
someKey2: someVal2
module-1:
  key1: overrideVal1

and when initialization happens of module-1's beans, I want the values to be seen as 当初始化发生module-1的bean时,我希望将值视为

key1: overrideVal1
key2: val2

How to configure spring boot to respect default values and override them ? 如何配置spring boot以遵循默认值并覆盖它们?


Edit 编辑

class AppConfig {

  private String key1;
  private int key2;

  private Module1Config conf;

  // getters + setters

}

This is my example application config, as you can see it has some specific to application config and other configs it is leveraging from other modules. 这是我的示例应用程序配置,因为您可以看到它具有一些特定于应用程序配置和其他模块的其他配置。

I want conf object to get assigned default set of value from module1 and then whatever application has specified as an override 我希望conf对象从module1中分配默认的值集,然后将任何应用程序指定为覆盖

Spring boot by default loads application.yml file from src/main/resources You can declare another application.yml file in config folder of root path and configuration from config folder will override configuration from src/main/resources Spring启动默认情况下从src / main / resources加载application.yml文件您可以在根路径的config文件夹中声明另一个application.yml文件,配置文件夹中的配置将覆盖src / main / resources中的配置

Config locations are searched in reverse order. 以相反的顺序搜索配置位置。 By default, the configured locations are classpath:/,classpath:/config/,file:./,file:./config/. 默认情况下,配置的位置是classpath:/,classpath:/ config /,file:./,file:./ config /。 The resulting search order is the following: 生成的搜索顺序如下:

file:./config/ file:./ classpath:/config/ classpath:/ file:./ config / file:./ classpath:/ config / classpath:/

Here is link from official documentation: 这是官方文档的链接:

https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

I think it's helpful 我认为这很有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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