Mastering Spring Boot Profiles: A Deep Dive into Environment-Specific Configuration

Master Spring Ter
3 min readJun 16, 2024

Spring Boot is renowned for its simplicity and powerful features, and one of its most valuable features is the concept of profiles. Profiles allow you to define environment-specific configurations, enabling your application to adapt seamlessly to different environments like development, testing, and production. In this article, we’ll explore how to effectively use Spring Boot profiles to manage your application’s configuration and enhance its flexibility.

Understanding Spring Boot Profiles

Spring Boot profiles provide a way to segregate parts of your application configuration and make it available only in certain environments. This is particularly useful when you have different settings for various environments, such as database URLs, logging levels, or API keys.

Setting Up Profiles

To get started with profiles, you need to define different configuration files for each environment. Spring Boot uses a simple naming convention: application-{profile}.properties or application-{profile}.yaml.

Example: Creating Configuration Files

# application.properties
spring.profiles.active=dev

# application-dev.properties
server.port=8080
spring.datasource.url=jdbc:mysql://localhost:3306/dev_db
spring.datasource.username=dev_user
spring.datasource.password=dev_pass

# application-prod.properties
server.port=80
spring.datasource.url=jdbc:mysql://prod-db-server:3306/prod_db
spring.datasource.username=prod_user
spring.datasource.password=prod_pass

In the application.properties file, the spring.profiles.active property specifies which profile is currently active. You can change this value to switch between environments.

Using @Profile Annotation

Spring Boot also allows you to conditionally load beans based on the active profile using the @Profile annotation. This can be particularly useful for defining beans that should only be available in certain environments.

Example: Conditional Bean Loading with @Profile

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

@Configuration
public class DataSourceConfig {

@Bean
@Profile("dev")
public DataSource devDataSource() {
return DataSourceBuilder.create()
.url("jdbc:mysql://localhost:3306/dev_db")
.username("dev_user")
.password("dev_pass")
.build();
}

@Bean
@Profile("prod")
public DataSource prodDataSource() {
return DataSourceBuilder.create()
.url("jdbc:mysql://prod-db-server:3306/prod_db")
.username("prod_user")
.password("prod_pass")
.build();
}
}

In this example, the devDataSource bean will only be loaded if the dev profile is active, and the prodDataSource bean will only be loaded if the prod profile is active.

Switching Profiles at Runtime

You can switch profiles without changing the configuration file by passing a JVM argument when running your application.

Example: Switching Profiles via JVM Argument

java -jar myapp.jar --spring.profiles.active=prod

This approach is useful for deployment scripts and CI/CD pipelines where you need to control the active profile dynamically.

Advanced Profile Management with YAML

Spring Boot also supports YAML for defining profiles, which can make managing complex configurations easier. Here’s how you can define multiple profiles in a single YAML file.

Example: Using YAML for Profile Configuration

# application.yaml
spring:
profiles:
group:
dev: dev, common
prod: prod, common

---
spring.config.activate.on-profile: dev
server:
port: 8080
spring:
datasource:
url: jdbc:mysql://localhost:3306/dev_db
username: dev_user
password: dev_pass

---
spring.config.activate.on-profile: prod
server:
port: 80
spring:
datasource:
url: jdbc:mysql://prod-db-server:3306/prod_db
username: prod_user
password: prod_pass

---
spring:
profiles: common
logging:
level:
org.springframework: DEBUG

In this example, the YAML file is divided into sections using ---, where each section defines settings for a specific profile. The spring.config.activate.on-profile property is used to specify the profile to which the configuration belongs.

Conclusion

Spring Boot profiles provide a powerful way to manage environment-specific configurations, making your application more flexible and easier to maintain. By leveraging profiles, you can ensure that your application behaves correctly in different environments without the need for manual configuration changes.

Embrace the power of Spring Boot profiles to streamline your development process and create robust, environment-aware applications. Happy coding!

This tutorial was generated using ChatGPT, specifically the Master Spring TER model. For more information, visit ChatGPT Master Spring TER.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Master Spring Ter
Master Spring Ter

Written by Master Spring Ter

https://chatgpt.com/g/g-dHq8Bxx92-master-spring-ter Specialized ChatGPT expert in Spring Boot, offering insights and guidance for developers.

No responses yet

Write a response