title: spring boot configuration properties not working
body: <p>This is driving me up the wall trying to get spring boot @configurationproperties annotation working. So hoping someone can shed some light on this for me as to what I am doing wrong.
  I have a spring boot application and it contains a application.properties on the classpath. It has a value in there of </p>
  
  <pre><code>server.contextPath=/test/v1
  server.port=8080
  
  spring.profiles.active=dev
  vendors=me
  </code></pre>
  
  <p>I have a application.class which has the spring boot annotation and sits at the top of my package hierarchy</p>
  
  <pre><code>package com.test;
  
  import org.springframework.boot.SpringApplication;
  import org.springframework.boot.autoconfigure.SpringBootApplication;
  
  @SpringBootApplication
  @EnableConfigurationProperties
  public class Application {
  
      public static void main(String[] args) {
          SpringApplication.run(Application.class, args);
      }
  }
  </code></pre>
  
  <p>I am trying to map the property vendors into a configurationproperties bean as below</p>
  
  <pre><code>package com.test.config;
  
  import org.springframework.boot.context.properties.ConfigurationProperties;
  import org.springframework.context.annotation.PropertySource;
  import org.springframework.stereotype.Component;
  
  @Component
  @PropertySource("classpath:application.properties")
  @ConfigurationProperties
  public class GlobalProperties {
  
      private String vendors;
  
      public String getVendors() {
          return vendors;
      }
  
      public void setVendors(String vendors) {
          this.vendors = vendors;
      }
  }
  </code></pre>
  
  <p>and then call this bean from my rest controller. I know it resolves the property as when i rename it the server fails to start. In the code below the props bean is not getting autowired and is null. //code ommitted for brevity</p>
  
  <pre><code>package com.test.controller;
  
  import org.springframework.beans.factory.BeanFactory;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.http.HttpStatus;
  import org.springframework.http.ResponseEntity;
  import org.springframework.stereotype.Component;
  import org.springframework.web.bind.annotation.PathVariable;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestMethod;
  import org.springframework.web.bind.annotation.RequestParam;
  import org.springframework.web.bind.annotation.RestController;
  
  import com.test.config.GlobalProperties;
  
  @RestController
  @Component
  public class MController {
  
  
      //TODO should be wired in from properties file 
      @Autowired
      GlobalProperties props;
  
  
      private boolean vendorUnknown(String vendor) {
          if(props.getAllowedVendor().equalsIgnoreCase(vendor)) {
              return true;
          }
          return false;
      }
  
      @RequestMapping(value = "/test/{id}", method = RequestMethod.GET, produces = { "application/json" })
      public ResponseEntity&lt;?&gt; getStatus(
              @PathVariable String id) {
          //@RequestBody Bookmark input
          if(vendorUnknown("me")) {
          System.out.println("found");
      };
          return ResponseEntity.noContent().build();
      }
  
  }
  </code></pre>
  
  <p>Anybody point me to what i have done wrong please? </p>
  
  <p>UPDATE:</p>
  
  <p>changed the code above to be a more simplistic version with a test class to recreate the issue. See below for my pom.xml and test class</p>
  
  <p>Pom.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  
      &lt;groupId&gt;com.me.test&lt;/groupId&gt;
      &lt;artifactId&gt;test-service&lt;/artifactId&gt;
      &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
  
      &lt;parent&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
          &lt;version&gt;1.5.3.RELEASE&lt;/version&gt;
      &lt;/parent&gt;
  
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;!-- tag::actuator[] --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-actuator&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;!-- end::actuator[] --&gt;
          &lt;!-- tag::tests[] --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;!-- end::tests[] --&gt;
      &lt;/dependencies&gt;
  
      &lt;properties&gt;
          &lt;java.version&gt;1.8&lt;/java.version&gt;
      &lt;/properties&gt;
  
      &lt;build&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
                  &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
              &lt;/plugin&gt;
              &lt;plugin&gt;
                  &lt;artifactId&gt;maven-failsafe-plugin&lt;/artifactId&gt;
                  &lt;executions&gt;
                      &lt;execution&gt;
                          &lt;goals&gt;
                              &lt;goal&gt;integration-test&lt;/goal&gt;
                              &lt;goal&gt;verify&lt;/goal&gt;
                          &lt;/goals&gt;
                      &lt;/execution&gt;
                  &lt;/executions&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/build&gt;
  
  &lt;/project&gt;
  </code></pre>
  
  <p>Test class:</p>
  
  <pre><code>package com.test.controller;
  
  import static org.mockito.Mockito.when;
  import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
  import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
  
  import org.junit.Before;
  import org.junit.Test;
  import org.junit.runner.RunWith;
  import org.mockito.InjectMocks;
  import org.mockito.Mock;
  import org.mockito.Mockito;
  import org.mockito.MockitoAnnotations;
  import org.springframework.beans.factory.BeanFactory;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
  import org.springframework.boot.test.context.SpringBootTest;
  import org.springframework.http.MediaType;
  import org.springframework.test.context.junit4.SpringRunner;
  import org.springframework.test.web.servlet.MockMvc;
  import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
  import org.springframework.test.web.servlet.setup.MockMvcBuilders;
  
  
  @RunWith(SpringRunner.class)
  @SpringBootTest
  @AutoConfigureMockMvc
  public class MControllerTest {
  
      @Autowired
      private MockMvc mockMvc;
  
      @InjectMocks
      MController testController;
  
      @Before
      public void setup() {
  
          // this must be called for the @Mock annotations above to be processed
          // and for the mock service to be injected into the controller under
          // test.
          MockitoAnnotations.initMocks(this);
  
          this.mockMvc = MockMvcBuilders.standaloneSetup(testController).build();
      }
  
      @Test
      public void testPropertiesMsg() throws Exception {
          mockMvc.perform(MockMvcRequestBuilders.get("/test/1").accept(MediaType.APPLICATION_JSON))
          .andExpect(status().isOk());
      }
  }
  </code></pre>