Unravel › Java concepts › Spring and Boot
Spring configuration properties
Spring reads settings from application.yml, environment variables and command-line flags, and injects them into beans with @Value or @ConfigurationProperties, so behaviour changes per environment without rebuilding.
Learn these first
At a glance
Settings are the dials on the back of a machine.
- 1@Value("${app.timeout:30}") injects one property, with 30 as the default if it is missing
- 2@ConfigurationProperties(prefix = "app") binds a whole group (app.timeout, app.retries) onto a typed class, which is cleaner and validates at startup
- 3Sources are layered: command line over environment variables over application-{profile}.yml over application.ymlSPRING_DATASOURCE_URL in the environment overrides spring.datasource.url.
- 4@Profile("prod") loads a bean only when that profile is active (spring.profiles.active=prod)
- A missing property without a default fails the application at startup, which is good: you find out when deploying, not at 3 a.m
- Typed properties classes can use bean validation (@Validated, @NotNull) so a bad value stops the boot with a clear message
- The Environment holds an ordered list of PropertySources
@Componentpublic class RetryPolicy { @Value("${app.retry.max-attempts:3}") private int maxAttempts; @Value("${app.retry.delay-ms}") private long delayMs;}- line 3 uses Value, the core of Spring configuration properties
- line 6 uses Value, the core of Spring configuration properties
Read the full explanation (about 1 min)
Spring reads settings from application.yml, environment variables and command-line flags, and injects them into beans with @Value or @ConfigurationProperties, so behaviour changes per environment without rebuilding.
A mental model
Settings are the dials on the back of a machine. The same machine is built once and the operator turns the dials differently in test and in production.
How it works
@Value("${app.timeout:30}") injects one property, with 30 as the default if it is missing.
@ConfigurationProperties(prefix = "app") binds a whole group (app.timeout, app.retries) onto a typed class, which is cleaner and validates at startup.
Sources are layered: command line over environment variables over application-{profile}.yml over application.yml. SPRING_DATASOURCE_URL in the environment overrides spring.datasource.url.
@Profile("prod") loads a bean only when that profile is active (spring.profiles.active=prod).
Going deeper
A missing property without a default fails the application at startup, which is good: you find out when deploying, not at 3 a.m. Keep secrets out of the repository and supply them through environment variables or a secret manager.
Typed properties classes can use bean validation (@Validated, @NotNull) so a bad value stops the boot with a clear message.
Under the hood
The Environment holds an ordered list of PropertySources. Relaxed binding maps app.max-retries, APP_MAXRETRIES and app.maxRetries to the same property, which is why environment variable names look different from YAML keys.
Common mistakes
- Hard-coding environment-specific values.
- Committing secrets to application.yml.
- Forgetting that environment variables override the file.
How Unravel recognises it
When you paste code, Unravel looks for these signs of spring configuration properties. Spotting them yourself is a good habit when you read unfamiliar code:
- Annotations:
@Value@ConfigurationProperties@EnableConfigurationProperties@Profile@ConditionalOnProperty@PropertySource - Types:
Environment
Part of a learning path
- Background work in Spring · previous: Spring beans and dependency injection · next: @Scheduled jobs
Related concepts
Builds on this
Frequently asked questions
What is Spring configuration properties?
Spring reads settings from application.yml, environment variables and command-line flags, and injects them into beans with @Value or @ConfigurationProperties, so behaviour changes per environment without rebuilding.
What are the most common mistakes with Spring configuration properties?
Hard-coding environment-specific values. Committing secrets to application.yml. Forgetting that environment variables override the file.
How can I see Spring configuration properties in real code?
Paste your own Java into Unravel and it will point out where spring configuration properties appears, explain how it works at the detail level you choose, and draw the flow of the code around it.