如何在yml配置文件中使用中文注解
yml配置文件中使用中文注解
問題
我們在yml中寫注解一般是這樣的 #xxxx
當我們啟動時我們會遇到這樣的問題
Failed to load property source from
'file:/D:/idea/bonc/server/monitor-streaming/target/classes/application.yml'
(classpath:/application.yml)
根本原因
因為我們在的yml的文件格式時GBK的 我們的中文注釋在target文件中是亂碼的
解決
修改文件格式 文件格式都改為UTF-8

yml配置文件簡單語法及小坑
yml文件使用方法
1-語法
K : (空格)V
表示一對鍵值對,以空格縮進來控制層級關系,只要左對齊的一列數據,都是一個層級的。屬性和值是大小寫敏感
2-寫法
普通值
- 字符串默認不加單引號或者雙引號;
- 雙引號,不會轉義字符串里面的特殊字符,特殊字符會作為本身想表示的意思
- 單引號:會轉義特殊字符,特殊字符只會是一個普通的字符串數據
- 特殊情況:如 00013 ,類似的數值要加上單引號,否則讀取時會出錯。
對象,map(屬性和值) (鍵值對)
user:
userName: "小明"
boss: true
birth: 2022/07/13
age: 20
##============行內寫法
user:{userName: "小明",boss: true,birth: 2022/07/13,age: 20}數組(List,Set)
用 - 值 表示數組中的一個元素
pets: ?? ?- cat ?? ?- dog ?? ?- pig #=====行內 pets: [cat,dog,pig]
舉個栗子:
pet實體
package cn.maggie.bussiness.entity;
import lombok.Data;
import lombok.ToString;
@Data
@ToString
public class Pet {
? ? /**
? ? ?* 名字
? ? ?*/
? ? private String name;
? ? /**
? ? ?* 體重w
? ? ?*/
? ? private String ?weight;
}user實體–讀取配置組件
package cn.maggie.bussiness.entity;
import lombok.Data;
import lombok.ToString;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
@Component
@Data
@ToString
@ConfigurationProperties(prefix = "user")
public class User {
? ? private String ?userName;
? ? private Boolean ?boss;
? ? private Date ?birth;
? ? private Integer age;
? ? private Pet ?pet;
? ? private String [] interests;
? ? private List<String> animal;
? ? private Map<String,Object> score;
? ? private Set<BigDecimal> salary;
? ? private Map<String ,List<Pet>> allPets;
}yml文件
user:
? userName: "小明"
? boss: true
? birth: 2022/07/13
? age: 20
?# 數組 ?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
?#對象類型 --鍵值對
? pet:
? ? name: 小哈
? ? weight: 9
# ?map復雜集合 --2種寫法
? allPets:
? ? sick:
? ? ? - {name: 紅紅,weight: 99}
? ? ? - name: niuniu
? ? ? ? weight: 88
? ? health: [{name: 胖胖,weight: 79},{name: 小白,weight: 90}]測試 -啟動類打印輸出
package cn.maggie.bussiness;
import cn.maggie.bussiness.entity.User;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class BussinessApplication {
? ? public static void main(String[] args) {
? ? ? ? ConfigurableApplicationContext run = SpringApplication.run(BussinessApplication.class, args);
? ? ? ? User user = run.getBean(User.class);
? ? ? ? System.out.println("============================>"+user.toString());
? ? }
}配置文件注入值數據校驗
@Validated//JSR303數據校驗,此注解加于配置類上
屬性可用到的注解

多環(huán)境profile
server:
port: 8080
spring:
profiles:
active: dev #激活,默認就是8080
---
server:
port: 8083
spring:
profiles: test
---
server:
port: 8081
spring:
profiles: prod #指定屬于哪個配置引入此依賴,自定義bean與配置文件綁定時,配置文件會有提醒
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!--打包時排除此包-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<!-- 打包時,排除此包-->
<exclude>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Spring Data JPA結合Mybatis進行分頁查詢的實現(xiàn)
本文主要介紹了Spring Data JPA結合Mybatis進行分頁查詢的實現(xiàn)2024-03-03
SpringBoot+SpringSecurity實現(xiàn)認證的流程詳解
這篇文章主要介紹了SpringBoot+SpringSecurity實現(xiàn)認證的流程,文中通過代碼示例和圖文結合的方式講解的非常詳細,對大家的學習或工作有一定的幫助,需要的朋友可以參考下2024-05-05
Java設計模式之單態(tài)模式(Singleton模式)介紹
這篇文章主要介紹了Java設計模式之單態(tài)模式(Singleton模式)介紹,本文講解了如何使用單例模式、使用單例模式注意事項等內容,需要的朋友可以參考下2015-03-03

