从json文件加载spring-boot属性

And*_*y W 5 java spring json properties spring-boot

是否可以从.json文件而不是.yaml或.properties加载spring-boot配置?从文档上看,这不是开箱即用的-我想知道是否有可能,如果可以,怎么办?

Kir*_*ill 10

文档GitHub 中所述

YAML 是 JSON 的超集

所以你可以在你的 Spring Boot 项目中创建以下类:

public class JsonPropertySourceLoader extends YamlPropertySourceLoader {
    @Override
    public String[] getFileExtensions() {
        return new String[]{"json"};
    }
}
Run Code Online (Sandbox Code Playgroud)

然后创建一个文件:

/src/main/resources/META-INF/spring.factories 具有以下内容:

org.springframework.boot.env.PropertySourceLoader=\
io.myapp.JsonPropertySourceLoader
Run Code Online (Sandbox Code Playgroud)

您的 Spring 应用程序已准备好从application.json. 优先级将是:.properties -> .yaml -> .json

如果您有多个应用程序,您可以使用共享PropertySourceLoaderspring.factories文件创建一个 jar,以便将其包含到您需要的任何项目中。


pan*_*adb 6

弹簧启动方式:

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

    }
}
Run Code Online (Sandbox Code Playgroud)

定义您自己的PropertySourceFactory并通过@PropertySource注释将其挂钩。阅读资源,设置属性,在任何地方使用它们。

唯一的问题是,您如何翻译嵌套属性。Spring 的方式来做到这一点(顺便说一下,您也可以将 Json 定义为属性的变量,请参阅:https : //docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external -config.html ) 是这样翻译嵌套属性的:

{"test": { "test2" : "x" } }
Run Code Online (Sandbox Code Playgroud)

变成:

test.test2.x
Run Code Online (Sandbox Code Playgroud)

希望有所帮助,

阿图尔


Sta*_*avL 0

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;
}
Run Code Online (Sandbox Code Playgroud)

帖子里得到的

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);
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

帖子里得到的

所以你可以将两者结合起来。将 json 作为资源加载并转换为 yaml,然后将找到的所有属性添加到环境中