太強了!用 JSON 文件管理 Spring Boot 配置,簡單又高效!
在 Spring Boot 3.4 中,JSON 配置文件管理提供了更靈活、高效的方式。開發(fā)者可以利用 @ConfigurationProperties 輕松綁定 JSON 配置,使應用程序啟動時自動解析 JSON 數(shù)據(jù),并將其加載到應用環(huán)境中。此外,還可以通過 spring.application.json 參數(shù)或 SPRING_APPLICATION_JSON 環(huán)境變量傳遞 JSON 數(shù)據(jù),增強動態(tài)配置能力。本文將詳細介紹如何使用 JSON 管理 Spring Boot 配置。
通過命令行加載 JSON 配置
在 Spring Boot 啟動時,可以直接使用 spring.application.json 或 SPRING_APPLICATION_JSON 來動態(tài)提供 JSON 配置。
- 使用環(huán)境變量:
SPRING_APPLICATION_JSON='{"server":{"port":8080}}' java -jar myapp.jar
這樣,應用程序的 server.port 將被設置為 8080。
- 通過 JVM 參數(shù):
java -Dspring.application.json='{"server":{"port":8080}}' -jar myapp.jar
- 作為啟動參數(shù):
java -jar myapp.jar --spring.application.json='{"server":{"port":8080}}'
盡管這種方式簡單直接,但當 JSON 數(shù)據(jù)量較大時,手動維護會變得復雜,因此建議采用文件方式進行管理。
通過 @PropertySource 讀取 JSON 配置
配置 JSON 文件
創(chuàng)建 JSON 配置文件 configprops.json,示例如下:
{
"port": 8080,
"host": "127.0.0.1"
}
創(chuàng)建 Java 配置類
package com.icoderoad.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@PropertySource(value = "classpath:configprops.json", factory = JsonPropertySourceFactory.class)
@ConfigurationProperties
public class JsonProperties {
private Integer port;
private String host;
// Getters and Setters
}
自定義 PropertySourceFactory
package com.icoderoad.config;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertySourceFactory;
import java.io.IOException;
import java.util.Map;
public class JsonPropertySourceFactory implements PropertySourceFactory {
@Override
public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
Map<String, Object> readValue = new ObjectMapper().readValue(resource.getInputStream(), Map.class);
return new MapPropertySource("json-property", readValue);
}
}
處理 JSON 嵌套結(jié)構
當 JSON 結(jié)構中包含嵌套字段時,可以使用 Map 類型來映射子對象。
{
"port":8080,
"host":"127.0.0.1",
"database":{
"url":"jdbc:mysql://localhost:3306/test",
"username":"root"
}
}
修改 Java 配置類:
package com.icoderoad.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import java.util.Map;
@Component
@PropertySource(value = "classpath:configprops.json", factory = JsonPropertySourceFactory.class)
@ConfigurationProperties
public class JsonProperties {
private Integer port;
private String host;
private Map<String, Object> database;
// Getters and Setters
}
使用 ApplicationContextInitializer 讀取 JSON 配置
如果需要更細粒度的控制,可以使用 ApplicationContextInitializer 進行手動加載。
package com.icoderoad.config;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.io.ClassPathResource;
import java.io.IOException;
import java.util.Map;
public class JsonPropertyContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(ConfigurableApplicationContext context) {
try {
Map<String, Object> jsonMap = new ObjectMapper().readValue(new ClassPathResource("configprops.json").getInputStream(), Map.class);
context.getEnvironment().getPropertySources().addFirst(new MapPropertySource("json-config", jsonMap));
} catch (IOException e) {
throw new RuntimeException("加載 JSON 配置失敗", e);
}
}
}
在 application.yml 中注冊 ApplicationContextInitializer:
context:
initializer:
classes: com.icoderoad.config.JsonPropertyContextInitializer
通過 @Bean 方式直接讀取 JSON
另一種方式是使用 @Bean 讀取 JSON 配置。
package com.icoderoad.config;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import java.io.IOException;
@Configuration
public class AppConfig {
@Bean
public JsonProperties jsonProperties() throws IOException {
return new ObjectMapper().readValue(new ClassPathResource("configprops.json").getInputStream(), JsonProperties.class);
}
}
總結(jié)
通過以上幾種方法,可以輕松地使用 JSON 文件管理 Spring Boot 3.4 的應用配置。其中,使用 @PropertySource 結(jié)合 PropertySourceFactory 是最直觀的方式,而 ApplicationContextInitializer 適用于更高級的自定義需求。如果你想在 Spring Boot 項目中靈活管理配置,JSON 絕對是一個高效的選擇!