當前位置:首頁 » 存儲配置 » 不知如何運行配置文件

不知如何運行配置文件

發布時間: 2023-06-07 16:33:22

Ⅰ SpringBoot 如何優雅讀取配置文件10分鍾教你搞定

很多時候我們需要將一些常用的配置信息比如阿里雲 oss 配置、發送簡訊的相關信息配置等等放到配置文件中。

下面我們來看一下 Spring 為我們提供了哪些方式幫助我們從配置文件中讀取這些配置信息。

application.yml 內容如下:

wuhan2020: 2020年初武漢爆發了新型冠狀病毒,疫情嚴重,但是,我相信一切都會過去!武漢加油!中國加油!my-profile:name: Guide哥email: [email protected]:location: 湖北武漢加油中國加油books:    -name: 天才基本法description: 二十二歲的林朝夕在父親確診阿爾茨海默病這天,得知自己暗戀多年的校園男神裴之即將出國深造的消息——對方考取的學校,恰是父親當年為她放棄的那所。    -name: 時間的秩序description: 為什麼我們記得過去,而非未來?時間「流逝」意味著什麼?是我們存在於時間之內,還是時間存在於我們之中?卡洛·羅韋利用詩意的文字,邀請我們思考這一亘古難題——時間的本質。    -name: 了不起的我description: 如何養成一個新習慣?如何讓心智變得更成熟?如何擁有高質量的關系? 如何走出人生的艱難時刻?

1.通過 @value 讀取比較簡單的配置信息

使用 @Value("${property}") 讀取比較簡單的配置信息:

@Value("${wuhan2020}")String wuhan2020;

需要注意的是 @value這種方式是不被推薦的,Spring 比較建議的是下面幾種讀取配置信息的方式。

2.通過@ConfigurationProperties讀取並與 bean 綁定

LibraryProperties 類上加了 @Component 註解,我們可以像使用普通 bean 一樣將其注入到類中使用。

importlombok.Getter;importlombok.Setter;importlombok.ToString;importorg.springframework.boot.context.properties.ConfigurationProperties;importorg.springframework.context.annotation.Configuration;importorg.springframework.stereotype.Component;importjava.util.List;@Component@ConfigurationProperties(prefix ="library")@Setter@Getter@{privateString location;privateList books;@Setter@Getter@ToStringstaticclassBook{        String name;        String description;    }}

這個時候你就可以像使用普通 bean 一樣,將其注入到類中使用:

packagecn.javaguide.readconfigproperties;importorg.springframework.beans.factory.InitializingBean;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;/** *@authorshuang.kou */@ntsInitializingBean{privatefinalLibraryProperties library;(LibraryProperties library){this.library = library;    }publicstaticvoidmain(String[] args){        SpringApplication.run(.class,args);    }@(){        System.out.println(library.getLocation());        System.out.println(library.getBooks());    }}

控制台輸出:

湖北武漢加油中國加油[LibraryProperties.Book(name=天才基本法, description........]

3.通過@ConfigurationProperties讀取並校驗

我們先將application.yml修改為如下內容,明顯看出這不是一個正確的 email 格式:

my-profile:name: Guide哥email: koushuangbwcx@

ProfileProperties 類沒有加 @Component 註解。我們在我們要使用ProfileProperties 的地方使用@

EnableConfigurationProperties注冊我們的配置 bean:

importlombok.Getter;importlombok.Setter;importlombok.ToString;importorg.springframework.boot.context.properties.ConfigurationProperties;importorg.springframework.stereotype.Component;importorg.springframework.validation.annotation.Validated;importjavax.validation.constraints.Email;importjavax.validation.constraints.NotEmpty;/***@authorshuang.kou*/@Getter@Setter@ToString@ConfigurationProperties("my-profile")@{@NotEmptyprivateString name;@Email@NotEmptyprivateString email;//配置文件中沒有讀取到的話就用默認值privateBooleanhandsome =Boolean.TRUE;}

具體使用:

packagecn.javaguide.readconfigproperties;importorg.springframework.beans.factory.InitializingBean;importorg.springframework.beans.factory.annotation.Value;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.boot.context.properties.EnableConfigurationProperties;/** *@authorshuang.kou */@SpringBootApplication@EnableConfigurationProperties(ProfileProperties.class){privatefinalProfileProperties profileProperties;(ProfileProperties profileProperties){this.profileProperties = profileProperties;    }publicstaticvoidmain(String[] args){        SpringApplication.run(.class,args);    }@(){        System.out.println(profileProperties.toString());    }}

因為我們的郵箱格式不正確,所以程序運行的時候就報錯,根本運行不起來,保證了數據類型的安全性:

Binding to target org.springframework.boot.context.properties.bind.BindException:Failedtobindpropertiesunder'my-profile'to cn.javaguide.readconfigproperties.ProfileProperties failed:Property:my-profile.emailValue:koushuangbwcx@Origin:classpathresource[application.yml]:5:10Reason:mustbeawell-formedemailaddress

我們把郵箱測試改為正確的之後再運行,控制台就能成功列印出讀取到的信息:

ProfileProperties(name=Guide哥, [email protected], handsome=true)

4.@PropertySource讀取指定 properties 文件

importlombok.Getter;importlombok.Setter;importorg.springframework.beans.factory.annotation.Value;importorg.springframework.context.annotation.PropertySource;importorg.springframework.stereotype.Component;@Component@PropertySource("classpath:website.properties")@Getter@SetterclassWebSite{@Value("${url}")privateString url;}

使用:

@Autowiredprivate WebSite webSite;System.out.println(webSite.getUrl());//https://javaguide.cn/

5.題外話:Spring 載入配置文件的優先順序

Spring 讀取配置文件也是有優先順序的,直接上圖:

原文鏈接:https://www.toutiao.com/a6791445278911103500/?log_from=7f5fb8f9b4b47_1640606437752

Ⅱ 易語言如何寫載入指定配置文件與運行命令

配置

Ⅲ 找不到系統配置文件該怎麼辦

這個問題很常見。
我也有過相似的情況,但是這樣解決的:

在Windows XP系統下,運行「msconfig」後,系統提示找不到該文件。但是該文件明明存在於系統分區中,而且可以雙擊打開。

這是因為msconfig.exe存在於 %systemroot%\PCHealth\HelpCtr\Binaries 目錄中,而不是
%systemroot% 或 %systemroot%\system32
,無法省略其路徑而直接運行文件名打開。我們平時之所以能夠直接運行msconfig打開該程序,是因為在注冊表中的
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App
Paths
鍵下有msconfig.exe
的相關路徑,起到導向功能。如果沒有了msconfig.exe相關路徑或者路徑錯誤,就會導致無法直接運行文件名打開。解決方法:運行「regedit」,找到
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App
Paths
在右邊窗口中,右擊空白處,依次選擇【新建】→【項】,將其名改為「MSCONFIG.EXE」。然後選中「MSCONFIG.EXE」,再在右邊窗口中,雙擊字元串值【默認】,把數值改為「C:\Windows\PCHealth\HelpCtr\Binaries\MSConfig.exe」(根據系統所在分區自行更改)。修改後,我們就可以像以前一樣直接運行msconfig打開該程序啦

希望你也成功!

Ⅳ 游戲的配置設置文件在哪

游戲的配置設置文件在steam本地文件夾中。以CSGO游戲為例,查看方法如下:

准備材料:CSGO游戲、電腦

1、在steam本地文件夾中,找到userdata文件夾,雙擊進入,


熱點內容
如何配置2檸檬酸 發布:2025-02-06 00:57:26 瀏覽:434
積木源碼 發布:2025-02-06 00:55:26 瀏覽:545
變分的運演算法則 發布:2025-02-06 00:55:21 瀏覽:775
x2哪個配置性價比高 發布:2025-02-06 00:40:12 瀏覽:109
豬哥亮訪問張菲 發布:2025-02-06 00:37:52 瀏覽:570
期貨賬戶怎麼改密碼 發布:2025-02-06 00:32:35 瀏覽:279
qq自動上傳群文件 發布:2025-02-06 00:26:25 瀏覽:111
安卓照片放在什麼地方 發布:2025-02-06 00:26:24 瀏覽:988
linux系統鏡像iso 發布:2025-02-06 00:15:39 瀏覽:188
存儲上料模塊的意義 發布:2025-02-06 00:14:14 瀏覽:125