Backend Development 9 min read

Injecting List, Array, Set, and Map Values in Spring Using @Value and EL Expressions

This article explains how to configure and inject List, Map, Set, and array values in Spring applications using @Value annotations, EL expression split functions, default values, and custom parsing methods, providing practical code examples and discussing the advantages and limitations of each approach.

Top Architect
Top Architect
Top Architect
Injecting List, Array, Set, and Map Values in Spring Using @Value and EL Expressions

In everyday Spring development you often need to store collection types such as List or Map in configuration files. While Spring natively supports these types, using @Value directly can cause errors, so alternative approaches are required.

1. Configuring and Injecting a List

YAML configuration:

test:
  list:
    - aaa
    - bbb
    - ccc

Properties configuration:

test.list[0]=aaa
test.list[1]=bbb
test.list[2]=ccc

Attempting to inject with @Value("${test.list}") fails with IllegalArgumentException: Could not resolve placeholder 'test.list' . The recommended solution is to create a configuration class:

@Configuration
@ConfigurationProperties("test")
public class TestListConfig {
    private List
list;
    public List
getList() { return list; }
    public void setList(List
list) { this.list = list; }
}

Then autowire the class wherever needed:

@Autowired
private TestListConfig testListConfig;
// testListConfig.getList();

2. Using Arrays with @Value

YAML configuration for arrays:

test:
  array1: aaa,bbb,ccc
  array2: 111,222,333
  array3: 11.1,22.2,33.3

Injection examples:

@Value("${test.array1}")
private String[] testArray1;

@Value("${test.array2}")
private int[] testArray2;

@Value("${test.array3}")
private double[] testArray3;

Adding default values prevents errors when the key is missing:

@Value("${test.array1:}")
private String[] testArray1;

@Value("${test.array2:}")
private int[] testArray2;

@Value("${test.array3:}")
private double[] testArray3;

3. Pros and Cons of the Array Approach

Advantages: no need to write a configuration class; values are concise and comma‑separated.

Disadvantages: business code rarely works with arrays directly, requiring conversion to List for typical operations.

4. Using EL Expressions to Parse Collections

For a List, store a comma‑separated string in YAML:

test:
  list: aaa,bbb,ccc

Inject with EL split:

@Value("#{'${test.list}'.split(',')}")
private List
testList;

Provide a default value to avoid errors when the key is absent:

@Value("#{'${test.list:}'.split(',')}")
private List
testList;

Handle the empty‑string case correctly:

@Value("#{'${test.list:}'.empty ? null : '${test.list:}'.split(',')}")
private List
testList;

Parsing a Set

YAML example:

test:
  set: 111,222,333,111

Injection:

@Value("#{'${test.set:}'.empty ? null : '${test.set:}'.split(',')}")
private Set
testSet;

Parsing a Map

YAML example (JSON strings as values):

test:
  map1: '{"name": "zhangsan", "sex": "male"}'
  map2: '{"math": "90", "english": "85"}'

Direct injection works only when the key exists:

@Value("#{${test.map1}}")
private Map
map1;

@Value("#{${test.map2}}")
private Map
map2;

For more flexible handling, a custom decoder can be used:

public class MapDecoder {
    public static Map
decodeMap(String value) {
        try {
            return JSONObject.parseObject(value, new TypeReference
>() {});
        } catch (Exception e) {
            return null;
        }
    }
}

Usage with @Value:

@Value("#{T(com.github.jitwxs.demo.MapDecoder).decodeMap('${test.map1:}')}")
private Map
map1;

@Value("#{T(com.github.jitwxs.demo.MapDecoder).decodeMap('${test.map2:}')}")
private Map
map2;

5. Final Remarks

The article demonstrates that by leveraging EL expressions, default values, and custom parsing methods, developers can conveniently configure and use collection‑type properties in Spring. It also warns that @Value should not be combined with @AllArgsConstructor on the same class, as this leads to compilation errors.

Javabackend developmentconfigurationSpringEL ExpressionsValue Annotation
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

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.