title: Spring Boot context created programmatically does not set typesafe configuration properties
body: <p>We're moving our huge application to Spring Boot at the moment which works very well for our production code. Unfortunately we also have to adapt all tests. We want to keep changes as little as possible here for now and try to only rewrite the one big factory class nearly every test uses to create instances of common classes.
  Because objects are created by method call we are forced to create a context programmatically. We've managed it to get insertion of properties to fields annotated with @Value working, but it does not work for the new way Spring Boot introduces (<a href="http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-typesafe-configuration-properties" rel="nofollow">typesafe configuration properties</a>).</p>
  
  <p>Here a little example which illustrates the problem:</p>
  
  <pre><code>AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
  PropertySourcesPlaceholderConfigurer propertiesPostProcessor = new PropertySourcesPlaceholderConfigurer();
  ropertiesPostProcessor.setLocation(new ClassPathResource("config/test.properties"));
  context.addBeanFactoryPostProcessor(propertiesPostProcessor);
  context.register(PropertyTest.class);
  context.refresh();
  
  PropertyTest bean = context.getBean(PropertyTest.class);
  System.out.println("value="+bean.getValue());
  System.out.println("valueTypesafe="+bean.getValueTypesafe());
  </code></pre>
  
  <p>PropertyTest class:</p>
  
  <pre><code>@Configuration
  @ConfigurationProperties(prefix = "test")
  @EnableConfigurationProperties
  public class PropertyTest {
      @Value("${test.value}")
      private String value;
  
      private String valueTypesafe;
  
      public String getValue() {
          return value;
      }
  
      public void setValue(String value) {
          this.value = value;
      }
  
      public String getValueTypesafe() {
          return valueTypesafe;
      }
  
      public void setValueTypesafe(String valueTypesafe) {
          this.valueTypesafe = valueTypesafe;
      }
  }
  </code></pre>
  
  <p>test.properties</p>
  
  <pre><code>test.value="works"
  test.valueTypesafe="works"
  valueTypesafe="works"
  </code></pre>
  
  <p>output:</p>
  
  <pre><code>value="works"
  valueTypesafe=null
  </code></pre>
  
  <p>We've also tried several variations of this (other annotations, contexts, etc.), but nothing worked. I've tried to find out how Spring Boot does it, but ended up in <em>EnableConfigurationPropertiesImportSelector</em> and <em>ConfigurationPropertiesBindingPostProcessor</em>, where we did not managed it to integrate them into our context.</p>
  
  <hr>
  
  <h2>Solution</h2>
  
  <p>We've finally managed it by touching every test and adding <code>@SpringApplicationConfiguration</code>. Thus not creating the context manually. The programmatically way did not work and would have had other disadvantages as well.</p>