4

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.

Sri
  • 437
  • 1
  • 4
  • 13
Sarthak Srivastava
  • 1,490
  • 3
  • 15
  • 33
  • So basically, the `helloApp-dev.properties` gets picked up because of the profile mentioned in boostrap.yaml, i.e., `profiles: dev`. If you rename as suggested in an answer below: `helloApp-commonConfig.properties -> helloApp.properties` and keep `helloApp-dev.properties` as it is, then `helloApp.properties` will be shared across all profiles – Nagaraj Tantri Feb 09 '22 at 02:09
  • Doesn't this answer you question: https://stackoverflow.com/questions/25855795/spring-boot-and-multiple-external-configuration-files ? – pringi Feb 09 '22 at 15:00

2 Answers2

2

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)
akozintsov
  • 21
  • 1
2

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.

jccampanero
  • 50,989
  • 3
  • 20
  • 49