How does Spring Boot handle application configuration with application.properties and profiles?
Understand Spring Boot configuration with application.properties and profiles: override order, activating profiles, @Value and @ConfigurationProperties.
Expected Interview Answer
Spring Boot externalizes configuration into application.properties (or application.yml) files, and profiles let you keep environment-specific settings in files like application-dev.properties or application-prod.properties that are activated by name so the same build runs differently per environment.
Boot loads properties from an ordered set of sources — command-line arguments, environment variables, profile-specific files, and the base file — where higher-priority sources override lower ones. You activate a profile with spring.profiles.active (set via property, environment variable, or command-line), and Boot merges the matching application-<profile> file over the base file. Values are injected with @Value, or grouped into a type-safe class using @ConfigurationProperties, and @Profile can conditionally register beans only for chosen environments.
- One build artifact runs across dev, test, and prod
- Sensitive or environment-specific values stay out of code
- Clear override order makes precedence predictable
- @ConfigurationProperties gives type-safe, validated binding
- @Profile conditionally activates beans per environment
AI Mentor Explanation
Profiles are like preparing different game plans for different pitches: one strategy sheet for a spinning subcontinent track, another for a bouncy Australian wicket. The core team stays the same, but the captain activates the plan that matches the ground on match day.
Step-by-Step Explanation
Step 1
Define the base file
Put shared defaults in application.properties or application.yml on the classpath.
Step 2
Add profile-specific files
Create application-dev.properties, application-prod.properties, etc., for per-environment overrides.
Step 3
Activate a profile
Set spring.profiles.active=prod via property, environment variable, or --spring.profiles.active on the command line.
Step 4
Bind the values
Inject single values with @Value, or map groups to a class with @ConfigurationProperties.
Step 5
Gate beans by profile
Annotate beans with @Profile("prod") so they register only when that profile is active.
What Interviewer Expects
- Understanding externalized configuration and its override order
- Difference between application.properties and profile-specific files
- How to activate a profile in multiple ways
- When to use @Value versus @ConfigurationProperties
- How @Profile conditionally registers beans
Common Mistakes
- Thinking a separate build is needed per environment
- Not knowing the precedence order of configuration sources
- Confusing @Profile (bean activation) with spring.profiles.active (profile selection)
- Hardcoding secrets in application.properties instead of externalizing them
- Assuming profile-specific files replace rather than override the base file
Best Answer (HR Friendly)
“Spring Boot keeps settings in a properties file instead of the code, and profiles let you have different files for development and production. You just tell the app which profile is active, and it picks the matching settings — so the same program behaves correctly in each environment.”
Code Example
# application.properties (base)
app.timeout=30
spring.profiles.active=dev
# application-dev.properties
app.datasource.url=jdbc:h2:mem:devdb
# application-prod.properties
app.datasource.url=jdbc:postgresql://db:5432/app@ConfigurationProperties(prefix = "app")
public class AppProps {
private int timeout;
// getters and setters
}
@Bean
@Profile("prod")
public Cache productionCache() {
return new RedisCache();
}Follow-up Questions
- What is the precedence order of Spring Boot configuration sources?
- How do @Value and @ConfigurationProperties differ?
- How do you activate more than one profile at the same time?
- What does @Profile do and how is it different from spring.profiles.active?
- How would you keep secrets out of application.properties?
MCQ Practice
1. Which property activates a Spring Boot profile?
spring.profiles.active names the profile(s) to activate; matching application-<profile> files are then loaded.
2. How does a profile-specific file relate to the base application.properties?
Profile-specific files override overlapping keys and supplement the base file rather than replacing it entirely.
3. Which annotation binds a group of properties into a type-safe class?
@ConfigurationProperties maps a prefixed group of properties onto fields of a class with validation support.
Flash Cards
How do you activate a profile? — Set spring.profiles.active via a property, environment variable, or --spring.profiles.active command-line argument.
What file holds prod-only settings? — application-prod.properties (or application-prod.yml), loaded when the prod profile is active.
@Value vs @ConfigurationProperties? — @Value injects one value; @ConfigurationProperties binds a group of properties to a type-safe class.
What does @Profile do? — Registers a bean only when the named profile is active.
Continue Learning
Related Interview Questions
What are Spring bean scopes and how do singleton and prototype differ?
medium
What is the Spring bean lifecycle and what callbacks does it provide?
medium
What is the difference between @Autowired field, constructor, and setter injection?
medium
What is @SpringBootApplication and what annotations does it combine?
easy