成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

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

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

在 Spring Boot 3.4 中,JSON 配置文件管理提供了更靈活、高效的方式。開發者可以利用 @ConfigurationProperties 輕松綁定 JSON 配置,使應用程序啟動時自動解析 JSON 數據,并將其加載到應用環境中。此外,還可以通過 spring.application.json 參數或 SPRING_APPLICATION_JSON 環境變量傳遞 JSON 數據,增強動態配置能力。本文將詳細介紹如何使用 JSON 管理 Spring Boot 配置。

通過命令行加載 JSON 配置

在 Spring Boot 啟動時,可以直接使用 spring.application.json 或 SPRING_APPLICATION_JSON 來動態提供 JSON 配置。

  • 使用環境變量:
SPRING_APPLICATION_JSON='{"server":{"port":8080}}' java -jar myapp.jar

這樣,應用程序的 server.port 將被設置為 8080。

  • 通過 JVM 參數:
java -Dspring.application.json='{"server":{"port":8080}}' -jar myapp.jar
  • 作為啟動參數:
java -jar myapp.jar --spring.application.json='{"server":{"port":8080}}'

盡管這種方式簡單直接,但當 JSON 數據量較大時,手動維護會變得復雜,因此建議采用文件方式進行管理。

通過 @PropertySource 讀取 JSON 配置

配置 JSON 文件

創建 JSON 配置文件 configprops.json,示例如下:

{
  "port": 8080,
  "host": "127.0.0.1"
}

創建 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 嵌套結構

當 JSON 結構中包含嵌套字段時,可以使用 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);
    }
}

總結

通過以上幾種方法,可以輕松地使用 JSON 文件管理 Spring Boot 3.4 的應用配置。其中,使用 @PropertySource 結合 PropertySourceFactory 是最直觀的方式,而 ApplicationContextInitializer 適用于更高級的自定義需求。如果你想在 Spring Boot 項目中靈活管理配置,JSON 絕對是一個高效的選擇!

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

2025-01-13 13:47:13

2025-05-14 01:00:00

Spring工具工廠類

2025-01-22 14:02:35

2025-04-10 00:25:00

Spring@JsonView注解

2020-12-31 11:28:09

GitLabCICD

2025-04-08 01:00:00

Spring開發系統

2025-04-02 04:55:00

2022-05-30 16:31:08

CSS

2025-02-08 08:00:00

JavaDeepSeekIDEA

2019-01-15 11:40:14

開發技能代碼

2025-01-08 10:35:26

代碼開發者Spring

2025-04-14 04:01:00

2022-06-06 12:18:44

配置可視化Nginx

2021-03-04 09:31:42

開源技術 項目

2025-06-26 01:22:00

SpringBean開發

2021-08-05 16:25:37

Windows 11Windows微軟

2022-06-08 08:01:28

模板字面量類型

2024-01-30 09:21:29

CSS文字效果文字裝飾

2023-04-28 15:15:39

數據庫JPA

2023-12-10 20:33:50

Redis搜索全文
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 免费看啪啪网站 | 亚洲天天干 | 人妖无码 | 一区二区三区四区不卡视频 | 国产亚洲人成a在线v网站 | 日韩在线高清 | 欧美日韩一 | 国产成人一区在线 | 国外成人在线视频 | 超碰免费在线 | 亚洲播放一区 | 婷婷久久综合 | 免费成人高清在线视频 | 久草在线 | 青青久草| 超碰免费在线 | 99久久电影 | 视频二区在线观看 | 天天操夜夜操 | 欧美国产日韩一区二区三区 | 日韩精品专区在线影院重磅 | 亚洲精品视频在线看 | 亚洲视频在线一区 | 色视频在线观看 | 成人精品国产 | 波多野结衣一区二区 | 精品日韩电影 | 亚洲人成人一区二区在线观看 | 成人性生交大片免费看中文带字幕 | 欧美视频三级 | 网色 | 激情六月丁香婷婷 | 国产成人久久精品一区二区三区 | 色狠狠桃花综合 | 亚洲精品白浆高清久久久久久 | 久久一区二区视频 | 欲色av| 亚洲 中文 欧美 日韩 在线观看 | 国产剧情久久 | 亚洲精品av在线 | 国际精品鲁一鲁一区二区小说 |