Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot: Load common config application properties file using bootstrap.yml

In our config server we have following application properties files:

helloApp-dev.properties //dev env properties
helloApp-commonConfig.properties //common properties across env

The properties are accessible from URI like:

https://myapp.abc.com/1234/helloApp-dev.properties
https://myapp.abc.com/1234/helloApp-commonConfig.properties

Below is our bootstrap.yml of helloApp application:

---
spring:
  application:
    name: helloApp
    
---
spring  
  profiles: dev
  cloud:
    config:
      label: 1234
      name: {spring.application.name}
      uri: https://myapp.abc.com/

I am using Spring boot version 2.2.4. The helloApp-dev.properties are loading successfully in application but not commonConfig.

like image 270
Sarthak Srivastava Avatar asked Sep 06 '25 02:09

Sarthak Srivastava


2 Answers

The configuration in commonConfig is not being loaded because you are not indicating to Spring that it should load that profile/config, because you are activating only the dev profile - and the configuration for that profile is the one which is being loaded:

---
spring  
  profiles: dev

In addition to the good solutions proposed by @akozintsov in his/her answer, if you need to include certain common configuration in different environments or profiles, let's say dev, qa or prod, you can use the spring.profiles.include configuration property and include, as a comma separated list of values, if using properties files, or as a list, if using yaml, the different common profiles and corresponding configurations you need to.

In your example, in helloApp-dev.properties you need to include the following information within the rest of your configuration:

spring.profiles.include=commonConfig

These related SO question and this article could be of help as well.

like image 50
jccampanero Avatar answered Sep 08 '25 16:09

jccampanero


To load properties file, profiles should match. You have 2 solutions:

  1. Rename helloApp-commonConfig.properties -> helloApp.properties
  2. Use multiple profiles for your application (dev, commonConfig)
like image 40
akozintsov Avatar answered Sep 08 '25 17:09

akozintsov