欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot讀取配置文件的五種方法總結(jié)

 更新時間:2022年08月17日 09:29:58   作者:Java中文社群  
這篇文章主要為大家詳細(xì)介紹了SpringBoot讀取配置文件的五種方法,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)SpringBoot有一定幫助,需要的可以參考一下

Spring Boot 中讀取配置文件有以下 5 種方法:

  • 使用 @Value 讀取配置文件。
  • 使用 @ConfigurationProperties 讀取配置文件。
  • 使用 Environment 讀取配置文件。
  • 使用 @PropertySource 讀取配置文件。
  • 使用原生方式讀取配置文件。

它們的具體使用方法如下,為了方便測試,我們在 Spring Boot 配置文件 application.properties 添加以下內(nèi)容:

profile.name=Spring Boot Profile
profile.desc=Spring Boot Profile Desc.

1.使用 @Value 讀取配置文件

使用 @Value 可以讀取單個配置項,如下代碼所示:

@SpringBootApplication
public class DemoApplication implements InitializingBean {
    @Value("${profile.name}")
    private String name;

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("My Profile Name:" + name);
    }
}

以上程序的執(zhí)行結(jié)果如下圖所示:

2.使用 @ConfigurationProperties 讀取配置文件

@ConfigurationProperties 和 @Value 的使用略微不同,@Value 是讀取單個配置項的,而 @ConfigurationProperties 是讀取一組配置項的,我們可以使用 @ConfigurationProperties 加實體類讀取一組配置項,如下代碼所示:

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

@Component
@ConfigurationProperties(prefix = "profile")
@Data
public class Profile {
    private String name;
    private String desc;
}

其中 prefix 表示讀取一組配置項的根 name,相當(dāng)于 Java 中的類名,最后再把此配置類,注入到某一個類中就可以使用了,如下代碼所示:

@SpringBootApplication
public class DemoApplication implements InitializingBean {
    @Autowired
    private Profile profile;

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("Profile Object:" + profile);
    }
}

以上程序的執(zhí)行結(jié)果如下圖所示:

3.使用 Environment 讀取配置文件

Environment 是 Spring Core 中的一個用于讀取配置文件的類,將此類使用 @Autowired 注入到類中就可以使用它的 getProperty 方法來獲取某個配置項的值了,如下代碼所示:

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.env.Environment;

@SpringBootApplication
public class DemoApplication implements InitializingBean {

    @Autowired
    private Environment environment;

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("Profile Name:" + environment.getProperty("profile.name"));
    }
}

以上程序的執(zhí)行結(jié)果如下圖所示:

4.使用 @PropertySource 讀取配置文件

使用 @PropertySource 注解可以用來指定讀取某個配置文件,比如指定讀取 application.properties 配置文件的配置內(nèi)容,具體實現(xiàn)代碼如下:

@SpringBootApplication
@PropertySource("classpath:application.properties")
public class DemoApplication implements InitializingBean {
    @Value("${profile.name}")
    private String name;

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("Name:" + name);
    }
}

以上程序的執(zhí)行結(jié)果如下圖所示:

中文亂碼

如果配置文件中出現(xiàn)中文亂碼的情況,可通過指定編碼格式的方式來解決中文亂碼的問題,具體實現(xiàn)如下:

@PropertySource(value = "dev.properties", encoding = "utf-8")

注意事項

@PropertySource 注解默認(rèn)是只支持 properties 格式配置文件的讀取的。

5.使用原生方式讀取配置文件

我們還可以使用最原始的方式 Properties 對象來讀取配置文件,如下代碼所示:

import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Properties;

@SpringBootApplication
public class DemoApplication implements InitializingBean {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        Properties props = new Properties();
        try {
            InputStreamReader inputStreamReader = new InputStreamReader(
                    this.getClass().getClassLoader().getResourceAsStream("application.properties"),
                    StandardCharsets.UTF_8);
            props.load(inputStreamReader);
        } catch (IOException e1) {
            System.out.println(e1);
        }
        System.out.println("Properties Name:" + props.getProperty("profile.name"));
    }
}

以上程序的執(zhí)行結(jié)果如下圖所示:

總結(jié)

在 Spring Boot 中讀取配置文件有以下 5 種方法:

  • 使用 @Value 讀取配置文件。
  • 使用 @ConfigurationProperties 讀取配置文件。
  • 使用 @PropertySource 讀取配置文件。
  • 使用 Environment 讀取配置文件。
  • 使用原生方式讀取配置文件。

其中最常用的是前 3 種,如果讀取某一個配置項可使用 @Value,如果讀取一組配置項可使用 @ConfigurationProperties,如果要指定讀取某一個具體的配置文件可使用 @PropertySource 來指定。

到此這篇關(guān)于SpringBoot讀取配置文件的五種方法總結(jié)的文章就介紹到這了,更多相關(guān)SpringBoot讀取配置文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 解讀JSONArray刪除元素的兩種方式

    解讀JSONArray刪除元素的兩種方式

    這篇文章主要介紹了解讀JSONArray刪除元素的兩種方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • 關(guān)于@Bean的使用方式

    關(guān)于@Bean的使用方式

    這篇文章主要介紹了關(guān)于@Bean的使用方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • Java?FTP協(xié)議實現(xiàn)文件下載功能

    Java?FTP協(xié)議實現(xiàn)文件下載功能

    FTP(File?Transfer?Protocol)就是文件傳輸協(xié)議。通過FTP客戶端從遠(yuǎn)程FTP服務(wù)器上拷貝文件到本地計算機稱為下載,將本地計算機上的文件復(fù)制到遠(yuǎn)程FTP服務(wù)器上稱為上傳,上傳和下載是FTP最常用的兩個功能
    2022-11-11
  • 圖文詳解JAVA實現(xiàn)快速排序

    圖文詳解JAVA實現(xiàn)快速排序

    這篇文章主要給大家介紹了關(guān)于JAVA實現(xiàn)快速排序的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-05-05
  • Springboot自定義banner及驗證過程

    Springboot自定義banner及驗證過程

    這篇文章主要介紹了Springboot自定義banner及驗證過程,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-04-04
  • java實現(xiàn)的MD5摘要算法完整實例

    java實現(xiàn)的MD5摘要算法完整實例

    這篇文章主要介紹了java實現(xiàn)的MD5摘要算法,結(jié)合完整實例形式分析了java實現(xiàn)md5單項加密的具體步驟與相關(guān)操作技巧,需要的朋友可以參考下
    2017-01-01
  • Java如何批量執(zhí)行datax腳本

    Java如何批量執(zhí)行datax腳本

    這篇文章主要介紹了Java如何批量執(zhí)行datax腳本問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • Java面向?qū)ο笾b類的用途與實際使用

    Java面向?qū)ο笾b類的用途與實際使用

    所謂包裝類,就是能夠直接將簡單類型的變量表示為一個類,在執(zhí)行變量類型的相互轉(zhuǎn)換時,我們會大量使用這些包裝類,本文我們來深入探索一下Java包裝類的相關(guān)內(nèi)容,需要的朋友可以參考下
    2022-03-03
  • Java中PreparedStatement的用法解析

    Java中PreparedStatement的用法解析

    這篇文章主要介紹了Java中PreparedStatement的用法解析,在JDBC應(yīng)用中,PreparedStatement是一種比Statement更好的選擇,PreparedStatement可以通過使用參數(shù)化查詢來避免SQL注入攻擊,并且可以提高查詢性能,需要的朋友可以參考下
    2023-09-09
  • RestTemplate接口調(diào)用神器常見用法匯總

    RestTemplate接口調(diào)用神器常見用法匯總

    這篇文章主要介紹了RestTemplate接口調(diào)用神器常見用法匯總,通過案例代碼詳細(xì)介紹RestTemplate接口調(diào)用神器常見用法,需要的朋友可以參考下
    2022-07-07

最新評論