如何在yml配置文件中使用中文注解_第1頁
如何在yml配置文件中使用中文注解_第2頁
如何在yml配置文件中使用中文注解_第3頁
如何在yml配置文件中使用中文注解_第4頁
如何在yml配置文件中使用中文注解_第5頁
已閱讀5頁,還剩3頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

第如何在yml配置文件中使用中文注解目錄yml配置文件中使用中文注解問題根本原因解決yml配置文件簡單語法及小坑yml文件使用方法

yml配置文件中使用中文注解

問題

我們?cè)趛ml中寫注解一般是這樣的#xxxx

當(dāng)我們啟動(dòng)時(shí)我們會(huì)遇到這樣的問題

Failedtoloadpropertysourcefrom

file:/D:/idea/bonc/server/monitor-streaming/target/classes/application.yml

(classpath:/application.yml)

根本原因

因?yàn)槲覀冊(cè)诘膟ml的文件格式時(shí)GBK的我們的中文注釋在target文件中是亂碼的

解決

修改文件格式文件格式都改為UTF-8

yml配置文件簡單語法及小坑

yml文件使用方法

1-語法

K:(空格)V

表示一對(duì)鍵值對(duì),以空格縮進(jìn)來控制層級(jí)關(guān)系,只要左對(duì)齊的一列數(shù)據(jù),都是一個(gè)層級(jí)的。屬性和值是大小寫敏感

2-寫法

普通值

字符串默認(rèn)不加單引號(hào)或者雙引號(hào);雙引號(hào),不會(huì)轉(zhuǎn)義字符串里面的特殊字符,特殊字符會(huì)作為本身想表示的意思單引號(hào):會(huì)轉(zhuǎn)義特殊字符,特殊字符只會(huì)是一個(gè)普通的字符串?dāng)?shù)據(jù)特殊情況:如00013,類似的數(shù)值要加上單引號(hào),否則讀取時(shí)會(huì)出錯(cuò)。

對(duì)象,map(屬性和值)(鍵值對(duì))

user:

userName:"小明"

boss:true

birth:2025/07/13

age:20

##============行內(nèi)寫法

user:{userName:"小明",boss:true,birth:2025/07/13,age:20}

數(shù)組(List,Set)

用-值表示數(shù)組中的一個(gè)元素

pets:

-cat

-dog

-pig

#=====行內(nèi)

pets:[cat,dog,pig]

舉個(gè)栗子:

pet實(shí)體

packagecn.maggie.bussiness.entity;

importlombok.Data;

importlombok.ToString;

@Data

@ToString

publicclassPet{

/**

*名字

*/

privateStringname;

/**

*體重w

*/

privateString

weight;

}

user實(shí)體讀取配置組件

packagecn.maggie.bussiness.entity;

importlombok.Data;

importlombok.ToString;

importperties.ConfigurationProperties;

importorg.springframework.stereotype.Component;

importjava.math.BigDecimal;

importjava.util.Date;

importjava.util.List;

importjava.util.Map;

importjava.util.Set;

@Component

@Data

@ToString

@ConfigurationProperties(prefix="user")

publicclassUser{

privateString

userName;

privateBoolean

boss;

privateDate

birth;

privateIntegerage;

privatePet

pet;

privateString[]interests;

privateListStringanimal;

privateMapString,Objectscore;

privateSetBigDecimalsalary;

privateMapString,ListPetallPets;

}

yml文件

user:

userName:"小明"

boss:true

birth:2025/07/13

age:20

#數(shù)組

2種寫法

#

interests:[打球,旅游]

interests:

-'喝水'

-睡覺

animal:

-阿貓

-阿狗

#

map集合

2種寫法

#

score:[math:90,English:100,chainses:30]

sore:

math:90

english:100

chainese:90

#

set集合

2種寫法

#

salary:[22.0,333.90]

salary:

-22.90

-33.80

#對(duì)象類型--鍵值對(duì)

pet:

name:小哈

weight:9

#

map復(fù)雜集合--2種寫法

allPets:

sick:

-{name:紅紅,weight:99}

-name:niuniu

weight:88

health:[{name:胖胖,weight:79},{name:小白,weight:90}]

測試-啟動(dòng)類打印輸出

packagecn.maggie.bussiness;

importcn.maggie.bussiness.entity.User;

importorg.springframework.boot.SpringApplication;

importorg.springframework.boot.autoconfigure.SpringBootApplication;

importorg.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication

publicclassBussinessApplication{

publicstaticvoidmain(String[]args){

ConfigurableApplicationContextrun=SpringApplication.run(BussinessApplication.class,args);

Useruser=run.getBean(User.class);

System.out.println("============================"+user.toString());

}

}

配置文件注入值數(shù)據(jù)校驗(yàn)

@Validated//JSR303數(shù)據(jù)校驗(yàn),此注解加于配置類上

屬性可用到的注解

多環(huán)境profile

server:

port:8080

spring:

profiles:

active:dev#激活,默認(rèn)就是8080

server:

port:8083

spring:

profiles:test

server:

port:8081

spring:

profiles:prod#指定屬于哪個(gè)配置

引入此依賴,自定義bean與配置文件綁定時(shí),配置文件會(huì)有提醒

dependency

groupIdorg.springframework.boot/groupId

artifactIdspring-boot-configuration-processor/artifactId

optionaltrue/optional

/dependency

!--打包時(shí)排除此包--

build

plugins

plugin

groupIdorg.springframework.boot/groupId

artifactIdspring-boot-maven-plugin/artifactId

configuration

excludes

!--打包時(shí),排除此包--

exclude

groupIdorg.springframewo

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論