How to load an external configuration with Spring Boot?
java, spring, spring-boot
Solution
First of all your `Test` class should be annotated with `@Component` in order for it to be registered as a bean by spring (also make sure all your classes are under your main package - the main package is where a class that is annotated with `@SpringBootApplication` reside).
Now you should either move all your properties to `application.yml` (`src/main/resources/application.yml`), that is picked automatically by spring boot (note that it should be `.yml` instead of `.yaml` or register a custom `PropertySourcesPlaceholderConfigurer`.
Example for `PropertySourcesPlaceholderConfigurer`:
@Bean
public static PropertySourcesPlaceholderConfigurer PropertySourcesPlaceholderConfigurer() throws IOException {
PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
MutablePropertySources propertySources = new MutablePropertySources();
Resource resource = new DefaultResourceLoader().getResource("classpath:application.yml");
YamlPropertySourceLoader sourceLoader = new YamlPropertySourceLoader();
PropertySource<?> yamlProperties = sourceLoader.load("yamlProperties", resource, null);
propertySources.addFirst(yamlProperties);
configurer.setPropertySources(propertySources);
return configurer;
}
Now your properties should be loaded to spring's environment and they will be available for injection with `@Value` to your beans.
Problem
I'm currently learning how to work with Spring Boot. Until now I never used Frameworks like Spring and used files directly (FileInputStream, etc.) 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. Here is some code to make clear what I'm searching for: ``` @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(); } } ``` 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. I also found that doc: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html but I have now idea how to apply this to my project. How can I achive this? Thank you in advance :) Edit: Here are some parts of my code: WatchdogBootstrap.java ``` 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); } } ``` Beans.xml (Located in default package) ``` <?xml version = "1.0" encoding = "UTF-8"?> <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"> <context:annotation-config></context:annotation-config> </beans> ``` Watchdog.java ``` 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); } // ... } ``` watchdog.yml (Located in src/main/resources) ``` watchdog: token: fghaepoghaporghaerg ```