The @Value annotation injects a value into a Spring bean field, constructor parameter or method parameter. The value can be a literal, a property resolved with ${...} from application.properties, application.yml, environment variables or command-line arguments, or a computed result of a SpEL expression written as #{...}. It keeps configuration out of your code and lets the same build run in different environments.
@Value lives in org.springframework.beans.factory.annotation.Value. For groups of related settings, @ConfigurationProperties is usually a better choice, and this article explains when to use each.
Define properties in application.properties:
Then inject them. Constructor injection is preferred because it keeps beans immutable and easy to test:
Spring converts the text to int, boolean and List<String> (comma-separated values) using its conversion service. If a property does not exist, startup fails with an IllegalArgumentException about an unresolvable placeholder, which is usually what you want for required settings.
Add a default after a colon so the bean starts when the property is absent:
The empty form : gives an empty string, and :#{null} gives a real null.
#{...} evaluates a Spring Expression Language expression:
Use SpEL sparingly. Expressions are strings, so mistakes appear at runtime and are hard to refactor.
@Value also works with resources:
To read a custom properties file in a plain Spring application, add @PropertySource("classpath:custom.properties") to a configuration class. Spring Boot loads application.* files automatically, and spring.config.import pulls in extra files.
static fields, because injection happens per instance.new, since Spring never processes them.@PostConstruct method or use constructor injection.| Aspect | @Value | @ConfigurationProperties |
|---|---|---|
| Best for | A few single values | A group of related settings |
Relaxed binding (maxRetries, max-retries) | Limited, use the exact kebab-case key | Yes |
| Type-safe objects and nesting | No | Yes |
| Bean validation | Manual | @Validated on the class |
| IDE metadata | None | Generated configuration metadata |
A typed alternative to the earlier example:
Enable it with @ConfigurationPropertiesScan or @EnableConfigurationProperties(AppProperties.class).
${...} resolves a property placeholder from the environment. #{...} evaluates a SpEL expression. You can nest one inside the other, as in #{2 * ${app.max-retries}}.
Use comma-separated text for a list, as in ${app.allowed-origins}. For a map, SpEL such as #{${app.map}} works, but @ConfigurationProperties is cleaner.
The class is probably not a Spring bean, the field is static, or you read it before injection finished. Use constructor injection to avoid the last problem.
Yes. Spring maps APP_NAME to app.name, so @Value("${app.name}") finds it. For real secrets prefer a secrets manager or mounted files over plain properties.
@Value injects literals, property placeholders and SpEL results into Spring beans. Use ${key:default} for configuration, prefer constructor injection, and switch to @ConfigurationProperties when you have several related or validated settings.