Spring boot 应用配置问题 [英] Spring boot application configuration question

查看:39
本文介绍了Spring boot 应用配置问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序结构如下

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

每个模块都有自己的配置,例如module-1是与mysql对话的库,它有自己的配置(连接字符串、用户名、密码等...)

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中以Java POJO的形式来表示这个配置.application.yml 和一个读取配置并设置值的 bean.

Now I want to represent this configuration in the form of Java POJO in module-1. application.yml and a bean that read configuration and sets the values.

例如

class Module1Config {

  private String key1;
  private String key2;

  // getters & setters
}

application.yml 在 module-1

application.yml at module-1

key1: val1
key2: val2

现在作为module-1的消费者,SpringBootApplicationA将接收module-1的配置,这是module-1默认设置的.

now as a consumer of module-1, SpringBootApplicationA will receive module-1's configuration which is what set as default by module-1.

在消费者方面,它将具有这样的应用程序配置

On the consumer side it will have application configuration like this

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

并且当模块 1 的 bean 发生初始化时,我希望这些值被视为

and when initialization happens of module-1's beans, I want the values to be seen as

key1: overrideVal1
key2: val2

如何配置 spring boot 以尊重默认值并覆盖它们?

How to configure spring boot to respect default values and override them ?

编辑

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.

我希望 conf 对象从 module1 获得分配的默认值集,然后任何应用程序指定为覆盖

I want conf object to get assigned default set of value from module1 and then whatever application has specified as an override

推荐答案

Spring boot 默认加载 application.yml 文件来自 src/main/resources您可以在中声明另一个 application.yml 文件根路径和配置的 config 文件夹来自config 文件夹将覆盖来自 src/main/resources 的配置

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

以相反的顺序搜索配置位置.默认情况下,配置的位置是 classpath:/,classpath:/config/,file:./,file:./config/.结果搜索顺序如下:

Config locations are searched in reverse order. By default, the configured locations are classpath:/,classpath:/config/,file:./,file:./config/. The resulting search order is the following:

文件:./config/文件:./类路径:/配置/类路径:/

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

我觉得有用

这篇关于Spring boot 应用配置问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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