Backend Development 6 min read

Spring @Value Annotation: Property, SpEL, Map, Constructor, Setter, and Record Injection

This article explains how Spring's @Value annotation can inject literal strings, file values, system properties, defaults, arrays, maps, and SpEL expressions into fields, constructors, setters, and Java records, providing practical code examples for each scenario.

Cognitive Technology Team
Cognitive Technology Team
Cognitive Technology Team
Spring @Value Annotation: Property, SpEL, Map, Constructor, Setter, and Record Injection

Spring's @Value annotation can be used to inject values into Spring-managed bean properties, and it works on fields as well as constructor or method parameters.

Property injection examples : you can inject a literal string, a value from a properties file, a system property, or provide a default value when the property is missing. If a property exists both as a system property and in a file, the system property takes precedence. Comma‑separated values can be injected into an array, e.g., @Value("{listOfValues}") private String[] valuesArray;

Advanced SpEL examples : using Spring Expression Language you can retrieve system properties ( @Value("#{systemProperties['priority']}") private String spelValue; ), supply defaults with the Elvis operator ( @Value("#{systemProperties['unknown']?:'some default'}") private String spelSomeDefault; ), reference other beans ( @Value("#{someBean.someValue}") private Integer someBeanValue; ), split a comma‑separated list into a List ( @Value("#{'${listOfValues}'.split(',')}") private List valuesList; ), and inject maps defined in property files ( @Value("#{{valuesMap}}") private Map<String,Integer> valuesMap; ). You can access specific map keys ( @Value("#{{valuesMap}.key1}") private Integer valuesMapKey1; ), safely retrieve unknown keys with a default ( @Value("#{{valuesMap}['unknownKey']?:5}") private Integer unknownMapKeyWithDefaultValue; ), filter map entries ( @Value("#{{valuesMap}.?[value>'1']}") private Map<String,Integer> valuesMapFiltered; ), or inject all system properties as a map ( @Value("#{systemProperties}") private Map<String,String> systemPropertiesMap; ).

Constructor injection : @Autowired public PriorityProvider(@Value("${priority:normal}") String priority) { this.priority = priority; } injects a property directly into the constructor, with a default fallback.

Setter injection : private List<String> values = new ArrayList<>(); @Autowired public void setValues(@Value("#{'${listOfValues}'.split(',')}") List<String> values) { this.values.addAll(values); } demonstrates injecting a list via a setter method.

Note: method injection can also assign values to static fields.

Record injection : Since Java 14, records provide immutable data carriers, and Spring 6.0.6+ supports @Value on record components, e.g., @Component @PropertySource("classpath:values.properties") public record PriorityRecord(@Value("{priority:normal}") String priority) {} , injecting the value directly into the record's constructor.

JavaSpringSPELAnnotationsdependency injection
Cognitive Technology Team
Written by

Cognitive Technology Team

Cognitive Technology Team regularly delivers the latest IT news, original content, programming tutorials and experience sharing, with daily perks awaiting you.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.