Spring boot application configuration question

user3833308 :

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...)

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.

For example

class Module1Config {

  private String key1;
  private String key2;

  // getters & setters
}

application.yml at module-1

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.

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

key1: overrideVal1
key2: val2

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


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

Mykhailo Moskura :

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

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:

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

I think it’s helpful

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=89328&siteId=1