Spring Boot Basics - What Is A Profile?


When we develop applications, we would want to be able to provide different application configuration in different environments? How do you achieve it with Profiles?

You will learn

  • What is a Profile?
  • How do you provide different application configuration in different environments using Profiles?
  • How do you activate beans based on Profiles?
  • What is the default profiles with Spring Boot?

What Is A Profile?

When we create an application, we need to deploy it in different kinds of environments, such as development, QA, staging and production.

The application configuration in each of these environments will be different.

One of the approaches to handling application configuration is to create something called a profile. Spring Boot has the concept of a profile built in.

A Profile answers the question of “How to have different configurations in different environments?”

Spring Boot Profiles

You can define default configuration in application.properties. Environment specific overrides can be configured in specific files:

  • application-dev.properties
  • application-qa.properties
  • application-stage.properties
  • application-prod.properties

Setting A Spring Boot Profile

Here are a couple of ways of setting the active profile:

  • At the time of launching the Java application
    • -Dspring.profiles.active=qa - in the VM properties, OR
  • Do the following in the application.properties file
    • spring.application.profiles=qa.

Depending on which profile is currently the active, the appropriate configuration is picked up.

Using Profiles In Code

A profile can be used in code to define your beans. For example, have a look at the following piece of code:


	@Profile("dev")
	@Bean
	public String devBean() {
		return "I will be available in profile dev";
	}

	@Profile("prod")
	@Bean
	public String prodBean() {
		return "I will be available in profile prod";
	}

The bean devBean() will only be available with the dev profile, as it has been annotated with @Profile("dev"). Similarly, the bean prodBean() is only available with the profile prod.

Do check out our video on the same topic:

image info

Summary

Spring Boot Profiles provides a simple option to have environment specific configuration.

Related Posts

Spring Boot Tutorials for Beginners

At in28Minutes, we are creating a number of tutorials with videos, articles & courses on Spring Boot for Beginners and Experienced Developers. This resources will help you learn and gain expertise at Spring Boot.

Microservices Architectures - What is Fault Tolerance?

In this article, we discuss an important property of microservices, called fault tolerance

Microservices Architectures - Introduction to Auto Scaling

In this article, we focus our attention on the Dynamic Scaling or Auto Scaling. Why do we need applications that can auto scale?

Microservices Architectures - Importance of Centralized Logging

In this article, we explore the concept of centralized logging, in the context of microservices.

Microservices Architectures - Introduction to API Gateway

In this article, we look at what an API Gateway is, in the context of microservices.

Microservices Architectures - Centralized Configuration and Config Server

In this article, we explore the concept of centralized configuration, in the context of microservices.

Microservices Architectures - What is Service Discovery?

In this article, we talk about Service Discovery. Why is Service Discovery important in microservices architecture, and how to set it up for your application.

Microservices Architectures - Advantages Of Microservices

Microservices architectures are very popular today. In this article, we discuss the three main advantages of having a microservices architecture.

Microservice Architectures - Challenges with building Microservices

Microservices architectures are very popular today. In this article, we discuss the important challenges in developing a microservices architecture.

Microservices Architectures - Microservices vs SOA

Microservices architectures are very popular today. In this article, we take a look at how microservices architectures are different from Service Oriented Architectures (SOA).