java 从 json 文件加载 spring-boot 属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44564166/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Load spring-boot properties from json file
提问by Andy W
Is it possible to load spring-boot config from a .json file as opposed to .yaml or .properties? From looking at the documentation, this isn't supported out of the box - I'm wondering if it's possible and if so how one would go about doing it?
是否可以从 .json 文件而不是 .yaml 或 .properties 加载 spring-boot 配置?从查看文档来看,这不是开箱即用的 - 我想知道是否有可能,如果有的话,人们将如何去做?
回答by pandaadb
The spring boot way:
弹簧启动方式:
@EnableAutoConfiguration
@Configuration
@PropertySource(value = { "classpath:/properties/config.default.json" }, factory=SpringBootTest.JsonLoader.class )
public class SpringBootTest extends SpringBootServletInitializer {
@Bean
public Object test(Environment e) {
System.out.println(e.getProperty("test"));
return new Object();
}
public static void main(String[] args) {
SpringApplication.run(SpringBootTest.class);
}
public static class JsonLoader implements PropertySourceFactory {
@Override
public org.springframework.core.env.PropertySource<?> createPropertySource(String name,
EncodedResource resource) throws IOException {
Map readValue = new ObjectMapper().readValue(resource.getInputStream(), Map.class);
return new MapPropertySource("json-source", readValue);
}
}
}
Define your own PropertySourceFactory
and hook it in via the @PropertySource
annotation. Read the resource, set the properties, use them anywhere.
定义您自己的PropertySourceFactory
并通过@PropertySource
注释将其挂钩。阅读资源,设置属性,在任何地方使用它们。
Only thing is, how do you translate nested properties. The Spring way to do that (by the way you can define Json also as a variable for properties, see: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html) is to translate nested properties as such:
唯一的问题是,您如何翻译嵌套属性。Spring 的方式来做到这一点(顺便说一下,您也可以将 Json 定义为属性的变量,请参阅:https: //docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external -config.html) 是这样翻译嵌套属性的:
{"test": { "test2" : "x" } }
Becomes:
变成:
test.test2.x
Hope that helps,
希望有所帮助,
Artur
阿图尔
回答by Kirill
YAML is a superset of JSON
YAML 是 JSON 的超集
So you can just create the following class in your Spring Boot project:
所以你可以在你的 Spring Boot 项目中创建以下类:
public class JsonPropertySourceLoader extends YamlPropertySourceLoader {
@Override
public String[] getFileExtensions() {
return new String[]{"json"};
}
}
Then create a file:
然后创建一个文件:
/src/main/resources/META-INF/spring.factories
with the following content:
/src/main/resources/META-INF/spring.factories
具有以下内容:
org.springframework.boot.env.PropertySourceLoader=\
io.myapp.JsonPropertySourceLoader
And your Spring application is ready to load JSON configurations from application.json
. The priority will be: .properties -> .yaml -> .json
您的 Spring 应用程序已准备好从application.json
. 优先级将是:.properties -> .yaml -> .json
If you have multiple apps, you can create a jar with the shared PropertySourceLoader
and spring.factories
file in order to include it to any project you need.
如果您有多个应用程序,您可以使用共享PropertySourceLoader
和spring.factories
文件创建一个 jar,以便将其包含到您需要的任何项目中。
回答by Ashu
The SPRING_APPLICATION_JSON properties can be supplied on the command line with an environment variable. For example, you could use the following line in a UN*X shell:
SPRING_APPLICATION_JSON 属性可以在带有环境变量的命令行上提供。例如,您可以在 UN*X shell 中使用以下行:
$ SPRING_APPLICATION_JSON='{"acme":{"name":"test"}}' java -jar myapp.jar
$ SPRING_APPLICATION_JSON='{"acme":{"name":"test"}}' java -jar myapp.jar
In the preceding example, you end up with acme.name=test in the Spring Environment. You can also supply the JSON as spring.application.json in a System property, as shown in the following example:
在前面的示例中,您最终在 Spring 环境中使用 acme.name=test。您还可以在 System 属性中将 JSON 作为 spring.application.json 提供,如以下示例所示:
$ java -Dspring.application.json='{"name":"test"}' -jar myapp.jar
$ java -Dspring.application.json='{"name":"test"}' -jar myapp.jar
You can also supply the JSON by using a command line argument, as shown in the following example:
您还可以使用命令行参数提供 JSON,如以下示例所示:
$ java -jar myapp.jar --spring.application.json='{"name":"test"}'
$ java -jar myapp.jar --spring.application.json='{"name":"test"}'
You can also supply the JSON as a JNDI variable, as follows:
您还可以将 JSON 作为 JNDI 变量提供,如下所示:
java:comp/env/spring.application.json.
java:comp/env/spring.application.json。
Reference documentation: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html
参考文档:https: //docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html
回答by StanislavL
2 steps
2 步
public String asYaml(String jsonString) throws JsonProcessingException, IOException {
// parse JSON
JsonNode jsonNodeTree = new ObjectMapper().readTree(jsonString);
// save it as YAML
String jsonAsYaml = new YAMLMapper().writeValueAsString(jsonNodeTree);
return jsonAsYaml;
}
Got from the post
从帖子中得到
and
和
public class YamlFileApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
try {
Resource resource = applicationContext.getResource("classpath:file.yml");
YamlPropertySourceLoader sourceLoader = new YamlPropertySourceLoader();
PropertySource<?> yamlTestProperties = yamlTestProperties = sourceLoader.load("yamlTestProperties", resource, null);
applicationContext.getEnvironment().getPropertySources().addFirst(yamlTestProperties);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
Got from the post
从帖子中得到
So you can combine both. Load your json as resource and convert to yaml and then add to Environment all the found properties
所以你可以将两者结合起来。将您的 json 作为资源加载并转换为 yaml,然后将所有找到的属性添加到 Environment