@Value Injection
BeginnerInjects individual property values, Spring EL expressions, or system variables directly into fields or constructor parameters.
Overview
@Value is the simplest way to inject a single property into a Spring bean. It resolves values from application.properties/yaml, system properties, and environment variables. It also supports Spring Expression Language (SpEL) for computed values. While convenient for simple cases, @ConfigurationProperties is preferred for groups of related properties because it offers type safety, IDE completion, and validation.
Basic Property Injection
@Value("${property.key}") binds a property by key. An optional default value after ":" is used when the key is absent. SpEL expressions use #{...} syntax and can call methods or reference other beans.
@Component
public class AppConfig {
@Value("${app.name}")
private String appName;
@Value("${app.timeout:5000}") // default 5000 if not set
private int timeoutMs;
@Value("${app.admins:admin1,admin2}")
private List<String> admins; // comma-separated → List
@Value("#{systemProperties['user.home']}")
private String userHome; // SpEL — system property
@Value("#{T(java.lang.Math).PI}")
private double pi; // SpEL — static method/field
}Constructor Injection & Profiles
Prefer constructor injection over field injection for testability. Profile-specific properties files (application-prod.properties) override defaults and are picked up automatically.
@Service
public class MailService {
private final String host;
private final int port;
public MailService(
@Value("${mail.host}") String host,
@Value("${mail.port:587}") int port) {
this.host = host;
this.port = port;
}
}
// application.properties
app.name=AiCanCode
app.timeout=3000
mail.host=smtp.example.com
// application-prod.properties (overrides above when profile=prod)
mail.host=smtp.prod.example.com@ConfigurationProperties vs @Value
For more than one or two properties, @ConfigurationProperties on a @Component or @Bean is cleaner. It supports nested objects, JSR-303 validation, and relaxed binding (kebab-case ↔ camelCase).
@ConfigurationProperties(prefix = "app.mail")
@Validated
public class MailProperties {
@NotBlank
private String host;
private int port = 587;
private boolean ssl;
// getters/setters ...
}
// Enable in main class or any @Configuration class:
@EnableConfigurationProperties(MailProperties.class)
// application.yml
app:
mail:
host: smtp.example.com
port: 465
ssl: trueKey Points to Remember
- 1@Value("${key}") reads from environment/properties; #{expr} evaluates SpEL.
- 2Default values are specified with a colon: @Value("${key:default}").
- 3Comma-separated strings are auto-split into List<String> or arrays.
- 4Field injection works but constructor injection is preferred for testability.
- 5@ConfigurationProperties is better for groups of related properties (type-safe, validated).
- 6Profile-specific files (application-{profile}.properties) override base properties automatically.
Interview Questions
Sign in to ask AriaWhat is the difference between ${...} and #{...} in @Value?
How do you set a default value in @Value when the property is missing?
When would you choose @ConfigurationProperties over @Value?
Can @Value inject a List or Map directly from properties? How?
Why does @Value not work in @Configuration classes before the context is fully initialised?
Ask Aria about @Value Injection
Your personal AI tutor — ask anything about this concept
Revision Status
Personal Notes
Sign in to save personal notes for this topic.
Discussion
Sign in to join the discussion.