Spring profiles in Spring Boot tutorial

Spring profiles are an advanced feature in Spring Boot. That's not to say they are difficult to understand -- they're actually very straightforward. However, new Spring developers often brush over the topic as profiles' utility isn't clear when working on small projects.

When to use Spring profiles?

The need for multiple active Spring profiles only becomes apparent after a project reaches a critical mass, where developers and DevOps personnel must deal with factors such as the following:

  • Multiple deployment targets with drastically different runtime requirements.
  • Multiple deployment stages, such as quality assurance (QA), user acceptance testing (UAT), pre-production and production.
  • Applications that must be tested against various back-end resources, including databases and queues.

However, despite being an advanced feature, Spring profiles are not a difficult concept to master.

How to use Spring profiles

To configure a Spring Boot application with support for Spring profiles, simply follow these three steps:

  1. Decide on the number of profiles the application must support.
  2. Create an application.properties or Spring YAML file for each profile.
  3. Decorate classes with @Profile annotations to associate them with a specific Spring profile at runtime.

Spring profiles and properties files

For example, if an application must support development, QA/UAT and production, the standard practice is to create four application property files with the following names:

  • application-dev.properties
  • application-uat.properties
  • application-prod.properties
  • application.properties

Configurations specific to each profile would go in the corresponding properties file.

Customize the active Spring profile

Profiles can also be used to select which Java classes to inject as dependencies into fields, setters or constructors at runtime.

For example, the @Profile annotations in the following code tell the Spring framework to inject the RandomNumberGenerator when the production profile is active, while the StaticNumberGenerator should be injected when the development profile is active:

interface NumberGenerator {
	public int getNumber();
}

@Component
@Profile("prod")
class RandomNumberGenerator implements NumberGenerator{
	public int getNumber() {
		return (int)(Math.random() * 10);
	}
}

@Component
@Profile("dev")
class StaticNumberGenerator implements NumberGenerator {
	public int getNumber() {
		return 7;
	}
}

How to set the active Spring profile

Spring Boot can support an unlimited number of Spring profiles, but only one can be active at a time. The standard way to set the active Spring profile is to specify it in the application's YAML or properties file, as so:

spring.profiles.active=dev

If it's not possible to edit the Spring application's property files directly, one can override the setting at runtime by parameterizing the deployment target. Common approaches to set the active Spring profile outside the code include the following:

  • Pass JVM arguments to override the internal property file.
  • Configure environment variables in a Docker container.
  • Set the active profile through an environment file passed to a Kubernetes cluster.

Regardless of the deployment target, Spring profiles make it easy to switch between active profiles and enable a single application to function properly in various environments.

Cameron McKenzie has been a Java EE software engineer for 20 years. His current specialties include Agile development; DevOps; Spring; and container-based technologies such as Docker, Swarm and Kubernetes.

View All Videos
App Architecture
Software Quality
Cloud Computing
Security
SearchAWS
Close