Spring Boot Configuration & Profiles (Beginner Guide)
Configure Spring Boot the right way: externalize settings in application.yml, read them with @Value, use dev/prod profiles per environment, and keep secrets in env variables.
Your app needs settings: a database URL, an email password, a server port, feature flags. And those settings differ between your laptop and the live server. Hardcoding them is a recipe for disaster (and leaked secrets!). Spring Boot solves this cleanly with external configuration and profiles. This lesson shows you how to configure your app properly — the way real production systems do it.
Externalising configuration
Instead of burying settings in your code, Spring Boot reads them from a configuration file: application.properties or application.yml (YAML — cleaner for nested settings). Both live in src/main/resources. Here's the same config in each format:
# application.properties
server.port=8081
spring.datasource.url=jdbc:postgresql://localhost:5432/mydb
app.page-size=20
# application.yml — same thing, nested
server:
port: 8081
spring:
datasource:
url: jdbc:postgresql://localhost:5432/mydb
app:
page-size: 20
Change a setting here, and you never touch your Java code. That separation of config from code is a core principle of well-built applications.
Reading config values in your code
You met @Value earlier — it injects a single setting. For groups of related settings, @ConfigurationProperties maps a whole section onto a class:
// inject a single value, with a default after the colon @Value("${app.page-size:20}") private int pageSize;
Profiles: different settings per environment
Here's the powerful part. A profile is a named set of configuration for a particular environment — typically dev (your machine) and prod (the live server). You create a base application.yml plus one file per profile:
| File | Used when… |
|---|---|
application.yml |
always (shared/base settings) |
application-dev.yml |
the dev profile is active |
application-prod.yml |
the prod profile is active |
The base file loads first, then the active profile's file overrides matching values. So dev might point at a local database while prod points at the real one — same code, different config. You choose the active profile with a setting or environment variable:
# pick the active profile SPRING_PROFILES_ACTIVE=prod # or in application.yml: spring: profiles: active: dev
dev profile runs on port 8081 against a local PostgreSQL database, while its prod profile runs against the live database with production email providers — all selected by SPRING_PROFILES_ACTIVE. Same codebase, deployed everywhere, configured per environment. That's the whole point of profiles.
Keeping secrets OUT of your config files
This is critical. Passwords, API keys, and database credentials must never be committed to your code repository — that's one of the most common and dangerous security mistakes. Instead, read them from environment variables:
spring:
datasource:
username: ${DB_USERNAME} # read from the environment
password: ${DB_PASSWORD} # NOT written in the file
The ${DB_PASSWORD} syntax tells Spring to pull the value from an environment variable at runtime. Your config file stays safe to commit; the actual secret lives only in the server's environment (or a local .env file you never commit).
.env to your .gitignore. A leaked credential in a public repo can be exploited within minutes — take this seriously from day one.
Honestly, I learned the importance of this the slightly hard way — like many developers, I had credentials sitting in config early on before tightening things up. Now, environment variables for every secret is second nature. The mental model that helps: your code and its configuration are separate things, and its secrets are separate again. Code goes in Git, config files (without secrets) go in Git, and secrets live only in the environment. Profiles let one codebase behave correctly everywhere from my laptop to production. Get this structure right early and you'll avoid a whole category of painful, sometimes costly, mistakes.
Key takeaways
- Externalise settings into
application.properties/application.yml— never hardcode them. - Read values with
@Value(single) or@ConfigurationProperties(grouped). - Profiles (
application-dev.yml,application-prod.yml) give per-environment config; select withSPRING_PROFILES_ACTIVE. - Keep secrets in environment variables (
${DB_PASSWORD}), never committed to Git.
← Previous: Spring Lesson 8 — Exception Handling
Next: Spring Lesson 10 — Security & JWT →
↑ Back to the Spring Boot roadmap