title: Spring Boot - inject static map from application.yml
body: <p>I refered <a href="https://stackoverflow.com/questions/24917194/spring-boot-inject-map-from-application-yml">Spring Boot - inject map from application.yml</a> for injecting map from application.yml file</p>
  
  <p>My application.yml snippet  is below</p>
  
  <pre><code>easy.app.pairMap:
      test1: 'value1' 
      test2: 'value2'
  </code></pre>
  
  <p>Properties file is like below</p>
  
  <pre><code>@Component
  @Configuration
  @ConfigurationProperties("easy.app")
  @EnableConfigurationProperties
  public class TestProperties {
  
  
  
  private Map&lt;String, String&gt; pairMap= new HashMap&lt;String, String&gt;();
  
  public void setPairMap(Map&lt;String, String&gt; pairMap) {
      this.pairMap= pairMap;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>The above given code works .Map is not read from application.yml file when the '<em>pairMap</em>' is set as static as below. </p>
  
  <pre><code>@Component
  @Configuration
  @ConfigurationProperties("easy.app")
  @EnableConfigurationProperties
  public class TestProperties {
  
  
  
  private static Map&lt;String, String&gt; pairMap= new HashMap&lt;String, String&gt;();
  
  public static void setPairMap(Map&lt;String, String&gt; pairMap) {
      TestProperties .pairMap= pairMap;
  }
  </code></pre>
  
  <p>}</p>
  
  <p><strong>PS : The issue is only when injecting map , but not on injecting string. Why is this behaviour?</strong></p>
  
  <p>ie the following injection of string in the following configuration works , but not the map injection</p>
  
  <pre><code>easy.app.key1: 'abc'
  
  easy.app.pairMap:
       test1: 'value1' 
       test2: 'value2'
  </code></pre>
  
  <p>Properties file like below </p>
  
  <pre><code>@Component
  @Configuration
  @ConfigurationProperties("easy.app")
  @EnableConfigurationProperties
  public class TestProperties {
  
  
  
  private Map&lt;String, String&gt; pairMap= new HashMap&lt;String, String&gt;();
  
  private static String key1;
  
  public static void setPairMap(Map&lt;String, String&gt; pairMap) {
      this.pairMap= pairMap;
  }
  
  public static void setKey1(String key1) {
      TestProperties.key1= key1;
  }
  
  
  
  public String getKey1(){
      return key1;
  }
  </code></pre>