摘要:Spring Boot支持通过特定的方式加载JSON配置文件。开发者可以创建一个JSON格式的配置文件,并使用@ConfigurationProperties注解将其绑定到应用程序的配置类中。这样,在应用程序启动时,SPRING Boot会自动解析JSON配置
环境:SpringBoot3.4.0
1. 简介
Spring Boot支持通过特定的方式加载JSON配置文件。开发者可以创建一个JSON格式的配置文件,并使用@ConfigurationProperties注解将其绑定到应用程序的配置类中。这样,在应用程序启动时,SPRING Boot会自动解析JSON配置文件,并将其内容添加到应用程序的环境中。
此外,Spring Boot还允许在命令行中通过spring.application.json属性或SPRING_APPLICATION_JSON环境变量提供JSON数据,从而在应用程序启动时动态地配置属性。这种方式为开发者和运维人员提供了更大的灵活性和便利性。
本篇文章将介绍几种配置读取JSON格式数据作为应用程序配置属性的方式。
2. 实战案例
2.1 从命令行加载JSON配置
当应用程序启动时,任何名为spring.application.json的属性或SPRING_APPLICATION_JSON环境变量都会被解析并添加到应用程序的环境中。
例如,在UN*X shell中,可以通过环境变量的方式在命令行上提供SPRING_APPLICATION_JSON属性:
$ SPRING_APPLICATION_JSON="{\"pack\":{\"title\":\"xxxooo\"}}" java -jar myapp.jar在前面的例子中,最终在Spring环境中得到了pack.title=xxxooo这个属性。
通过系统属性配置:
java -Dspring.application.json="{\"pack\":{\"title\":\"xxxooo\"}}" -jar load_json-1.0.0.jar通过命令行参数:
java -jar load_json-1.0.0.jar --spring.application.json="{\"pack\":{\"title\":\"xxxooo\"}}"这些是将JSON数据加载到我们的应用程序中最简单的方法。但这种极简方法的缺点是缺乏可扩展性。在命令行中加载大量数据可能会很繁琐且容易出错。
2.2 通过@PropertySource注解
首先,我们定义配置对象
public class JsonProperties { private Integer port ; private String host ; // getters, setters}其次,定义JSON文件内容
{ "port": 6666, "host": "127.0.0.1"}最后,我们在JsonProperties类上添加如下注解:
@Component@PropertySource(value = "classpath:configprops.json")@ConfigurationPropertiespublic class JsonProperties {}启动应用报错了:
莫名其妙的多了一个 "," 逗号。
要解决此问题,我们需要自定义PropertySourceFactory。
public class JsonPropertySourceFactory implements PropertySourceFactory { @Override public PropertySource createPropertySource(String name, EncodedResource Resource) throws IOException { Map readValue = new ObjectMapper.readValue(resource.getInputStream, Map.class); return new MapPropertySource("json-property", readValue); }}修改JsonProperties配置
@Component@PropertySource( value = "classpath:configprops.json", factory = JsonPropertySourceFactory.class)@ConfigurationPropertiespublic class JsonProperties {}启动服务后,输出如下:
成功读取JSON文件内容。
2.3 JSON嵌套结构
如果你的定义格式中有嵌套的结构,那么你可以通过一个Map来接收,如下JSON格式:
{ "port": 6666, "host": "127.0.0.1", "pack": { "title": "xxxooo", "author": "pack" }}修改JsonProperties如下:
@Component@PropertySource( value = "classpath:configprops.json", factory = JsonPropertySourceFactory.class)@ConfigurationPropertiespublic class JsonProperties { private Integer port ; private String host ; private LinkedHashMap pack ;}如上会将pack节点映射到上面的LinkedHashMap集合中。
2.4 使用ApplicationContextInitializer读取
如果我们想要对属性的加载拥有更多的控制权,我们可以使用自定义的ApplicationContextInitializer。这种手动方法更为繁琐。但是,作为结果,我们将能够完全控制数据的加载和解析。
public class JsonPropertyContextInitializer implements ApplicationContextInitializer { private static String CUSTOM_PREFIX = "custom."; @Override public void initialize(ConfigurableApplicationContext configurableApplicationContext) { try { Resource resource = configurableApplicationContext.getResource("classpath:configprops.json"); Map readValue = new ObjectMapper.readValue(resource.getInputStream, Map.class); Set set = readValue.entrySet; List propertySources = set.stream .map(entry -> new MapPropertySource(CUSTOM_PREFIX + entry.getKey, Collections.singletonMap(CUSTOM_PREFIX + entry.getKey, entry.getValue))) .collect(Collectors.toList); for (PropertySource propertySource : propertySources) { configurableApplicationContext.getEnvironment.getPropertySources.addFirst(propertySource); } } catch (IOException e) { throw new RuntimeException(e); } }}在上面的代码中我们会对JSON文件中配置的每一个属性加一个 "custom." 前缀,所以在访问时需要通过如下的方式进行访问:
@Componentpublic class CustomJsonProperties { @Value("${custom.port}") private Integer port ; @Value("${custom.host}") private String host ;}启动服务输出如下:
通过ApplicationContextInitilizater我们能做更多的控制。
2.5 直接定义Bean方式读取
我们直接通过@Bean+ObjectMapper的方式读取JSON文件:
@Configurationpublic class AppConfig { @Bean AppProperties appProperties throws IOException { ObjectMapper mapper = new ObjectMapper ; return mapper.readValue( new ClassPathResource("configprops.json").getInputStream, AppProperties.class ); }}以上就是通过读取JSON文件实现Spring Boot配置属性的所有方法。
来源:散文随风想