Search code examples
spring-bootspring-profilesspring-properties

Understanding the spring profile


I have basic idea how spring profile works. But here in this file this - i am not able to get it. And current Application.yml file mentioning the three profile which one will get active and when that i need to know as well. Below is the Application.yml file content.

spring:
  application:
       name:
  profiles:
    active:
      -default
      -local
      -swaggerinfo

Note: i have three config files present in my resources. Also if i want to look another config file then spring use the naming convention like Application-<Name>.extension . so - is already get added for the new config file then why we explicitly need to put another one in our application.yml file under spring.profile.active.

Below are the names of the three config files present under the resources folder.

application.yml
application-local.yml
bootstrap-default.yml

Solution

  • But here in this file this - i am not able to get it. spring use the naming convention like Application-.extension . so - is already get added for the new config file then why we explicitly need to put another one in our application.yml file under spring.profile.active

    spring:
      application:
           name:
      profiles:
        active:
          -default
          -local
          -swaggerinfo
    

    The declaration of profiles are incorrect. You must either put space or should not use (-) at all.

    spring:
      profiles:
        active:
          - default
          - local
          - swaggerinfo 
    

    The Spring also supports the following way of declarations.

    spring:
      profiles:
        active: default,local,swaggerinfo
    

    or

    spring:
      profiles:
        active: 
          default
          local
          swaggerinfo
      
    

    Here default refers to application.properties file not bootstrap-default.properties. Also, You don't need to specify the default profile. Spring automatically use application.properties as default one. So, in your case it's appropriate to go with local and swaggerinfo.

    current Aplication.yml file mentioning the three profile which one will get active and when that i need to know as well.

    Let's talk about the following declaration.

    spring:
      profiles:
        active:
          - local
          - swaggerinfo
    

    Both local and swaggerinfo profiles will be active for props loading. So,which means that all the three files (application.yml by default) will be consumed by spring.

    Let's talk about the order. The order in the above case would be

    application -> application-local -> application-swaggerinfo

    Note: Assume that you've mentioned the same prop in all the three files then in that case the precedence will be given as per the order highlighted above i.e prop mentioned in the application-swaggerinfo will override the ones available in the other twos.