The @Value annotation injects a value into a Spring bean field, constructor parameter or method parameter at startup. It reads from a property placeholder such as ${app.name} (property files, environment variables, system properties) or evaluates a Spring Expression Language expression such as #{2 * 10}. It lets you keep configuration outside the code.
For a single property it is the simplest option. For a group of related settings, Spring Boot recommends @ConfigurationProperties, which is type-safe and validated. Examples use Spring Boot 3.
${...} placeholders@Value versus @ConfigurationProperties${...} placeholdersDefine properties in application.properties (or application.yml):
Inject them with @Value. Prefer constructor injection, which keeps fields final and the class easy to test.
Spring converts the string to the parameter type, so int, boolean, long and enums work. In a Boot application, Duration and DataSize values such as 5s or 10MB also convert.
If a placeholder cannot be resolved, startup fails with an IllegalArgumentException ("Could not resolve placeholder"). Supply a default after a colon to make the property optional.
Spring's Environment merges many sources, so one annotation reads them all. Highest priority wins: command-line arguments, then system properties and environment variables, then the property files.
In Boot, an environment variable APP_NAME overrides app.name thanks to relaxed binding of the property key.
A comma-separated property converts to an array or list. SpEL (#{...}) gives you computed values and bean references.
Keep SpEL light. Complex expressions in annotations are hard to test and debug.
This injects a Resource you can read with welcome.getInputStream().
null inside the constructor. Use constructor parameters instead.@Value works only on Spring-managed beans. A class created with new receives nothing.@Value versus @ConfigurationPropertiesWhen settings belong together, bind them once into an immutable record:
Enable it with @EnableConfigurationProperties(AppProperties.class) or @ConfigurationPropertiesScan.
| Need | Better choice |
|---|---|
| One or two simple values | @Value |
| Group of related settings | @ConfigurationProperties |
| Validation of settings | @ConfigurationProperties with @Validated |
| SpEL expressions | @Value |
${} and #{} in @Value?${...} resolves a property placeholder. #{...} evaluates a SpEL expression. They can be combined, as in #{'${app.roles}'.split(',')}.
@Value field null?The class is not a Spring bean, the field is static, or you read it in the constructor before field injection. Use constructor injection.
@Value reload when a property changes?Not by default. Values are injected once at startup. Refresh scopes (such as Spring Cloud's @RefreshScope) are needed for live reloads.
Put the property in application-dev.properties and activate the profile with spring.profiles.active=dev.
@Value injects externalized configuration into beans using ${property:default} placeholders and SpEL expressions. Use constructor injection, give optional properties a default, and switch to @ConfigurationProperties once you have more than a few related settings.