What is Spring Boot Profiles ?

Khushi Vashishtha
CodeX
Published in
4 min readMay 25, 2024

In the lifecycle of a software project, several stages are typically involved, including Development, Testing, UAT (User Acceptance Testing), Pre-Production, and Production. Each stage requires a distinct environment configuration, encompassing server settings, database connections, and other critical parameters.

Manually managing these configurations for each environment can be cumbersome and error-prone, potentially leading to inconsistencies and deployment issues. To address these challenges, Spring Boot offers a streamlined and automated solution in the form of Spring Boot Profiles.

Spring Boot Profiles enable developers to define environment-specific configurations, facilitating seamless transitions between different stages of the software lifecycle. By leveraging profiles, you can ensure that your application runs with the appropriate settings for each environment, enhancing efficiency and reducing the risk of configuration errors.

The following steps outline how to effectively use Spring Boot Profiles in your application:

  1. Create Separate Properties Files for Each Environment

To manage configurations effectively for different environments, Spring Boot allows you to create separate properties files tailored to each environment. This practice ensures that your application has the correct settings based on the environment it is running in, such as development, testing, or production.

Properties File Naming Convention: The properties files should follow a specific naming convention to differentiate between environments:

  • application.properties: Default properties file, used when no specific profile is active.
  • application-dev.properties: Configuration for the Development environment.
  • application-test.properties: Configuration for the QA (Quality Assurance) environment.
  • application-prod.properties: Configuration for the Production environment.

2. Specify the Active Profile

Specifying the active profile in Spring Boot is crucial because it dictates which set of configuration properties the application will use at runtime. This flexibility allows you to seamlessly switch between different environments (such as development, testing, and production) without manually altering configuration files. Here’s how you can set the active profile:

2.1. Using application.properties

You can set the active profile in the application.properties file, which is the default properties file used by Spring Boot. This method is straightforward and effective for environments where you control the properties file.

application.properties file

When you specify spring.profiles.active=dev in application.properties, Spring Boot will load properties from application-dev.properties.

Pros:

  • Simple to configure.
  • Easy to manage for local development.

Cons:

  • Requires modification of the properties file when changing environments, which might not be ideal for automated deployments.

2.2. Using System Properties

You can set the active profile programmatically within your Spring Boot application’s main class. This is useful when you need to control the profile dynamically or when deploying in environments where modifying properties files is not feasible.

System Properties

By calling System.setProperty(“spring.profiles.active”, “dev”) before running the application, you ensure that the dev profile is active.

Pros:

  • Dynamic control over the active profile.
  • Can be used in combination with other deployment scripts or tools.

Cons:

  • Requires code modification if the profile needs to be changed frequently.

2.3. Using Application Arguments

· You can specify the active profile as an application argument when starting your Spring Boot application. This is particularly useful for CI/CD pipelines and automated deployments.

· Example:

· Application Arguments: –-spring.profiles.active=dev

By adding — spring.profiles.active=dev as an argument, you instruct Spring Boot to activate the dev profile.

Pros:

  • No need to modify the code or properties files.
  • Ideal for automated and scripted deployments.

Cons:

  • Requires passing arguments explicitly every time the application is run.

2.4. Using VM Arguments

· Similar to application arguments, you can set the active profile using JVM arguments. This method is convenient for environments where you manage JVM settings directly, such as application servers or IDE run configurations.

· Example:

VM Arguments: — -spring.profiles.active=dev

Add -Dspring.profiles.active=dev as a JVM argument when running your application.

Pros:

  • Easily configurable in IDEs and server configurations.
  • No code or properties file changes required.

Cons:

  • Requires proper configuration management in environments where JVM settings are specified.

3. Run the application

EX for profiles Application :

1. Create properties files under resources folder

[application.properties] ( common to all properties )

common to all properties file

[application-dev.properties]

For Development

[application-prod.properties]

For Production

2. Create a bean class

[ProfileEnvironment.java] -> Create variables and their getter setter

spring boot class with getter and setter

3.Create Controller class

[ProfilesController.java]

controller class

4. Springbootapp19Application.java (main method)

Main class

5. Run the Application

Output url [http://localhost:1234/profiles]

Output of the application

--

--

Khushi Vashishtha
CodeX
Writer for

CS student | Java Developer | Tech Blogger @ Medium | Sharing Java insights, tutorials, and tips.