title: SpringBoot Configuration with application.yml
body: <p>I have a little SpringBoot Application, which can execute different functions via OpenLdap.</p>
  
  <ul>
  <li><code>getUser</code></li>
  <li><code>createUser</code></li>
  <li><code>deleteUser</code></li>
  <li><code>etc.</code></li>
  </ul>
  
  <p>That works fine. Now i want to create an application.Yml, where i can manage different environments with different credentials. I read some tutorials, but i still have some understanding problems. Actually my code looks like that:</p>
  
  <p>UserController:</p>
  
  <pre><code>...
  protected static String serverURL = "xxxxx:90xx";
  protected static String LdapBindDn = "cn=admin, xxxxx";
  protected static String LdapPassword = "xxxx";
  ...
  
  @RequestMapping(value = "/{userid:.+}",method = RequestMethod.GET,consumes="application/json",produces = "application/json")
  public UserData getUser(@PathVariable String userid) {          
      DirContext context = connectToLdap();
      //some operations...
      context.close();    
      return user;
  }
  ... // same for the other functions
  </code></pre>
  
  <p>My plan is now, that i want to specify the credentials in an extra application.yml instead of at the beginning of the UserController (see above).</p>
  
  <p>Then i have created an application.yml in the src/main/resources:</p>
  
  <pre><code># Actual environment
  spring: 
    profiles.actives: development
  ---
  # Dev Profile 
  spring:
    profiles: dev  
  datasource:
    serverUrl: ldaps://xxxxxx:90xx
    AdminName: xxxx
    AdminPassword: xxxxxx
    BaseDN: xxxxx
  ---
  # Production Profile  
  spring:
    profiles: prod     
  datasource:
   serverUrl: ldaps://xxxx2:90xx
   AdminName: xxxxx2
   AdminPassword: xxxxx2
   BaseDN: xxxxxx
  </code></pre>
  
  <p>Now i need to call this configuration. I have read in one tutorial (<a href="http://therealdanvega.com/blog/2017/06/26/spring-boot-configuration-using-yaml" rel="nofollow noreferrer">http://therealdanvega.com/blog/2017/06/26/spring-boot-configuration-using-yaml</a>) that i have to create an extra class "ApplicationProperties" for the properties of the .yml file.</p>
  
  <pre><code>@Component
  @ConfigurationProperties("datasource")
  public class ApplicationProperties {
  
  private String serverURL;
  private String adminName;
  private String adminPassword;
  private String baseDN;
  
  // Getter-/Setter-Methods
  </code></pre>
  
  <p>}</p>
  
  <p>Now i need to define my variables from the beginning with the values from the .yml, right? I went back to my UserController and tried something like that:</p>
  
  <pre><code>private String serverURL;
  private String adminName;
  private String adminPassword;
  private String baseDN;  
  
  @Autowired   
  ApplicationProperties appProp;
  
  @RequestMapping(value = "/{userid:.+}",method = RequestMethod.GET,consumes="application/json",produces = "application/json")
  public UserData getUser(@PathVariable String userid) {          
      DirContext context = connectToLdap();
      //some operations...
      context.close();    
      return user;
  }
  ... // same for the other functions
  
  
  private DirContext connectToLdap(){
      System.out.prinln(appProp.getServerURL());
      System.out.prinln(appProp.getAdminName());
      System.out.prinln(appProp.getAdminPassword());
      .... // Code for the Ldap connection 
  }
  </code></pre>
  
  <p>But the variable "appProp" is still empty. I know, that here is somewhere a big understanding problem. I don't know how to call these properties from the .yml file.</p>
  
  <p>Thanks for every help in advance!</p>