codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🍃

Spring Boot

8 / 62 topics
6Spring Boot Auto-Configuration7Using application.properties and application.yml8Spring Profiles
Tutorials/Spring Boot/Spring Profiles
🍃Spring Boot

Spring Profiles

Updated 2026-04-20
3 min read

Introduction

Spring Profiles are a powerful feature of the Spring Framework that allows developers to segregate parts of their application configuration and make it available only in certain environments. This is particularly useful for managing different configurations for development, testing, and production environments without having to maintain multiple separate configuration files.

In this tutorial, we will explore how to use Spring Profiles effectively in a Spring Boot application. We'll cover the basics of defining profiles, activating them, and using them to manage environment-specific configurations.

Understanding Spring Profiles

What are Spring Profiles?

Spring Profiles allow you to define beans that should only be loaded when a specific profile is active. This means you can have different sets of beans for different environments, such as development, testing, and production.

Why Use Spring Profiles?

  • Environment-Specific Configurations: Easily manage configurations that differ between environments.
  • Resource Management: Optimize resource usage by loading only necessary components for a specific environment.
  • Security: Secure sensitive information by keeping it out of the main configuration files.

Defining Spring Profiles

Step 1: Define Profile-Specific Properties

You can define properties specific to a profile in application-{profile}.properties or application-{profile}.yml files. For example:

application-dev.properties

server.port=8080
app.name=MyApp-Dev

application-prod.properties

server.port=8081
app.name=MyApp-Prod

Step 2: Define Profile-Specific Beans

You can also define beans that should only be loaded for specific profiles using the @Profile annotation.

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

@Configuration
public class AppConfig {

    @Bean
    @Profile("dev")
    public String devBean() {
        return "This is a development bean";
    }

    @Bean
    @Profile("prod")
    public String prodBean() {
        return "This is a production bean";
    }
}

Activating Spring Profiles

Step 1: Activate Profiles via Application Properties

You can activate profiles by setting the spring.profiles.active property in your main application.properties or application.yml file.

application.properties

spring.profiles.active=dev

Step 2: Activate Profiles via Command Line

You can also activate profiles when starting your Spring Boot application using command-line arguments:

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

Step 3: Activate Profiles via Environment Variables

Another way to activate profiles is by setting the SPRING_PROFILES_ACTIVE environment variable.

export SPRING_PROFILES_ACTIVE=dev
java -jar myapp.jar

Best Practices for Using Spring Profiles

  1. Keep Configurations Separate: Use separate files for different environments to keep your configurations organized.
  2. Avoid Hardcoding Sensitive Information: Store sensitive information like API keys or database credentials in environment variables or a secure vault.
  3. Use Default Profile: Define a default profile that contains common configurations used across all environments.
  4. Test Your Configurations: Ensure that your application behaves as expected when different profiles are active.

Real-World Example

Let's walk through a real-world example where we use Spring Profiles to manage database configurations for development and production environments.

Step 1: Define Database Properties

application-dev.properties

spring.datasource.url=jdbc:mysql://localhost:3306/dev_db
spring.datasource.username=dev_user
spring.datasource.password=dev_password

application-prod.properties

spring.datasource.url=jdbc:mysql://prod-db-server:3306/prod_db
spring.datasource.username=prod_user
spring.datasource.password=prod_password

Step 2: Create a Database Configuration Class

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import javax.sql.DataSource;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

@Configuration
public class DataSourceConfig {

    @Bean
    @Profile("dev")
    public DataSource devDataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setUrl("jdbc:mysql://localhost:3306/dev_db");
        dataSource.setUsername("dev_user");
        dataSource.setPassword("dev_password");
        return dataSource;
    }

    @Bean
    @Profile("prod")
    public DataSource prodDataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setUrl("jdbc:mysql://prod-db-server:3306/prod_db");
        dataSource.setUsername("prod_user");
        dataSource.setPassword("prod_password");
        return dataSource;
    }
}

Step 3: Activate the Profile

You can activate the profile when running your application:

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

Conclusion

Spring Profiles provide a flexible and powerful way to manage environment-specific configurations in a Spring Boot application. By using profiles, you can keep your configuration organized, secure, and optimized for different environments. This tutorial has covered the basics of defining and activating profiles, as well as best practices for their use.

By following these guidelines and examples, you should be able to effectively manage your Spring Boot application's configurations across multiple environments.


PreviousUsing application.properties and application.ymlNext Components and Beans in Spring Boot

Recommended Gear

Using application.properties and application.ymlComponents and Beans in Spring Boot