Spring Boot Profiles

Necessity for profiles

In general, to prepare any software project we have to use the following software lifecycle phases.

Analysis
Development
Testing
Production
——
——

In the above software lifecycle phases we will provide a separate environment for each and every lifecycle phase, here the environment means the configurations like Server configurations, database configurations,…..

In the above context, configurations are varied from one environment to another environment , here to switch the project from one environment to another environment we have to provide all the configurations as per the environment, if we provide all the configurations manually then it is very much difficult for the developers and it is not giving guarantee for the configurations.

To overcome the above problem, Spring boot has provided a solution in the form of “Spring boot Profiles”.

Steps to create Profiles ?

To provide Profiles in Spring Boot applications we have to use the following steps.

  • Create a Separate properties file for each and every environment:

Properties file name format is application-profileName.properties

application.properties for default environment.
application-dev.properties for Development Environment
application-test.properties for testing Environment
application-prod.properties for Production  Environment

  • Provide the active profile:
    By using application.properties file:

spring.profiles.active=dev

By Using System Property: In Spring boot main class, before SpringApplication.run() in main() method.

    System.setProperty(“spring.profiles.active”, “dev”);

    By using application arguments and Vm Arguments:

      – – spring.profiles.active=dev

      • Run Spring Boot Application.

      Example :

      application.properties
      spring.application.name=app14
      spring.profiles.active=prod


      application-dev.properties
      spring.application.name = ICICI Application - DEV Environment
      driverClass = com.mysql.cj.jdbc.Driver
      url = jdbc:mysql://localhost:3306/ICICI-DEV-DB
      username = ICICI-DEV
      password = ICICI-DEV

      application-test.properties
      spring.application.name = ICICI Application - TEST Environment
      driverClass = com.mysql.cj.jdbc.Driver
      url = jdbc:mysql://localhost:3306/ICICI-TEST-DB
      username = ICICI-TEST
      password = ICICI-TEST

      application-prod.properties
      spring.application.name = ICICI Application - PROD Environment
      driverClass = com.mysql.cj.jdbc.Driver
      url = jdbc:mysql://localhost:3306/ICICI-PROD-DB
      username = ICICI-PROD
      password = ICICI-PROD

      AppRunner.java
      package com.durgasoft.app14.runner;

      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.boot.CommandLineRunner;
      import org.springframework.core.env.Environment;
      import org.springframework.stereotype.Component;

      @Component
      public class AppRunner implements CommandLineRunner {
      @Autowired
      private Environment env;

      @Override
      public void run(String... args) throws Exception {
      System.out.println("Environment Details");
      System.out.println("-----------------------------");
      System.out.println("Application Name : "+env.getProperty("spring.application.name"));
      System.out.println("Driver Clas. : "+env.getProperty("driverClass"));
      System.out.println("Driver URL. : "+env.getProperty("url"));
      System.out.println("User Name : "+env.getProperty("username"));
      System.out.println("Password : "+env.getProperty("password"));
      }
      }

      App14Application.java
      package com.durgasoft.app14;

      import org.springframework.boot.SpringApplication;
      import org.springframework.boot.autoconfigure.SpringBootApplication;

      @SpringBootApplication
      public class App14Application {
      public static void main(String[] args) {
      SpringApplication.run(App14Application.class, args);
      }
      }

      Output :

      Environment Details
      -----------------------------
      Application Name : ICICI Application - PROD Environment
      Driver Class. : com.mysql.cj.jdbc.Driver
      Driver URL. : jdbc:mysql://localhost:3306/ICICI-PROD-DB
      User Name : ICICI-PROD
      Password : ICICI-PROD

      If we want to provide profiles by using yaml files then we have to use the following yaml files.

      application.yml
      spring:
      profiles:
      active: dev

      application-dev.yml
      spring:
      profiles:
      activate:
      profiles-on: dev
      —------
      —-------

      application-test.yml
      spring:
      profiles:
      activate:
      profiles-on: test
      —------
      —------

      application-prod.yml
      spring:
      profiles:
      activate:
      profiles-on: prod
      —------
      —------

      Specify name to a profile

      To provide a particular name to the profile we have to use the following configuration.

      spring:
      profiles:
      activate:
      on-profile: profileName

      To provide an active profile name we have to use the following configuration.

      spring:
      profiles:
      active: ProfileName

      Example :

      application.yml
      spring:
      application:
      name: app14
      profiles:
      active: prod


      ---

      spring:
      config:
      activate:
      on-profile: dev

      application:
      name: ICICI Application - DEV Environment
      driverClass: com.mysql.cj.jdbc.Driver
      url: jdbc:mysql://localhost:3306/ICICI-DEV-DB
      username: ICICI-DEV
      password: ICICI-DEV

      ---

      spring:
      config:
      activate:
      on-profile: test

      application:
      name: ICICI Application - TEST Environment
      driverClass: com.mysql.cj.jdbc.Driver
      url: jdbc:mysql://localhost:3306/ICICI-TEST-DB
      username: ICICI-TEST
      password: ICICI-TEST

      ---

      spring:
      config:
      activate:
      on-profile: prod

      application:
      name: ICICI Application - PROD Environment
      driverClass: com.mysql.cj.jdbc.Driver
      url: jdbc:mysql://localhost:3306/ICICI-PROD-DB
      username: ICICI-PROD
      password: ICICI-PROD