The @Value annotation injects a value into a Spring bean field, constructor parameter or method parameter. The value can come from a property placeholder such as ${app.name}, which Spring resolves from application.properties, application.yml, environment variables and system properties, or from a Spring Expression Language (SpEL) expression such as #{...}.
It is the simplest way to externalise a single setting. For groups of related settings, Spring Boot's @ConfigurationProperties is usually the better tool.
@Value and better alternativesDefine the property in application.properties (or application.yml).
Then reference it with the ${...} placeholder syntax. Constructor injection keeps the bean immutable and easy to test.
Spring converts the string from the property file to int and boolean using its conversion service. If a placeholder cannot be resolved and has no default, the application fails at startup with an IllegalArgumentException about an unresolvable placeholder.
Add a default after a colon so the bean still starts when the property is missing.
An empty default after the colon gives an empty string rather than a startup failure. Use it for optional settings, and check for blank before use.
A comma-separated property converts directly to an array or a List.
You can also combine a placeholder with SpEL when you need custom parsing, for example @Value("#{'${app.languages}'.split(',')}"). For maps and nested structures, prefer @ConfigurationProperties, because @Value does not support relaxed binding of structured data.
Anything inside #{...} is evaluated as SpEL at bean creation time.
Placeholders are resolved first, then the SpEL expression is evaluated. T(...) references a static type, and a bean name such as appSettings refers to another bean.
Spring Boot reads environment variables through its Environment. A property app.max-connections can be overridden by the environment variable APP_MAXCONNECTIONS or APP_MAX_CONNECTIONS thanks to relaxed binding rules, without changing code. Profile files such as application-prod.properties override the base file when the prod profile is active.
When you read an environment variable directly by its name, the value in the placeholder must match the variable name.
@Value and better alternatives| Need | Use |
|---|---|
| One or two simple settings | @Value |
| Group of related settings with validation | @ConfigurationProperties |
| Read property in code at runtime | Environment.getProperty(...) |
| Value in a static field | Inject through a setter, @Value ignores static fields |
@Value fields are not injected into objects you create with new, because Spring only processes managed beans. The value is also fixed once the bean is created, so it does not change when the file changes unless you use a refresh mechanism.
${...} and #{...}?${...} is a property placeholder resolved from the environment. #{...} is a SpEL expression evaluated at runtime, and it can contain placeholders inside it.
@Value field null?The class is probably not a Spring bean, for example created with new, or the field is static. Make the class a bean and use constructor injection.
@Value or @ConfigurationProperties?Use @Value for a few independent values and @ConfigurationProperties for grouped, validated, type-safe configuration.
@Value inject a secret?It can read one from the environment or a secrets source, but never store secrets in committed property files.
@Value injects externalised values and SpEL results into Spring beans. Use ${key:default} for properties, prefer constructor injection, supply defaults for optional values, and switch to @ConfigurationProperties when configuration grows beyond a few settings.