title: Spring loads a configuration class with only empty member variables
body: <p>I have a Spring web application, and I'm trying to load a YAML configuration file into a Java configuration class. However, my configuration class only contains empty member variables after it is instantiated in my JUnit test. I'm new to Spring and have probably missed something obvious. I built the project with Maven and have a Maven style directory tree.</p>
  
  <p>My configuration Java class:</p>
  
  <blockquote>
    <p>src/main/java/com/my/package/config/YAMLConfigDatabase:</p>
  </blockquote>
  
  <pre><code>package com.my.package;
  import org.springframework.boot.context.properties.ConfigurationProperties;
  import org.springframework.boot.context.properties.EnableConfigurationProperties;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  
  @Configuration
  @EnableConfigurationProperties
  @ConfigurationProperties(prefix = "database")
  
  public class YAMLConfigDatabase {
  
      private String url;
  
      private int port;
  
      private String schema;
  
      private String username;
  
      private String password;
  
      //Getters and setters are all here.
  
  }
  </code></pre>
  
  <p>My configuration YAML file:</p>
  
  <blockquote>
    <p>src/main/resources/application.yml</p>
  </blockquote>
  
  <pre><code>server.port: 8090
  
  database:
    url: 'localhost'
    port: 3306
    schema: 'my_schema'
    username: 'webappuser'
    password: 'secretPassword'
  </code></pre>
  
  <p>My JUnit test to check whether I can indeed load the configuration file:</p>
  
  <pre><code>package com.my.package;
  
  import com.my.package.config.YAMLConfigDatabase;
  import org.junit.Test;
  import org.junit.runner.RunWith;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.test.context.ContextConfiguration;
  import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  
  @RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(classes = {YAMLConfigDatabase.class})
  public class YAMLConfigTest {
  
  
      private YAMLConfigDatabase config;
  
      @Autowired
      public void setYAMLConfigDatabase(YAMLConfigDatabase config){
          this.config = config;
      }
  
      @Test
      public void isYAMLConfigLoaded(){
          System.out.println(this.config);
          System.out.println(this.config.getPassword()); 
          //The above line returns "null", but I would like it to return "secretPassword".
      }
  }
  </code></pre>
  
  <hr>
  
  <p>EDIT:</p>
  
  <p>I changed my <code>YAMLConfigDatabase.java</code> to look like this:</p>
  
  <pre><code>package com.my.package.config;
  
  import org.springframework.beans.factory.annotation.Value;
  import org.springframework.boot.context.properties.ConfigurationProperties;
  import org.springframework.context.annotation.PropertySource;
  import org.springframework.stereotype.Component;
  
  @ConfigurationProperties(prefix = "database")
  @PropertySource(value = "classpath:application.yml") //new line
  @Component
  public class YAMLConfigDatabase {
  
      @Value("${url}") //new line
      private String url;
  
      @Value("${port}") //new line
      private Integer port;
  
      @Value("${schema}") //new line
      private String schema;
  
      @Value("${username}") //new line
      private String username;
  
      @Value("${password}") //new line
      private String password;
  }
  </code></pre>
  
  <p>I used Senior Promidor tip to add the @Value annotation to all member variables, and I also had to add the line <code>@PropertySource(value = "classpath:application.yml")</code>. If I skipped the latter step, the arguments inside the @Value annotations were interpreted literally, as I mentioned in a comment.</p>