Spring Boot Profiling – Configure for Different Environments

Spring Boot Profiling provides a way to segregate parts of your application configuration and make it only available in the certain environment. It is one of those concepts which we don’t use if we are working in development or just for learning purpose. Spring Boot Profiling is needed for applications which need’s to load different configurations for different environments Such as QA, DEV OR PROD.

In this Post, we will see how we use different database URL for different environments and let spring boot load the Url based on environment specified or profiling mentioned.

In order to demonstrate Spring Boot Profiling, we will create two different property files for prod and QA environments. I am using PostgreSQL for dev environment and MySQL for prod environment. Let us Check the Project structure below.

Project Structure

spring boot profiles

Let’s check property files for different environments, we are using different property files for different config in our application. Let’s configure database URL for those property files.

Configure Spring Profiles using application.properties

spring.jpa.hibernate.ddl-auto=create
spring.datasource.url=jdbc:mysql://localhost:3308/profiling
spring.datasource.username=root
spring.datasource.password=root
spring.profiles.active=prod

​We can also specify active profiles inapplication.properties and replace them using the command line switch. Using spring.profiles.active environment variable we can actually set an active profile from command line .

We can also specify multiple active spring boot profiles, for example:- spring.profiles.active=dev, prod in our properties file.

application-prod.properties

spring.jpa.hibernate.ddl-auto=create
spring.datasource.url=jdbc:mysql://localhost:3306/app2
spring.datasource.username=root
spring.datasource.password=root

application-dev.properties

spring.jpa.hibernate.ddl-auto=create

spring.datasource.url=jdbc:postgresql://localhost:5432/appointment
spring.datasource.username=postgres
spring.datasource.password=root
spring.datasource.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.datasource.driver.classname=org.postgresql.Driver

DemoApplication.java:-

package com.profiling.demo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication implements CommandLineRunner {

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

	@Value(value = "${spring.datasource.url}")
	String dbName;
	
	@Override
	public void run(String... arg0) throws Exception {
		// TODO Auto-generated method stub
		
		
		System.out.println("******************"+dbName+"************************");
	}
}

Here we are printing out the database URL which is getting loaded in Spring Boot Profiling.

Programmatically Getting Active Profiles In Spring Boot

There are use cases where we need to retrieve the active profiles of a spring boot app . We can get this by autowiring the bean org.springframework.core.env.Environment . Have a look at the sample code .

@Autowired
private Environment environment;

private String[] getActiveProfile(){
this.environment.getActiveProfiles();
}

It returns you the list of active as well as default profiles.

Running the Application

Navigate to the Project directory and run mvn clean install .

Now type this command mvn spring-boot: run, once you run this maven command you will see something like this below:-

spring profiling

You can see our prod database Url is now loaded by default. Now let’s assume I am in development machine and want to run development database config, so I need to run my application in development mode. Please run following command line switch to switch to your dev profile.

spring boot profiling
spring boot profiling yml

output:-

Here the Postgresql database url must be loaded if everything is correct.

spring boot profiling

This is how we do profiling in a real-world application, spring also provides the Programmatic way of doing profiling. Please check out this link to find about Programmatic Way of Spring Boot Profiling. We can encrypt spring application properties and use profiling there as well .

Have a look how we can Encrypt Spring boot Application Properties

Check this Book below to Learn More Basics Of Spring Boot .

It’s Suggested to do and apply configurations to deployment .

Related Links:-

Creating REST Service with Spring Boot

Spring MVC Basic Setup and Hello World
Custom ClassLoaders In Java
Overload Or Override Static Methods in Java