Creating inline map in SpEL

In this post under Spring SpEL, I will show with example how to create an inline map.

Below is the main class for your reference.

Main class

1  package spel.package6;
2  
3  import org.springframework.beans.factory.annotation.Value;
4  import org.springframework.context.ApplicationContext;
5  import org.springframework.context.annotation.AnnotationConfigApplicationContext;
6  import org.springframework.context.annotation.ComponentScan;
7  import org.springframework.stereotype.Component;
8  
9  import java.util.Map;
10 
11 @Configuration
12 @ComponentScan(basePackages = "spel.package6")
13 public class Example6 {
14     @Value("#{{name:'nikolai',age:'40'}}")
15     private Map<String, String> map;
16 
17     public static void main(String[] args) {
18         ApplicationContext applicationContext = new AnnotationConfigApplicationContext(Example6.class);
19         Example6 example6 = applicationContext.getBean(Example6.class);
20         System.out.println(example6.map.size());
21     }
22 }

In the above code, at line 14, I have created an inline map.

In this way we can create an inline map using SpEL.

Leave a comment