title: Cannot get application.yml properties from outside of the main application class
body: <p>I'm new to Spring Boot and relatively new to Java. I'm working with the <a href="https://github.com/spring-guides/tut-spring-boot-oauth2/tree/master/github" rel="nofollow noreferrer">Github OAuth2 sample application from Spring's documentation</a>. Out of the box, everything works as expected, however, when I try to move the <em>facebook()</em> or <em>github()</em> methods into a new class, they cannot obtain the information from the src/main/resources/application.yml file. I have tried with classes in the same package as the main Application class and have also tried putting the class in another package.</p>
  
  <p><em>src/main/resources/application.yml (all client information is straight from the documentation)</em></p>
  
  <pre><code>security:
    oauth2:
      client:
        client-id: acme
        client-secret: acmesecret
        scope: read,write
        auto-approve-scopes: '.*'
  
  facebook:
    client:
      clientId: 233668646673605
      clientSecret: 33b17e044ee6a4fa383f46ec6e28ea1d
      accessTokenUri: https://graph.facebook.com/oauth/access_token
      userAuthorizationUri: https://www.facebook.com/dialog/oauth
      tokenName: oauth_token
      authenticationScheme: query
      clientAuthenticationScheme: form
    resource:
      userInfoUri: https://graph.facebook.com/me
  github:
    client:
      clientId: bd1c0a783ccdd1c9b9e4
      clientSecret: 1a9030fbca47a5b2c28e92f19050bb77824b5ad1
      accessTokenUri: https://github.com/login/oauth/access_token
      userAuthorizationUri: https://github.com/login/oauth/authorize
      clientAuthenticationScheme: form
    resource:
      userInfoUri: https://api.github.com/user
  
  logging:
    level:
      org.springframework.security: DEBUG
  </code></pre>
  
  <p><strong>The working application</strong></p>
  
  <p>Directory structure with a single package that contains everything in the main Application class except for the class <em>ClientResources.java</em></p>
  
  <p><em>Application.java</em></p>
  
  <pre><code>package com.github.example;
  
  import java.io.Serializable;
  import java.util.ArrayList;
  import java.util.List;
  import javax.servlet.Filter;
  
  import org.springframework.boot.CommandLineRunner;
  import org.springframework.boot.SpringApplication;
  import org.springframework.boot.autoconfigure.SpringBootApplication;
  import org.springframework.context.ApplicationContext;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.boot.autoconfigure.security.oauth2.resource.UserInfoTokenServices;
  import org.springframework.boot.context.properties.ConfigurationProperties;
  import org.springframework.boot.web.servlet.FilterRegistrationBean;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.core.annotation.Order;
  import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  import org.springframework.security.oauth2.client.OAuth2ClientContext;
  import org.springframework.security.oauth2.client.OAuth2RestTemplate;
  import org.springframework.security.oauth2.client.filter.OAuth2ClientAuthenticationProcessingFilter;
  import org.springframework.security.oauth2.client.filter.OAuth2ClientContextFilter;
  import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
  import org.springframework.security.oauth2.config.annotation.web.configuration.EnableOAuth2Client;
  import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
  import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
  import org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint;
  import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;
  import org.springframework.security.web.csrf.CookieCsrfTokenRepository;
  import org.springframework.web.bind.annotation.RestController;
  import org.springframework.web.filter.CompositeFilter;
  
  @SpringBootApplication
  @RestController
  @EnableOAuth2Client
  @EnableAuthorizationServer
  @Order(6)
  public class Application extends WebSecurityConfigurerAdapter implements Serializable {
  
      private static final long serialVersionUID = 7526472295622776147L;
  
      @Autowired
      OAuth2ClientContext oauth2ClientContext;
  
      public static void main(String[] args) {
          SpringApplication.run(Application.class, args);
      }
  
      @Bean
      public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
          return args -&gt; {
              System.out.println("Ready to Authenticate!");
          };
      }
  
      @Override
      protected void configure(HttpSecurity http) throws Exception {
          // @formatter:off
          http.antMatcher("/**")
                  .authorizeRequests()
                  .antMatchers("/", "/login**", "/webjars/**")
                  .permitAll()
                  .anyRequest()
                  .authenticated().and().exceptionHandling()
                  .authenticationEntryPoint(new LoginUrlAuthenticationEntryPoint("/"))
                  .and().logout().logoutSuccessUrl("/").permitAll().and().csrf()
                  .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
                  .and().addFilterBefore(ssoFilter(), BasicAuthenticationFilter.class);
          // @formatter:on
      }
  
      @Configuration
      @EnableResourceServer
      protected static class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
          @Override
          public void configure(HttpSecurity http) throws Exception {
              // @formatter:off
              http.antMatcher("/me")
                  .authorizeRequests()
                  .anyRequest()
                  .authenticated();
              // @formatter:on
          }
      }
  
      @Bean
      public FilterRegistrationBean oauth2ClientFilterRegistration(OAuth2ClientContextFilter filter) {
          FilterRegistrationBean registration = new FilterRegistrationBean();
          registration.setFilter(filter);
          registration.setOrder(-100);
          return registration;
      }
  
      @Bean
      @ConfigurationProperties("facebook")
      public ClientResources facebook() {
          return new ClientResources();
      }
  
      @Bean
      @ConfigurationProperties("github")
      public ClientResources github() {
          return new ClientResources();
      }
  
      private Filter ssoFilter() {
          CompositeFilter filter = new CompositeFilter();
          List&lt;Filter&gt; filters = new ArrayList&lt;&gt;();
          filters.add(ssoFilter(facebook(), "/login/facebook"));
          filters.add(ssoFilter(github(), "/login/github"));
          filter.setFilters(filters);
          return filter;
      }
  
      private Filter ssoFilter(ClientResources client, String path) {
          OAuth2ClientAuthenticationProcessingFilter oAuth2ClientAuthenticationFilter
              = new OAuth2ClientAuthenticationProcessingFilter(path);
          OAuth2RestTemplate oAuth2RestTemplate
              = new OAuth2RestTemplate(client.getClient(), oauth2ClientContext);
  
          oAuth2ClientAuthenticationFilter.setRestTemplate(oAuth2RestTemplate);
          UserInfoTokenServices tokenServices = new UserInfoTokenServices(
                  client.getResource().getUserInfoUri(),
                  client.getClient().getClientId()
          );
  
          tokenServices.setRestTemplate(oAuth2RestTemplate);
          oAuth2ClientAuthenticationFilter.setTokenServices(tokenServices);
  
          return oAuth2ClientAuthenticationFilter;
      }
  
  }
  </code></pre>
  
  <p><em>ClientResources.java</em>
      package com.github.example;</p>
  
  <pre><code>import java.io.Serializable;
  import org.springframework.boot.autoconfigure.security.oauth2.resource.ResourceServerProperties;
  import org.springframework.boot.context.properties.NestedConfigurationProperty;
  import org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeResourceDetails;
  
  public class ClientResources implements Serializable {
  
      private static final long serialVersionUID = 7526472295622776147L;
  
      @NestedConfigurationProperty
      private AuthorizationCodeResourceDetails client = new AuthorizationCodeResourceDetails();
  
      @NestedConfigurationProperty
      private ResourceServerProperties resource = new ResourceServerProperties();
  
      public AuthorizationCodeResourceDetails getClient() {
          return client;
      }
  
      public ResourceServerProperties getResource() {
          return resource;
      }
  }
  </code></pre>
  
  <p><strong>Breaking up the application into multiple classes breaks when it comes to retrieving information from the src/main/resources/application.yml</strong></p>
  
  <p>I have two packages here:</p>
  
  <ol>
  <li>com.github.example</li>
  <li>com.github.example.providers</li>
  </ol>
  
  <p><em>com.github.example.Application.java</em></p>
  
  <pre><code>package com.github.example;
  
  import java.io.Serializable;
  import java.util.ArrayList;
  import java.util.List;
  import javax.servlet.Filter;
  
  import org.springframework.boot.CommandLineRunner;
  import org.springframework.boot.SpringApplication;
  import org.springframework.boot.autoconfigure.SpringBootApplication;
  import org.springframework.context.ApplicationContext;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.boot.autoconfigure.security.oauth2.resource.UserInfoTokenServices;
  import org.springframework.boot.context.properties.ConfigurationProperties;
  import org.springframework.boot.web.servlet.FilterRegistrationBean;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.core.annotation.Order;
  import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  import org.springframework.security.oauth2.client.OAuth2ClientContext;
  import org.springframework.security.oauth2.client.OAuth2RestTemplate;
  import org.springframework.security.oauth2.client.filter.OAuth2ClientAuthenticationProcessingFilter;
  import org.springframework.security.oauth2.client.filter.OAuth2ClientContextFilter;
  import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
  import org.springframework.security.oauth2.config.annotation.web.configuration.EnableOAuth2Client;
  import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
  import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
  import org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint;
  import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;
  import org.springframework.security.web.csrf.CookieCsrfTokenRepository;
  import org.springframework.web.bind.annotation.RestController;
  import org.springframework.web.filter.CompositeFilter;
  
  import com.github.example.providers.*;
  
  @SpringBootApplication
  @RestController
  @EnableOAuth2Client
  @EnableAuthorizationServer
  @Order(6)
  public class Application extends WebSecurityConfigurerAdapter implements Serializable {
  
      private static final long serialVersionUID = 7526472295622776147L;
  
      @Autowired
      OAuth2ClientContext oauth2ClientContext;
  
      public static void main(String[] args) {
          SpringApplication.run(Application.class, args);
      }
  
      @Bean
      public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
          return args -&gt; {
              System.out.println("Ready to Authenticate!");
          };
      }
  
      @Override
      protected void configure(HttpSecurity http) throws Exception {
          // @formatter:off
          http.antMatcher("/**")
                  .authorizeRequests()
                  .antMatchers("/", "/login**", "/webjars/**")
                  .permitAll()
                  .anyRequest()
                  .authenticated().and().exceptionHandling()
                  .authenticationEntryPoint(new LoginUrlAuthenticationEntryPoint("/"))
                  .and().logout().logoutSuccessUrl("/").permitAll().and().csrf()
                  .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
                  .and().addFilterBefore(ssoFilter(), BasicAuthenticationFilter.class);
          // @formatter:on
      }
  
      @Configuration
      @EnableResourceServer
      protected static class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
          @Override
          public void configure(HttpSecurity http) throws Exception {
              // @formatter:off
              http.antMatcher("/me")
                  .authorizeRequests()
                  .anyRequest()
                  .authenticated();
              // @formatter:on
          }
      }
  
      @Bean
      public FilterRegistrationBean oauth2ClientFilterRegistration(OAuth2ClientContextFilter filter) {
          FilterRegistrationBean registration = new FilterRegistrationBean();
          registration.setFilter(filter);
          registration.setOrder(-100);
          return registration;
      }
  
      @Bean
      @ConfigurationProperties("github")
      public ProviderResources github() {
          return new ProviderResources();
      }
  
      private Filter ssoFilter() {
          Facebook fb = new Facebook();
          CompositeFilter filter = new CompositeFilter();
          List&lt;Filter&gt; filters = new ArrayList&lt;&gt;();
          filters.add(ssoFilter(fb.getConfig(), "/login/facebook"));
          filters.add(ssoFilter(github(), "/login/github"));
          filter.setFilters(filters);
          return filter;
      }
  
      private Filter ssoFilter(ProviderResources client, String path) {
          OAuth2ClientAuthenticationProcessingFilter oAuth2ClientAuthenticationFilter
              = new OAuth2ClientAuthenticationProcessingFilter(path);
          OAuth2RestTemplate oAuth2RestTemplate
              = new OAuth2RestTemplate(client.getClient(), oauth2ClientContext);
  
          oAuth2ClientAuthenticationFilter.setRestTemplate(oAuth2RestTemplate);
          UserInfoTokenServices tokenServices = new UserInfoTokenServices(
                  client.getResource().getUserInfoUri(),
                  client.getClient().getClientId()
          );
  
          tokenServices.setRestTemplate(oAuth2RestTemplate);
          oAuth2ClientAuthenticationFilter.setTokenServices(tokenServices);
  
          return oAuth2ClientAuthenticationFilter;
      }
  
  }
  </code></pre>
  
  <p><em>com.github.example.providers.ProviderResources.java</em></p>
  
  <pre><code>package com.github.example.providers;
  
  import java.io.Serializable;
  import org.springframework.boot.autoconfigure.security.oauth2.resource.ResourceServerProperties;
  import org.springframework.boot.context.properties.NestedConfigurationProperty;
  import org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeResourceDetails;
  
  public class ProviderResources implements Serializable {
  
      private static final long serialVersionUID = 7526472295622776147L;
  
      @NestedConfigurationProperty
      private AuthorizationCodeResourceDetails client = new AuthorizationCodeResourceDetails();
  
      @NestedConfigurationProperty
      private ResourceServerProperties resource = new ResourceServerProperties();
  
      public AuthorizationCodeResourceDetails getClient() {
          return client;
      }
  
      public ResourceServerProperties getResource() {
          return resource;
      }
  }
  </code></pre>
  
  <p><em>com.github.example.providers.Facebook.java</em></p>
  
  <pre><code>package com.github.example.providers;
  
  import java.io.Serializable;
  
  import org.springframework.boot.context.properties.ConfigurationProperties;
  import org.springframework.context.annotation.Bean;
  import org.springframework.stereotype.Component;
  
  @Component
  @ConfigurationProperties("facebook")
  public class Facebook  implements Serializable {
      private static final long serialVersionUID = 7526472295622776147L;
  
      @Bean
      @ConfigurationProperties("facebook")
      public ProviderResources getConfig() {
          return new ProviderResources();
      }
  }
  </code></pre>
  
  <p><strong>Results</strong>
  In the broken source code, the github login still works, however, when attempting to login with Facebook, I get the following 500 error:</p>
  
  <pre><code>Whitelabel Error Page
  
  This application has no explicit mapping for /error, so you are seeing this as a fallback.
  
  Sun Jul 16 16:10:19 EDT 2017
  There was an unexpected error (type=Internal Server Error, status=500).
  HTTP URL must not be null
  </code></pre>
  
  <p><strong>Debugging Results</strong>
  While debugging, I noticed that the variables in ProviderResources() are null. I notice the same results when using the <em>@Value</em> annotation to set property values. The property values with the <em>@Value</em> annotation are set correctly in the main Application class, but are set to null in any other class.</p>
  
  <p>Again, I'm new to spring boot / java and I'm probably doing something newbie-ish. Thanks for the help in advance!</p>