Difficulty: Beginner
How does Spring Boot handle configuration, and how do profiles let you run the same app in dev, test, and production?
Every real application behaves differently across environments. Locally you use an H2 database and verbose logging, in staging you use a shared Postgres, and in production you use a managed database with secrets injected by the platform. Hard-coding any of that is a recipe for a bad day. Spring Boot's externalised configuration and profiles solve exactly this: the same compiled JAR, different behaviour depending on where it runs.
The main configuration file is application.properties or application.yml under src/main/resources. Properties uses flat keys like spring.datasource.url=jdbc:postgresql://localhost/app, while YAML uses indentation and is easier to read for nested structures. Both are equivalent, and if both exist properties wins. Values are read through the Environment, which merges many sources in a defined priority order. Roughly, from highest to lowest: command-line arguments, Java system properties, OS environment variables, profile-specific files outside the JAR, profile-specific files inside the JAR, the default application file, and finally defaults in code. This ordering is why you can override server.port with --server.port=9000 or SERVER_PORT=9000 without touching the file, which is exactly how Docker and Kubernetes configure containers.
Profiles are named sets of configuration. You create application-dev.yml, application-prod.yml, and so on, and activate one with spring.profiles.active=prod in a property, an environment variable SPRING_PROFILES_ACTIVE, or --spring.profiles.active on the command line. Boot loads the base file first and then overlays the profile file, so you only place the differences in the profile file. You can also attach profiles to beans with @Profile("dev"), so a mock payment gateway exists only in dev and the real one only in prod. In Boot 2.4 and later, a single YAML file can hold several documents separated by --- with spring.config.activate.on-profile conditions.
For reading values in code there are two tools. @Value with a placeholder such as app.jwt.expiry-minutes and a default of 60 injects a single value with a default. It is quick but scatters strings across the codebase and lacks type safety. @ConfigurationProperties binds a whole prefix to a typed class, supports nested objects, lists, durations, relaxed binding such as app.jwt-secret matching APP_JWT_SECRET, and validation with @Validated. For anything beyond one or two values, prefer it.
Security is the important closing point. Never commit passwords, API keys, or JWT secrets to the repository. Use environment variables, a secrets manager such as AWS Secrets Manager, HashiCorp Vault, or Kubernetes Secrets, or Spring Cloud Config with encryption. A file like application-local.yml added to .gitignore is acceptable for developer-only overrides. Also remember that Actuator's env endpoint can leak values, so Boot masks known sensitive keys and you should restrict the endpoint anyway.
Interview edge cases: multiple active profiles are allowed, comma separated, and later ones override earlier ones. A profile group in properties can bundle profiles together. And if no profile is active, the default profile named default is used.
# application.yml
spring:
application:
name: placement-service
profiles:
active: dev
app:
jwt:
secret: change-me-locally
expiry-minutes: 60
---
spring:
config:
activate:
on-profile: prod
datasource:
url: jdbc:postgresql://db.internal:5432/placements
username: ${DB_USER}
password: ${DB_PASSWORD}
app:
jwt:
secret: ${JWT_SECRET}
The prod document overrides only what differs and pulls secrets from environment variables.
@Validated
@ConfigurationProperties(prefix = "app.jwt")
public record JwtProperties(@NotBlank String secret, @Min(1) int expiryMinutes) { }
@SpringBootApplication
@ConfigurationPropertiesScan
public class App { }
@Profile("dev")
@Service
class FakePaymentGateway implements PaymentGateway { /* only in dev */ }
Records work for immutable properties in Boot 3, and validation fails startup on bad config.
application.yml, Profiles, ConfigurationProperties, Externalised Configuration