如何使用Spring Boot加载外部配置?

我目前正在学习如何使用Spring Boot。 到目前为止,我从未使用像Spring这样的框架并直接使用文件(FileInputStream等)

所以情况就是这样:我有一些动态配置值,比如OAuth令牌。 我想在我的应用程序中使用它们,但我不知道如何用Spring实现这一点。

这是一些代码,以明确我正在搜索的内容:

@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(); } } 

当然,这个例子很简单。 在这里,我想从YAML配置文件中动态获取值“token”。 此文件必须可供用户访问且不包含在JAR文件中。

我还发现doc: https : //docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html但我现在知道如何将它应用到我的项目中。

我怎么能得到这个? 先感谢您 :)

编辑:

以下是我的代码的一些部分:

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 (位于默认包中)

     

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 (位于src / main / resources)

 watchdog: token: fghaepoghaporghaerg 

首先,你的Test类应该用@Component注释,以便它在spring之前注册为bean(同时确保你的所有类都在你的主包下 – 主包是用@SpringBootApplication注释的类的@SpringBootApplication居住)。

现在你应该将所有属性移动到application.ymlsrc/main/resources/application.yml ),这是由spring boot自动选取的(注意它应该是.yml而不是.yaml或者注册一个自定义的PropertySourcesPlaceholderConfigurer

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; } 

现在你的属性应该被加载到spring的环境中,并且它们可以通过@Value注入你的bean。

你基本上有三个简单的选择。

  1. 使用application.properties是Springs内部配置文件。
  2. 使用--spring.config.name作为VM参数加载您自己的配置文件。
  3. 您可以使用@PropertySource加载内部或外部配置。 @PropertySource仅适用于.properties配置文件。 目前有一个开放的Jira票据来实现yaml支持。 您可以按照以下步骤进行操作: https : //jira.spring.io/browse/SPR-13912

请注意,如果您使用包含公共密钥的多个yaml和/或属性文件,它将始终使用最后加载的密钥的定义。 这就是下面的示例使用两个不同的键的原因。 如果它使用相同的密钥,那么它将打印两次PROPERTIES FILE

简短的代码片段:

 @Component @PropertySource("file:/path/to/config/app.properties") class Address{ @Value("${addr.street}") private String street; @Value("${addr.city}") private String city; } 

app.properties

 addr.street=Abbey Road addr.city=London 

广泛的例子

DemoApplication.java

 @SpringBootApplication public class DemoApplication { public static void main(String[] args) { ApplicationContext context = SpringApplication.run(DemoApplication.class, args); //Call class with properties context.getBean(WatchdogProperties.class).test(); //Call class with yaml context.getBean(WatchdogYaml.class).test(); } //Define configuration file for yaml @Bean public static PropertySourcesPlaceholderConfigurer properties() { PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer(); YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean(); yaml.setResources(new ClassPathResource("watchdog.yml")); propertySourcesPlaceholderConfigurer.setProperties(yaml.getObject()); return propertySourcesPlaceholderConfigurer; } } 

WatchdogProperties.java

 @Component //PropertySource only works for .properties files @PropertySource("classpath:watchdog.properties") public class WatchdogProperties{ //Notice the key name is not the same as the yaml key @Value("${watchdog.prop.token}") private String token; public void test(){ System.out.println(token); } } 

WatchdogYaml.java

 @Component class WatchdogYaml{ //Notice the key name is not the same as the properties key @Value("${watchdog.token}") private String token; public void test(){ System.out.println(token); } } 

属性和Yaml文件这两个文件都位于src/main/resources

watchdog.yml:

 watchdog: token: YAML FILE 

watchdog.properties:

 watchdog.prop.token=PROPERTIES FILE 

产量

 PROPERTIES FILE YAML FILE