title: How to load an external configuration with Spring Boot?
body: <p>I'm currently learning how to work with Spring Boot. Until now I never used Frameworks like Spring and used files directly (FileInputStream, etc.)</p>
  
  <p>So here is the case: I have some dynamic configuration values like OAuth tokens. I want to use them inside of my application but I have no clue how to realize this with Spring.</p>
  
  <p>Here is some code to make clear what I'm searching for:</p>
  
  <pre><code>@Config("app.yaml")
  public class Test {
      @Value("app.token")
      private String token;
      private IClient client;
  
      public Test(String token) {
          this.client = ClientFactory.build(token).login();
      }
  }
  </code></pre>
  
  <p>Sure, this example is very plain. Here I want to get the value "token" dynamically from a YAML configuration file. This file must be accessible for the user and not included in the JAR file.</p>
  
  <p>I also found that doc: <a href="https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html" rel="noreferrer">https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html</a> but I have now idea how to apply this to my project.</p>
  
  <p>How can I achive this? Thank you in advance :)</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>Here are some parts of my code:</p>
  
  <p><strong>WatchdogBootstrap.java</strong></p>
  
  <pre><code>package de.onkelmorph.watchdog;
  
  import org.springframework.boot.Banner.Mode;
  
  import org.springframework.boot.SpringApplication;
  import org.springframework.boot.autoconfigure.SpringBootApplication;
  import org.springframework.context.annotation.ImportResource;
  
  @SpringBootApplication
  @ImportResource("classpath:Beans.xml")
  public class WatchdogBootstrap {
      public static void main(String[] args) {
          SpringApplication app = new SpringApplication(WatchdogBeans.class);
          app.setBannerMode(Mode.OFF);
          app.setWebEnvironment(false);
          app.run(args);
      }
  }
  </code></pre>
  
  <p><strong>Beans.xml</strong> (Located in default package)</p>
  
  <pre><code>&lt;?xml version = "1.0" encoding = "UTF-8"?&gt;
  
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xmlns:context="http://www.springframework.org/schema/context"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-3.0.xsd"&gt;
  
      &lt;context:annotation-config&gt;&lt;/context:annotation-config&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p><strong>Watchdog.java</strong></p>
  
  <pre><code>package de.onkelmorph.watchdog;
  
  // Imports ...
  
  @Component
  @PropertySource("file:/watchdog.yml")
  public class Watchdog {
      // ...
  
      // Configuration
      @Value("${watchdog.token}")
      private String token;
  
      public Watchdog() {
          System.out.println(this.token);
          System.exit(0);
      }
  
      // ...
  }
  </code></pre>
  
  <p><strong>watchdog.yml</strong> (Located in src/main/resources)</p>
  
  <pre><code>watchdog:
    token: fghaepoghaporghaerg
  </code></pre>