Spring Boot Configuration and Profiles Explained (Beginner to Production Guide)

Spring Boot configuration is one of the most misunderstood areas for beginners and one of the most critical areas for production systems.

In this guide, you will learn how Spring Boot configuration and profiles actually work, why they exist, and how they are used in real-world production environments.

This article is written for:

  • Beginners learning Spring Boot
  • Developers moving to real projects
  • Engineers managing multiple environments (dev, test, prod)

What Is Configuration in Spring Boot?

Configuration in Spring Boot is the mechanism used to:

  • Externalize application settings
  • Avoid hardcoding environment-specific values
  • Run the same codebase across multiple environments

Examples of configuration values:

  • Server port
  • Database credentials
  • Logging levels
  • Feature flags
  • External service URLs

Spring Boot reads these values at startup and wires them into the application.

Why Configuration Is Critical in Production

In production systems:

  • Code should not change between environments
  • Only configuration should change

Without proper configuration:

  • Deployments become risky
  • Secrets get leaked
  • Environment-specific bugs appear

Spring Boot solves this using profiles and external configuration files.

What Are Spring Boot Profiles?

A Spring Boot profile represents a logical environment.

Common profiles:

  • dev → local development
  • test → automated testing / staging
  • prod → production

Profiles allow:

  • Different configurations per environment
  • Conditional bean loading
  • Safe separation of dev and prod behavior

One codebase → multiple environments.

How Spring Boot Loads Configuration Files

Spring Boot automatically looks for configuration files in the following formats:

  • application.properties
  • application.yml

These files live in the src/main/resources directory.

Example default file:

server.port=8080

This configuration applies when no profile is active.

Profile-Specific Configuration Files

Spring Boot supports profile-specific files using this naming convention:

  • application-dev.yml
  • application-test.yml
  • application-prod.yml

Each file overrides the default configuration when its profile is active.

Example:

server:
port: 8081

This allows each environment to have its own settings without touching code.

For a deeper comparison of file formats, see:
https://springbootfixes.com/application-properties-vs-application-yml-in-spring-boot-when-and-why-to-use-each/

How to Activate a Spring Profile

There are multiple safe ways to activate a profile.

Using application.properties

spring.profiles.active=dev

Using command line

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

Using environment variables (recommended for production)

SPRING_PROFILES_ACTIVE=prod

In production systems, environment variables are preferred.

How Configuration Is Loaded During Startup

When the application starts, Spring Boot:

  1. Determines the active profile
  2. Loads default configuration
  3. Loads profile-specific configuration
  4. Merges values (profile overrides default)
  5. Creates beans using resolved values

This startup process is explained in detail here:
https://springbootfixes.com/how-spring-boot-application-starts-startup-flow-explained/

Injecting Configuration Values

You can access configuration values using @Value or @ConfigurationProperties.

Using @Value (simple cases)

@Value("${server.port}")
private int port;

Using @ConfigurationProperties (recommended)

@ConfigurationProperties(prefix = "app")
public class AppConfig {
private String name;
}

@ConfigurationProperties is preferred for:

  • Type safety
  • Large configurations
  • Production maintainability

Profile-Based Bean Loading

Profiles can also control which beans are created.

Example:

@Profile("dev")
@Component
public class DevEmailService {}

This bean will only load when the dev profile is active.

This technique is widely used for:

  • Mock services
  • Feature toggles
  • Environment-specific integrations

Real-World Production Example

Typical setup in real projects:

  • dev → H2 database, debug logging
  • test → Test containers, controlled logging
  • prod → External DB, minimal logging, secrets via env vars

The code stays the same.
Only configuration changes.

This is how safe deployments are achieved.

Common Configuration Mistakes

Hardcoding values

Never hardcode:

  • URLs
  • Credentials
  • Ports

Using profiles incorrectly

Avoid:

  • Multiple active profiles unless necessary
  • Mixing dev and prod behavior

Storing secrets in Git

Always use:

  • Environment variables
  • Secret managers
  • External config servers

How Configuration Relates to Project Structure

Configuration files live in the resources directory, which is part of the standard Spring Boot project structure.

If you’re not clear on this, read:
https://springbootfixes.com/spring-boot-project-structure-explanation/

Summary

Spring Boot configuration and profiles allow you to:

  • Run one codebase everywhere
  • Safely manage environments
  • Avoid risky deployments
  • Build production-grade systems

Profiles are not optional — they are mandatory for real-world Spring Boot applications.

Related Spring Boot Guides

Fix Port 8080 Already in Use
https://springbootfixes.com/how-to-fix-port-8080-already-in-use-error-in-spring-boot/

Spring Boot Hello World Example
https://springbootfixes.com/spring-boot-hello-world-example-beginner-friendly/

How Spring Boot Creates Beans
https://springbootfixes.com/how-spring-boot-creates-beans-bean-lifecycle-simplified-for-production/

Leave a Comment

Your email address will not be published. Required fields are marked *