title: How do i load a yaml file programatically the same way as using @ConfiguationProperties?
body: <p>Not sure if this is an issue or if i'm just not going at it the right way. My issue is that i'm trying to load YAML files in 2 ways. One way is via the @configurationProperties and specifying a location. This works great and the YAML gets loaded properly into my Java object.</p>
  
  <p>PageDescriptor.java</p>
  
  <pre><code>import java.util.HashMap;
  import java.util.Map;
  
  import org.springframework.boot.context.properties.ConfigurationProperties;
  import org.springframework.context.annotation.Configuration;
  
  @Configuration
  @ConfigurationProperties(locations = "classpath:config/page.yml")
  public class PageDescriptor {
  private HashMap pageConfigs;
  
  public PageDescriptor() {
  
  }
  
  public HashMap&lt;String, PageConfig&gt; getPageConfigs() {
      return pageConfigs;
  }
  
  public void setPageConfigs(final HashMap&lt;String, PageConfig&gt; pageConfigs) {
      this.pageConfigs = pageConfigs;
  }
  </code></pre>
  
  <p>PageConfig.java</p>
  
  <pre><code>public class PageConfig {
      private String baseUrl;
      private String configLocation;
      private String navDescriptorKey;
      private String defaultRoute;
  
      public PageConfig() {
  
      }
  
      public String getBaseUrl() {
          return baseUrl;
      }
  
      public void setBaseUrl(final String baseUrl) {
          this.baseUrl = baseUrl;
      }
  
      public String getDefaultRoute() {
          return defaultRoute;
      }
  
      public void setDefaultRoute(final String defaultRoute) {
          this.defaultRoute = defaultRoute;
      }
  
      public String getNavDescriptorKey() {
          return navDescriptorKey;
      }
  
      public void setNavDescriptorKey(final String navDescriptorKey) {
          this.navDescriptorKey = navDescriptorKey;
      }
  
      public String getConfigLocation() {
          return configLocation;
      }
  }
  </code></pre>
  
  <p>Next i was trying to load other YAML files into the same object but via java code. I approached it like this.</p>
  
  <p>code to test pulling the config:</p>
  
  <pre><code>final PathMatchingResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();
      final Resource[] resources = resourceResolver.getResources("classpath*:com/**/page.yml");
      if (!((resources == null) || (resources.length == 0))) {
          for (final Resource resource : resources) {
              // final Yaml yaml = new Yaml();
              // final InputStream in = resource.getInputStream();
              // final PageDescriptor componentPageDescriptor = yaml.loadAs(in, PageDescriptor.class);
              // in.close();
              final YamlConfigurationFactory&lt;PageDescriptor&gt; factory = new YamlConfigurationFactory&lt;PageDescriptor&gt;(
                      PageDescriptor.class);
              final Map&lt;Class&lt;?&gt;, Map&lt;String, String&gt;&gt; aliases = new HashMap&lt;Class&lt;?&gt;, Map&lt;String, String&gt;&gt;();
              // aliases.put(PageConfig.class, Collections.singletonMap("pageConfig", "pageConfig"));
              factory.setResource(resource);
              factory.setExceptionIfInvalid(true);
              factory.setPropertyAliases(aliases);
              // factory.setValidator(validator);
              factory.setMessageSource(new StaticMessageSource());
              factory.afterPropertiesSet();
              final PageDescriptor componentPageDescriptor = factory.getObject();
          }
      }
  </code></pre>
  
  <p>While this loads the componentPageDescriptor object as a PageDescriptor it does not load the HashMap correctly it instead does HashMap>. Is there an easy way to simulate how the @ConfigurationProperties works in loading the YAML files?</p>
  
  <p>below is the YAML file</p>
  
  <pre><code>---
  #pageDescriptors:
    pageConfigs:
      Home:
        baseUrl: ""
        configLocation: "/static/app/src/scripts/pages/home/config.js"
        navDescriptorKey: "Default"
        defaultRoute: ""
  </code></pre>
  
  <p><strong>UPDATE:
  As a temporary solution I was able to convert the YAML to Object to JSON to PageDescriptor and it works as expected.</strong></p>
  
  <pre><code>    final PathMatchingResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();
      final Resource[] resources = resourceResolver
              .getResources("classpath*:com/**/page.yml");
  
      if (!((resources == null) || (resources.length == 0))) {
          for (final Resource resource : resources) {
              final Yaml yaml = new Yaml();
              final Object map = yaml.load(resource.getInputStream());
              final ObjectWriter ow = new ObjectMapper().writer();
              final String componentPageDescriptorString = ow.writeValueAsString(map);
              final PageDescriptor componentPageDescriptor = new ObjectMapper()
                      .readValue(componentPageDescriptorString, PageDescriptor.class);
              pageDescriptor.merge(componentPageDescriptor);
          }
      }
  </code></pre>