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

SpringBoot中讀取配置文件的6種方式總結(jié)

 更新時間:2026年03月19日 09:07:46   作者:劉大貓.  
這篇文章主要為大家詳細介紹了SpringBoot中讀取配置文件的六種方法,包括通過Environment,@Value,@ConfigurationPropertie等,感興趣的小伙伴可以了解下

概述:本文從讀取默認配置文件即自定義配置文件入手,去整理了解幾種加載方案的區(qū)別

SpringBoot讀取配置文件的幾種方式

  • 測試方式1:通過Environment讀取配置信息
  • 測試方式2:通過@Value注解讀取配置信息(推薦使用)
  • 測試方式3:通過@ConfigurationProperties注解讀取配置信息
  • 測試方式4:通過@PropertySource+@Value注解讀取配置信息
  • 測試方式5:通過@PropertySource+@ConfigurationProperties注解讀取配置信息
  • 測試方式6:通過Properties讀取配置信息

結(jié)論:無論什么場景都推薦使用@Value注解準備錯;其他了解即可。

準備工作

配置文件目錄

application.properties

server.port=8080
spring.profiles.active=dev

application-dev.properties

spring.redis.host=localhost
logging.level.root = info

application-prod.properties

spring.redis.port=6379
logging.level.root = warn

my.properties

demo.name=cat

案例說明

測試方式1:通過Environment讀取配置信息

注意點說明:

注意點1:Environment是用來讀取應用程序運行時的環(huán)境變量的類,可以通過key-value的方式讀取application.properties和系統(tǒng)環(huán)境變量,命令行輸入?yún)?shù),系統(tǒng)屬性等.

Controller

import com.example.demo.config.ReadProperties;
import com.example.demo.config.ReadProperties2;
import com.example.demo.config.ReadProperties3;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * 測試讀取配置文件的幾種方式:
 * @Author 211145187
 * @Date 2022/7/20 14:02
 **/
@RestController
public class ReadApplicationProperties {
    @Autowired
    private Environment environment;
    
	//測試方式1:通過Environment讀取配置信息
    @GetMapping("/readApplicationProperties1")
    public Map<String,Object> readApplicationProperties1(){
        Map<String,Object> map = new HashMap<>();
        map.put("port",environment.getProperty("server.port"));
        System.out.println("通過Environment讀取配置信息:" + environment.getProperty("server.port"));
        return  map;
    }
}

結(jié)果打?。?/p>

測試方式2:通過@Value注解讀取配置信息(推薦使用)

Controller

import com.example.demo.config.ReadProperties;
import com.example.demo.config.ReadProperties2;
import com.example.demo.config.ReadProperties3;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * 測試讀取配置文件的幾種方式:
 * @Author 211145187
 * @Date 2022/7/20 14:02
 **/
@RestController
public class ReadApplicationProperties {
    @Value("${server.port}")
    private Integer serverPort;

	//測試方式2:通過@Value注解讀取配置信息
    @GetMapping("/readApplicationProperties2")
    public void readApplicationProperties2(){
        System.out.println("通過@Value注解讀取配置信息:" + serverPort);
    }
}

結(jié)果打印

測試方式3:通過@ConfigurationProperties注解讀取配置信息

注意點說明:

注意點1:@ConfigurationProperties注解用于指定前綴,下方的屬性名稱必須和要獲取的配置信息名稱一致,比如必須叫port,否則獲取值為null

使用@ConfigurationProperties首先建立配置文件與對象的映射關(guān)系,然后在控制器方法中使用@Autowired注解將對象注入.

注意點2:配置生效的兩種方式:

  • 方式1:配置@Component
  • 方式2:啟動類添加@EnableConfigurationProperties(ReadProperties.class)

總結(jié):注解@Component和注解@EnableConfigurationProperties(ReadProperties.class)是等價的,寫一個就行。

注意點3:@ConfigurationProperties也可以和@Value和@Bean一起使用,只不過我沒寫案例。

注意點4:@ConfigurationProperties只能加載以application為前綴開頭的配置文件,比如application-dev.properties,加載自定義名稱配置文件內(nèi)容無效。

注意點5:

問題:從上面的示例中,我們可以看到在屬性綁定中@EnableConfigurationProperties和@Component的效果一樣,那么為啥springboot還要使用這個注解呢?

答案:當我們引用第三方jar包時,@Component標注的類是無法注入到spring容器中的,這時我們可以用@EnableConfigurationProperties來代替@Component

ReadProperties

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

/**
 * 1)注解@ConfigurationProperties中的prefix用于設置前綴
 * 2)下方的屬性名稱必須和要獲取的配置信息名稱一致,比如必須叫port,否則獲取值為null
 */
@ConfigurationProperties(prefix = "server")//這個注解是用找到類
@Component  //生效的兩種方式:方式1:配置@Component,方式2:啟動類添加@EnableConfigurationProperties(ReadProperties.class)
@Data
public class ReadProperties {
    private Integer port;
}

Controller

import com.example.demo.config.ReadProperties;
import com.example.demo.config.ReadProperties2;
import com.example.demo.config.ReadProperties3;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * 測試讀取配置文件的幾種方式:
 * @Author 211145187
 * @Date 2022/7/20 14:02
 **/
@RestController
public class ReadApplicationProperties {
    @Autowired
    private ReadProperties readProperties;

	//測試方式3:通過@ConfigurationProperties注解讀取配置信息
    @GetMapping("/readApplicationProperties3")
    public void readApplicationProperties3(){
        System.out.println("通過@ConfigurationProperties注解讀取配置信息:" + readProperties.getPort());
    }
}

結(jié)果打印

測試方式4:通過@PropertySource+@Value注解讀取配置信息

注意點說明:

注意點1:@PropertySource注解加載指定的屬性文件(*.properties)到 Spring 的 Environment 中??梢耘浜?@Value 和@ConfigurationProperties 和@Bean使用。

注意點2:@PropertySource注解可以配合 @Value 和@ConfigurationProperties 和@Bean一起使用,只不過我沒寫案例。

注意點3:使用@PropertySource注解推薦只加載自定義名稱的配置文件,不要加載以application為前綴開頭的配置文件,比如application-dev.properties,因為重名的key值會被覆蓋,這點會在注意點4中著重說明。

注意點4:(最容易出錯)

講解一個大坑

補充說明:application-dev.properties中設置logging.level.root = info,而application-prod.properties中設置logging.level.root = warn

案例說明:application.properties配置文件設置內(nèi)置spring.profiles.active=dev,用于關(guān)聯(lián)application-dev.properties配置文件,正常代碼運行會把application.properties和application-dev.properties配置文件都加載到內(nèi)存中,但是現(xiàn)在我想創(chuàng)建一個config或者bean,通過@PropertySource注解去注入并打印application-prod.properties中的這個內(nèi)容:logging.level.root = warn,正確打印logging.level.root的結(jié)果應該是warn,因為它是最后加載的,但實際打印結(jié)果logging.level.root的值是info,

問題:為什么?為什么打印info,而我想打印的是prod中的值warn

答案:如圖1,你看紅色框中你感覺prod在info下載加載,你會覺得prod相同的key會覆蓋dev中的值,實際答案真不是這樣,詳情請看如圖2這個人的回答。正常來說生產(chǎn)項目中application-dev.properties和application-prod.properties只會允許使用一個,才不會混用。

實際真實項目解決方案是:

  • 第1種方案:啟動類中配置環(huán)境變量也會通過指定dev還是prod生效,所以該案例只是自己的一個想法,實際沒太大作用且實際項目也不會允許同時加載dev和prod配置文件。
  • 第2種方案:為了避免重名key被覆蓋,我們會讓application.properties和application-dev.properties不會存放相同的key內(nèi)容,即application.properties有一個key,那么application-dev.properties和application-prod.properties中絕不會有這個相同的key內(nèi)容。
  • 第3種方案:如果非要使用@PropertySource注解注入一個配置文件,那么一定指向自定義名稱配置文件,千萬不要指向以application-為前綴的配置文件。

如圖1

如圖2

注意點5:配置文件加載的優(yōu)先級 > @PropertySource注解注入

ReadProperties2

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

/**
 * @Author 211145187
 * @Date 2022/7/20 15:47
 **/
@PropertySource(value = {"application.properties"})
@Component
@Data
public class ReadProperties2 {
    @Value("${server.port}")
    private Integer port;
}

ReadProperties4

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

/**
 * @Author 211145187
 * @Date 2022/7/20 15:51
 **/
@ConfigurationProperties(prefix = "spring.redis")//這個注解是用找到類    注意:@ConfigurationProperties無法加載自定義配置問價內(nèi)容,必須和@PropertySource配合使用才能獲取
@Component  //生效的兩種方式:方式1:配置@Component,方式2:啟動類添加@EnableConfigurationProperties(ReadProperties.class)
@PropertySource(value = {"classpath:application-prod.properties"})
@Data
public class ReadProperties4 {
    private String port;
}

Controller

import com.example.demo.config.ReadProperties;
import com.example.demo.config.ReadProperties2;
import com.example.demo.config.ReadProperties3;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * 測試讀取配置文件的幾種方式:
 * @Author 211145187
 * @Date 2022/7/20 14:02
 **/
@RestController
public class ReadApplicationProperties {
    @Autowired
    private ReadProperties2 readProperties2;

	//測試方式4:通過@PropertySource+@Value注解讀取配置信息
    @GetMapping("/readApplicationProperties4")
    public void readApplicationProperties4(){
        System.out.println("通過@PropertySource注解讀取配置信息:" + readProperties2.getPort());
    }
}

結(jié)果打印

測試方式5:通過@PropertySource+@ConfigurationProperties注解讀取配置信息

ReadProperties3

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

/**
 * @Author 211145187
 * @Date 2022/7/20 15:51
 **/
@ConfigurationProperties(prefix = "demo")//這個注解是用找到類    注意:@ConfigurationProperties無法加載自定義配置問價內(nèi)容,必須和@PropertySource配合使用才能獲取
@Component  //生效的兩種方式:方式1:配置@Component,方式2:啟動類添加@EnableConfigurationProperties(ReadProperties.class)
@PropertySource(value = {"classpath:my.properties"})
@Data
public class ReadProperties3 {
    private String name;
}

Controller

import com.example.demo.config.ReadProperties;
import com.example.demo.config.ReadProperties2;
import com.example.demo.config.ReadProperties3;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * 測試讀取配置文件的幾種方式:
 * @Author 211145187
 * @Date 2022/7/20 14:02
 **/
@RestController
public class ReadApplicationProperties {
    @Autowired
    private ReadProperties3 readProperties3;

	//測試方式5:通過@PropertySource+@ConfigurationProperties注解讀取配置信息
    @GetMapping("/readApplicationProperties5")
    public void readApplicationProperties5(){
        System.out.println("通過@PropertySource+@ConfigurationProperties注解讀取配置信息:" + readProperties3);
    }
}

結(jié)果打印

測試方式6:通過Properties讀取配置信息

Controller

import com.example.demo.config.ReadProperties;
import com.example.demo.config.ReadProperties2;
import com.example.demo.config.ReadProperties3;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * 測試讀取配置文件的幾種方式:
 * @Author 211145187
 * @Date 2022/7/20 14:02
 **/
@RestController
public class ReadApplicationProperties {
    //測試方式6:通過Properties讀取配置信息
    @GetMapping("/readApplicationProperties6")
    public void readApplicationProperties6() throws IOException {
        Resource resource = new ClassPathResource("application-prod.properties");
        Properties properties = PropertiesLoaderUtils.loadProperties(resource);
        String root = properties.getProperty("logging.level.root");
        System.out.println("通過xProperties讀取配置信息:" + root);
    }
}

結(jié)果打印

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

相關(guān)文章

  • 關(guān)于文件合并與修改md5值的問題

    關(guān)于文件合并與修改md5值的問題

    這篇文章主要介紹了關(guān)于文件合并與修改md5值的問題,使用本博客的方法,不僅僅可以修改md5值,還可以達到隱藏文件的目的,需要的朋友可以參考下
    2023-04-04
  • SpringBoot項目War包部署無法注冊到Nacos中的解決

    SpringBoot項目War包部署無法注冊到Nacos中的解決

    這篇文章主要介紹了SpringBoot項目War包部署無法注冊到Nacos中的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • java尋找迷宮路徑的簡單實現(xiàn)示例

    java尋找迷宮路徑的簡單實現(xiàn)示例

    這篇文章主要介紹了java尋找迷宮路徑的簡單實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-10-10
  • Spring中Bean的創(chuàng)建流程詳細解讀

    Spring中Bean的創(chuàng)建流程詳細解讀

    這篇文章主要介紹了Spring中Bean的創(chuàng)建流程詳細解讀,Spring 中創(chuàng)建 Bean ,是通過調(diào)用 GetBean 方法來觸發(fā)的,所以,我們會從這個方法開始,需要的朋友可以參考下
    2023-10-10
  • Mac系統(tǒng)上安裝?JDK?8?最穩(wěn)最全教程(Homebrew?方式)

    Mac系統(tǒng)上安裝?JDK?8?最穩(wěn)最全教程(Homebrew?方式)

    Homebrew是一個開源的包管理器,專為macOS(盡管也可以在Linux上使用)設計,用來簡化在Mac操作系統(tǒng)上安裝軟件的過程,這篇文章主要介紹了Mac系統(tǒng)上使用Homebrew方式安裝JDK8最穩(wěn)最全教程的相關(guān)資料,需要的朋友可以參考下
    2026-04-04
  • IDEA去除xml文件的黃色背景的操作步驟

    IDEA去除xml文件的黃色背景的操作步驟

    這篇文章主要介紹了IDEA去除xml文件的黃色背景的方法,本文通過圖文結(jié)合的方式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考價值,感興趣的朋友可以參考下
    2023-12-12
  • Java的NIO之通道channel詳解

    Java的NIO之通道channel詳解

    這篇文章主要介紹了Java的NIO之通道channel詳解,通道channel由java.nio.channels 包定義的,Channel 表示IO源與目標打開的連接,Channel類類似于傳統(tǒng)的"流",只不過Channel本身不能直接訪問數(shù)據(jù),Channel只能與Buffer進行交互,需要的朋友可以參考下
    2023-10-10
  • 如何在spring事務提交之后進行異步操作

    如何在spring事務提交之后進行異步操作

    這篇文章主要為大家介紹了如何在spring事務提交之后進行異步操作,這些異步操作必須得在該事務成功提交后才執(zhí)行,回滾則不執(zhí)行,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步
    2023-09-09
  • IntelliJ?IDEA中如何使用Checkstyle對Java代碼規(guī)范檢查

    IntelliJ?IDEA中如何使用Checkstyle對Java代碼規(guī)范檢查

    這篇文章主要介紹了IntelliJ IDEA中如何使用Checkstyle對Java代碼規(guī)范檢查的相關(guān)資料,checkstyle是提高代碼質(zhì)量,檢查代碼規(guī)范的很好用的一款工具,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2026-03-03
  • struts2通過action返回json對象

    struts2通過action返回json對象

    struts2通過action返回json對象其實很簡單的,首先我們需要引入jar包,然后在寫一個簡單的action就好了,接下來通過本文給大家介紹struts2通過action返回json對象的方法,感興趣的朋友一起看看吧
    2016-09-09

最新評論

庄河市| 启东市| 当阳市| 曲阳县| 乐昌市| 绩溪县| 常宁市| 宜良县| 南丹县| 略阳县| 斗六市| 鄂托克前旗| 诸暨市| 宝兴县| 远安县| 齐齐哈尔市| 南京市| 封丘县| 和平县| 龙门县| 海丰县| 磐石市| 大足县| 罗定市| 含山县| 合肥市| 鄂尔多斯市| 黔南| 吴旗县| 印江| 乡城县| 恩施市| 伊金霍洛旗| 千阳县| 周口市| 安庆市| 体育| 腾冲县| 封丘县| 海伦市| 库尔勒市|