Mor*_*rph 4 java spring 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();
}
}
Run Code Online (Sandbox Code Playgroud)
当然,这个例子很简单。在这里,我想从YAML配置文件中动态获取值“ token”。该文件必须可供用户访问,并且不包含在JAR文件中。
我还发现了该文档: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);
}
}
Run Code Online (Sandbox Code Playgroud)
Beans.xml(位于默认包中)
<?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>
Run Code Online (Sandbox Code Playgroud)
看门狗
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);
}
// ...
}
Run Code Online (Sandbox Code Playgroud)
watchdog.yml(位于src / main / resources中)
watchdog:
token: fghaepoghaporghaerg
Run Code Online (Sandbox Code Playgroud)
首先,Test应该为您的类添加注释,@Component以便在春季之前将其注册为Bean(还要确保所有类都位于您的主程序包下-主程序包所在的是带有注释的类@SpringBootApplication)。
现在,您应该将所有属性移到application.yml(src/main/resources/application.yml)(由Spring Boot自动选择)(请注意,它应.yml代替.yaml或注册一个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;
}
Run Code Online (Sandbox Code Playgroud)
现在,应该将属性加载到春天的环境中,并且可以将其注入@Value到您的bean中。
| 归档时间: |
|
| 查看次数: |
12572 次 |
| 最近记录: |