如何使用Spring Boot加载外部配置? [英] How to load an external configuration with Spring Boot?
问题描述
我目前正在学习如何使用Spring Boot.到目前为止,我从未使用过像Spring这样的框架,也没有直接使用过文件(FileInputStream等)
I'm currently learning how to work with Spring Boot. Until now I never used Frameworks like Spring and used files directly (FileInputStream, etc.)
情况就是这样:我有一些动态配置值,例如OAuth令牌.我想在我的应用程序中使用它们,但是我不知道如何用Spring实现它们.
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();
}
}
当然,这个例子很简单.在这里,我想从YAML配置文件中动态获取值"token".用户必须可以访问此文件,并且该文件不应包含在JAR文件中.
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 :)
这是我的代码的某些部分:
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 (位于默认程序包中)
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 (位于src/main/resources中)
watchdog.yml (Located in src/main/resources)
watchdog:
token: fghaepoghaporghaerg
推荐答案
首先,您的Test
类应使用@Component
进行注释,以便在春季之前将其注册为bean(还请确保所有类位于您的主程序包下-主程序包位于带有@SpringBootApplication
注释的类).
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).
现在,您应该将所有属性移至application.yml
(src/main/resources/application.yml
),该属性将由Spring Boot自动选择(请注意,该属性应为.yml
而不是.yaml
)或注册自定义的PropertySourcesPlaceholderConfigurer
.
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
.
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中.
Now your properties should be loaded to spring's environment and they will be available for injection with @Value
to your beans.
这篇关于如何使用Spring Boot加载外部配置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!