自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

太強了!用 JSON 文件管理 Spring Boot 配置,簡單又高效!

開發(fā) 前端
使用 @PropertySource? 結(jié)合 PropertySourceFactory? 是最直觀的方式,而 ApplicationContextInitializer 適用于更高級的自定義需求。如果你想在 Spring Boot 項目中靈活管理配置,JSON 絕對是一個高效的選擇!?

在 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 絕對是一個高效的選擇!

責任編輯:武曉燕 來源: Springboot全家桶實戰(zhàn)案例源碼
相關推薦

2025-01-13 13:47:13

2025-01-22 14:02:35

2025-04-10 00:25:00

Spring@JsonView注解

2020-12-31 11:28:09

GitLabCICD

2022-05-30 16:31:08

CSS

2025-02-08 08:00:00

JavaDeepSeekIDEA

2025-04-08 01:00:00

Spring開發(fā)系統(tǒng)

2025-04-02 04:55:00

2021-03-04 09:31:42

開源技術 項目

2025-04-14 04:01:00

2019-01-15 11:40:14

開發(fā)技能代碼

2025-01-08 10:35:26

代碼開發(fā)者Spring

2022-06-06 12:18:44

配置可視化Nginx

2023-12-10 20:33:50

Redis搜索全文

2024-01-30 09:21:29

CSS文字效果文字裝飾

2021-08-05 16:25:37

Windows 11Windows微軟

2022-06-08 08:01:28

模板字面量類型

2023-04-28 15:15:39

數(shù)據(jù)庫JPA

2025-01-13 12:46:31

SpringBootJacksonJSON

2021-09-15 08:45:55

Python文本文件代碼
點贊
收藏

51CTO技術棧公眾號