Hello! In our last lesson, we dove deep into the Spring IoC container's startup process, using the debugger to trace component scanning and the bean lifecycle. You saw firsthand how Spring finds and assembles your application components.
Today, we'll tackle the next logical step: external configuration. Hardcoding values like server ports or logging levels directly into our code is inflexible and a bad practice. Real-world applications must behave differently in development, staging, and production environments, and this is managed through configuration.
This lesson covers how to configure your Spring Boot application for different environments using YAML files and a core Spring feature called Profiles. This is a foundational skill for building production-ready systems.
1. Why Externalized Configuration Matters
In the MERN world, you're likely accustomed to using .env files to store environment-specific variables like database connection strings, API keys, and the NODE_ENV flag. This practice follows a key principle of the 12-Factor App methodology: strictly separate configuration from code.
Spring Boot embraces this principle wholeheartedly. The goal is to build a single, deployable artifact (a JAR file) that can run in any environment without code changes. The configuration is supplied externally at runtime.
2. Upgrading to YAML for Better Structure
By default, Spring Boot projects start with an application.properties file. We used it in the last lesson to expose Actuator endpoints. While functional, it's a flat key-value store, which can become unwieldy in large applications.
A more modern and readable alternative is YAML (.yml), which uses indentation to create a hierarchical structure.
MERN Comparison
application.properties: Similar to a simple, flat.envfile where every key is fully specified (e.g.,DATABASE_HOST=...,DATABASE_PORT=...).application.yml: More like a structured JSON or YAML configuration file. It allows you to group related properties, much like a JavaScript object, making the configuration easier to read and manage.

To understand the benefits more clearly, let's watch a short segment of the following video.
application.properties vs application.yaml : Java Spring Boot Interview Q13
The video 'application.properties vs application.yaml' clearly explains why YAML's hierarchical structure is superior for organizing complex configurations.
Watch from 00:55 to 02:51. The presenter compares how properties like server port and database settings are defined in both formats, highlighting the organizational benefits of YAML.
Hands-On: Migrating to application.yml
Let's apply this to our project.
-
In
src/main/resources/, renameapplication.propertiestoapplication.yml. -
If you have existing properties, convert them to YAML format. For example, the Actuator setting from our last lesson:
From (
.properties):management.endpoints.web.exposure.include=health,info,beansTo (
.yml):management: endpoints: web: exposure: include: health,info,beansNote: YAML is sensitive to indentation. Use spaces, not tabs.
-
Add a server port configuration and restart your application. You should see in the logs that Tomcat starts on the new port.
server: port: 8081 management: endpoints: web: exposure: include: health,info,beans
3. Managing Environments with Spring Profiles
In a real project, you'll have at least a development and a production environment, each with different needs.
- Development (
dev): You might want verbose logging (DEBUG), a local database, and perhaps some features disabled. - Production (
prod): You need stricter logging (WARNorERROR), a connection to the production database, and optimized performance settings.
Spring Profiles are the standard mechanism for handling this. A profile is simply a label for a set of configurations. You can have profile-specific configuration files (application-dev.yml, application-prod.yml) or, more conveniently, define all profiles within a single application.yml file.
This is achieved using a triple-dash (---) separator, which signifies a new document within the same YAML file.
Let's see how this works in practice.
application.properties vs application.yaml : Java Spring Boot Interview Q13
The same video from 'The Curious Coder' also has an excellent demonstration of using a multi-document YAML file to manage different profiles.
Watch from 02:51 to 07:57. This segment shows exactly how to structure a single application.yml file with sections for dev, test, and prod profiles and how to activate them.
Hands-On: Creating dev and prod Profiles
Let's modify our application.yml to define different settings for our dev and prod environments.
-
Structure your
application.ymlas follows. The first section (before any---) contains default properties that apply to all profiles unless overridden.# Default properties (applies to all profiles) server: port: 8080 # Default port spring: application: name: blog-platform --- # Development profile spring: config: activate: on-profile: dev server: port: 8081 logging: level: com.blogplatform: DEBUG --- # Production profile spring: config: activate: on-profile: prod server: port: 80 logging: level: com.blogplatform: INFO -
Activating a Profile: Now, how do we tell Spring which profile to use? There are several ways, but the most common for development and deployment are:
-
Via IDE Run Configuration (for development):
- In IntelliJ, go to
Run->Edit Configurations.... - Find the "Active profiles" field and enter
dev. - Click Apply. Now, when you run the app from your IDE, it will use the
devprofile. The console log will showTomcat started on port(s): 8081and you'll see debug logs.
- In IntelliJ, go to
-
Via Command-Line Argument (for deployment):
- When you run the packaged JAR file, you can pass the profile as an argument. This is how you'll typically run your app in a CI/CD pipeline or a production server.
java -jar your-app.jar --spring.profiles.active=prod
-
For a detailed walkthrough of setting these up, refer to the following video.
Spring Boot Profiles: Manage Dev, Staging & Production Environments Easily
This video by Philipp Lackner provides another clear walkthrough of setting up profiles and, importantly, demonstrates how to activate them through both IDE configurations and command-line arguments.
Watch from 17:56 to 22:17. This section focuses on the practical steps of setting active profiles in IntelliJ and then running the packaged JAR file with a command-line argument, which is crucial for understanding the deployment workflow.
4. Production-Grade Configuration: The Four Types
Knowing how to create profiles is only half the battle. Knowing what to put in them—and what to leave out—is what separates a hobby project from a production system. The following guide provides an excellent mental model based on industry best practices.
99% of Spring Boot Developers Get Configuration Wrong. Are You ...
The article '99% of Spring Boot Developers Get Configuration Wrong' is a fantastic guide that clarifies a point of major confusion: the difference between environment-specific config and deployment-specific config. It categorizes configuration into four distinct types.
Please read the first three sections of the article: 'The Foundation: Understanding the 12-Factor App', 'The 4 Types of Configuration', and 'The Decision Tree'. Focus on understanding the distinction between an 'environment' (dev, prod) and a 'deployment' (your laptop, a Docker container, an AWS region). This is a critical concept.
The Four Configuration Types Summarized
- Secrets (Passwords, API keys): NEVER in Git. Use Environment Variables or a secrets manager (like AWS Secrets Manager or HashiCorp Vault).
- Infrastructure Config (Database hosts, service URLs, ports): Use Environment Variables. Why? Because your production database host in the
us-east-1AWS region is different from the one ineu-west-1, even though both areprodenvironments. The code shouldn't care where it's deployed. - Application Behavior (Logging levels, connection pool sizes, timeouts): This is the perfect use case for Profile-specific YAML files (
application-dev.yml, etc.). These settings define how the app should behave in a given logical environment. - Code-Coupled Config (Application name, default server port if not specified elsewhere): This can live in the base
application.ymlfile that's checked into Git, as it's tightly coupled to the application's identity.
Hands-On: Applying Best Practices
Let's refine our configuration for an upcoming database connection. We won't connect the database yet, but we'll set up the configuration the right way.
In your application.yml, let's add placeholder properties for a database URL. Notice how we use ${VAR_NAME:default_value} syntax. This reads an environment variable named DB_HOST but falls back to localhost if it's not set. This allows the app to work seamlessly for local development while being overridable in production.
# In the default section of application.yml
spring:
application:
name: blog-platform
# --- NEW ---
datasource:
url: jdbc:postgresql://${DB_HOST:localhost}:${DB_PORT:5432}/${DB_NAME:blog_dev}
username: ${DB_USER:postgres}
password: ${DB_PASS:password}
# --- END NEW ---
With this setup:
- A developer can run the app locally without setting any environment variables.
- In production, your deployment script (or Kubernetes manifest) will set
DB_HOST,DB_PORT,DB_NAME,DB_USER, andDB_PASSas environment variables, which will automatically override the defaults.
This approach keeps secrets and infrastructure details out of your codebase, making your application portable and secure.
Conclusion
You've now mastered one of the most fundamental aspects of building production-ready Spring Boot applications. Moving configuration out of your code and managing it with profiles is a non-negotiable skill.
Key Takeaways:
- YAML (
.yml) is preferred over.propertiesfor its hierarchical structure and ability to define multiple profiles in one file. - Spring Profiles (
dev,prod, etc.) allow you to group configurations for different environments. - A profile is activated via the
spring.profiles.activeproperty, which can be set in your IDE for development or as a command-line argument for deployment. - Best practice dictates a clear separation of concerns:
- Secrets & Infrastructure: Environment Variables.
- Application Behavior: Profile-specific YAML files.
- Code-Coupled Config: Base YAML file.
Preview of the next lesson:
In our previous lessons, we encountered a NoSuchBeanDefinitionException. While we now know how to fix it, Spring Boot startup can fail for many reasons. In the next lesson, "Debugging Auto-Configuration and Startup Errors," we'll learn how to systematically diagnose these issues. We'll enable Spring Boot's debug output to see exactly what auto-configuration decisions it's making and how to intervene when those decisions are wrong.