最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring?Boot讀取配置文件的五種方式小結(jié)

 更新時間:2025年04月25日 10:50:33   作者:頗有幾分姿色  
Spring?Boot?提供了靈活多樣的方式來讀取配置文件,這篇文章為大家介紹了5種常見的讀取方式,文中的示例代碼簡潔易懂,大家可以根據(jù)自己的需要進行選擇

Spring Boot 提供了靈活多樣的方式來讀取配置文件(如 application.yml 或 application.properties),本文介紹幾種常見的讀取方式。

1. 配置文件位置與加載順序

Spring Boot 默認從以下位置加載配置文件(優(yōu)先級從高到低):

命令行參數(shù)(如:--server.port=8081)

application.properties / application.yml(位于 classpath:/config/)

classpath:/application.properties 或 application.yml

外部配置中心(如 Nacos、Spring Cloud Config)

2. 讀取配置文件的方式匯總

Spring Boot 提供了多種讀取配置文件的方式。

方式一:使用 @Value 注解讀取配置

這種方式適合讀取單個簡單配置項??梢杂脕碜⑷肱渲梦募械闹?,也可以指定默認值,防止配置項缺失時拋出異常 。

配置文件:

app:
  name: order-v
  version: v1

示例代碼:

@Component
public class AppProperties {

    @Value("${app.name:order}")
    private String name;

    @Value("${app.version:v1.0.0}")
    private String version;

    public void print() {
        System.out.println("App Name: " + name);
        System.out.println("App Version: " + version);
    }
}
  • : 后面就是默認值
  • 當配置文件中沒有對應(yīng)的值時,會使用默認值
  • 如果配置項存在對應(yīng)的值,默認值不生效

測試代碼:

@RestController
@RequestMapping("/test")
@AllArgsConstructor
public class TestController {

    private final AppProperties appProperties;

    @RequestMapping("/print")
    public void print() {
        appProperties.print();
    }
}

? 優(yōu)點:

簡單直接,適用于讀取單個變量

? 缺點:

不支持嵌套結(jié)構(gòu)、不支持批量綁定、不利于維護

方式二:使用 @ConfigurationProperties 自動綁定配置類(推薦)

適合綁定多個字段、嵌套結(jié)構(gòu)、List、Map等復(fù)雜配置。

配置文件:

app:
  name: order-v
  version: v1
  servers:
    - http://dev-server:8080
    - http://test-server:8081
    - http://prod-server:8082
  metadata:
    author: Alice
    version: 1.0.2
    website: https://emp.com
  modules:
    user:
      enabled: true
      path: /user
    admin:
      enabled: false
      path: /admin

配置類示例代碼:

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
@ConfigurationProperties(prefix = "app")
@Data
public class AppProperties {

    private String name;

    // List 示例
    private List<String> servers;

    // Map<String, String> 示例
    private Map<String, String> metadata;

    // Map<String, 自定義對象> 示例
    private Map<String, Module> modules;

    @Data
    public static class Module {
        private boolean enabled;
        private String path;
    }
    
    public void print() {
        System.out.println("name: " + name);
        System.out.println("servers: " + servers);
        System.out.println("metadata: " + metadata);
        System.out.println("modules: " + modules);
    }
}

使用示例:

import com.example.xiaoshitou.config.AppConfigProperties;
import com.example.xiaoshitou.config.AppProperties;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/test")
@AllArgsConstructor
public class TestController {

    private final AppProperties appProperties;
    private final AppConfigProperties appConfigProperties;

    @RequestMapping("/print")
    public void print() {
        appProperties.print();
    }

    @RequestMapping("/printConfig")
    public void printConfig() {
        appConfigProperties.print();
    }
}

? 優(yōu)點:

  • 支持對象結(jié)構(gòu)、嵌套對象、List、Map
  • 支持校驗(配合 @Validated)
  • 強類型綁定,IDE 支持友好

? 缺點:

需要額外的類定義

方式三:使用 Environment 編程式讀取配置

適合動態(tài)讀取、條件判斷場景。

示例代碼:

import com.example.xiaoshitou.config.AppConfigProperties;
import com.example.xiaoshitou.config.AppProperties;
import com.example.xiaoshitou.config.SmsConfig;
import lombok.AllArgsConstructor;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/***
 * @title
 * @author shijiangyong
 * @date 2025/4/24 17:28
 **/
@RestController
@RequestMapping("/test")
@AllArgsConstructor
public class TestController {

    private final AppProperties appProperties;
    private final AppConfigProperties appConfigProperties;
    private final Environment env;

    @RequestMapping("/print")
    public void print() {
        appProperties.print();
    }

    @RequestMapping("/printConfig")
    public void printConfig() {
        appConfigProperties.print();
    }

    @RequestMapping("/printEnv")
    public void printEnv() {
        String name = env.getProperty("app.name");
        System.out.println("app.name = " + name);
    }

}

? 優(yōu)點:

  • 動態(tài)、靈活,支持條件判斷
  • 可用于第三方庫或工具類中

? 缺點:

可讀性差,不支持自動綁定

方式四:加載自定義配置文件(@PropertySource)

當你需要讀取非 application.yml 的配置文件時使用。

自定義配置文件 sms-config.properties:

sms.sign=aliyun
sms.template.code=TPL001

示例代碼:

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

/***
 * @title
 * @author shijiangyong
 * @date 2025/4/25 10:08
 **/
@Configuration
@PropertySource("classpath:sms-config.properties")
@ConfigurationProperties(prefix = "sms")
@Data
public class SmsConfig {
    private String sign;
    private Template template;

    @Data
    public static class Template {
        private String code;
    }

    public void print() {
        System.out.println("sign : " + sign);
        System.out.println("template.code : " + template.getCode());
    }
}

使用示例:

import com.example.xiaoshitou.config.AppConfigProperties;
import com.example.xiaoshitou.config.AppProperties;
import com.example.xiaoshitou.config.SmsConfig;
import lombok.AllArgsConstructor;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/***
 * @title
 * @author shijiangyong
 * @date 2025/4/24 17:28
 **/
@RestController
@RequestMapping("/test")
@AllArgsConstructor
public class TestController {

    private final AppProperties appProperties;
    private final AppConfigProperties appConfigProperties;
    private final SmsConfig smsConfig;
    private final Environment env;

    @RequestMapping("/print")
    public void print() {
        appProperties.print();
    }

    @RequestMapping("/printConfig")
    public void printConfig() {
        appConfigProperties.print();
    }

    @RequestMapping("/printEnv")
    public void printEnv() {
        String name = env.getProperty("app.name");
        System.out.println("app.name = " + name);
    }


    @RequestMapping("/smsConfig")
    public void smsConfig() {
        smsConfig.print();
    }

}

? 優(yōu)點:

  • 支持加載自定義配置文件
  • 可與 @ConfigurationProperties 搭配使用

? 缺點:

  • 不支持 .yml 格式
  • 不支持動態(tài)刷新

方式五:多環(huán)境配置(多 Profile)

適用于開發(fā)、測試、生產(chǎn)環(huán)境的配置隔離。

配置文件:

# application-dev.yml
app:
  name: DevApp

# application-prod.yml
app:
  name: ProdApp

激活方式:

在 application.yml 中設(shè)置:

spring:
  profiles:
    active: dev

或通過啟動參數(shù):

--spring.profiles.active=prod

? 優(yōu)點:

  • 多環(huán)境隔離,配置清晰
  • 支持組合激活多個 profile

總結(jié)

場景推薦方式
讀取單個簡單值@Value
讀取嵌套結(jié)構(gòu)、對象@ConfigurationProperties
動態(tài)讀取Environment
非默認配置文件@PropertySource
多環(huán)境支持多 profile

以上就是Spring Boot讀取配置文件的五種方式小結(jié)的詳細內(nèi)容,更多關(guān)于Spring Boot讀取配置文件的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Spring MVC攔截器(Interceptor)的定義和配置過程

    Spring MVC攔截器(Interceptor)的定義和配置過程

    這篇文章主要介紹了Spring MVC攔截器(Interceptor)的定義和配置過程,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2025-03-03
  • SpringBoot集成DJL實現(xiàn)圖片分類功能

    SpringBoot集成DJL實現(xiàn)圖片分類功能

    DJL是一個使用Java?API簡化模型訓(xùn)練、測試、部署和使用深度學(xué)習(xí)模型進行推理的開源庫深度學(xué)習(xí)工具包,開源的許可協(xié)議是Apache-2.0,本文給大家介紹了SpringBoot集成DJL實現(xiàn)圖片分類功能,需要的朋友可以參考下
    2024-10-10
  • jvm原理之SystemGC源碼分析

    jvm原理之SystemGC源碼分析

    這篇文章主要介紹了jvm源碼分析之SystemGC的完全解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-01-01
  • java接口冪等性的實現(xiàn)方式

    java接口冪等性的實現(xiàn)方式

    本文介紹了在不同層面上實現(xiàn)Java接口冪等性的方法,包括使用冪等表、Nginx+Lua和Redis、以及SpringAOP,通過這些方法,可以確保接口在多次請求時只執(zhí)行一次,避免重復(fù)處理和數(shù)據(jù)不一致,每種方法都有其適用場景和優(yōu)勢,通過實際測試驗證了冪等性邏輯的有效性
    2025-01-01
  • Java編程訪問權(quán)限的控制代碼詳解

    Java編程訪問權(quán)限的控制代碼詳解

    這篇文章主要介紹了Java編程訪問權(quán)限的控制代碼詳解,涉及包名,公共的和私有的等相關(guān)內(nèi)容,分享了相關(guān)代碼示例,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下
    2018-01-01
  • Springboot 1.5.7整合Kafka-client代碼示例

    Springboot 1.5.7整合Kafka-client代碼示例

    這篇文章主要介紹了Springboot 1.5.7整合Kafka-client代碼示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-10-10
  • 手把手教你寫一個SpringBoot+gRPC服務(wù)

    手把手教你寫一個SpringBoot+gRPC服務(wù)

    本文將在本地環(huán)境下搭建gRPC客戶端和服務(wù)端,并成功建立通訊發(fā)送消息的方式,從而幫助大家深入了解gRPC在Spring Boot項目中的應(yīng)用,有需要的小伙伴可以參考下
    2023-12-12
  • idea out目錄與target目錄的區(qū)別詳解

    idea out目錄與target目錄的區(qū)別詳解

    這篇文章主要介紹了idea out目錄與target目錄的區(qū)別詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • 關(guān)于springmvc-servlet中的配置小知識詳解

    關(guān)于springmvc-servlet中的配置小知識詳解

    這篇文章主要介紹了關(guān)于springmvc-servlet中的配置小知識詳解,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-12-12
  • Java數(shù)據(jù)結(jié)構(gòu)和算法之冒泡,選擇和插入排序算法

    Java數(shù)據(jù)結(jié)構(gòu)和算法之冒泡,選擇和插入排序算法

    這篇文章主要為大家介紹了Java冒泡,選擇和插入排序算法 ,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-01-01

最新評論

玉山县| 周至县| 达孜县| 崇仁县| 和田县| 天峨县| 定远县| 山阳县| 巴中市| 富裕县| 哈密市| 手游| 邢台市| 郎溪县| 佛坪县| 辛集市| 兰溪市| 凤冈县| 资中县| 三穗县| 沧州市| 南木林县| 云霄县| 昆山市| 景宁| 大厂| 商城县| 明光市| 临江市| 阿图什市| 荣昌县| 赣榆县| 凭祥市| 青阳县| 沙坪坝区| 澜沧| 山东省| 姚安县| 易门县| 营山县| 阳江市|