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

如何在yml配置文件中使用中文注解

 更新時(shí)間:2022年10月27日 14:07:08   作者:酷酷的王大錘  
這篇文章主要介紹了如何在yml配置文件中使用中文注解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

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

問題

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

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

Failed to load property source from 
'file:/D:/idea/bonc/server/monitor-streaming/target/classes/application.yml' 
(classpath:/application.yml)

根本原因

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

解決

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

yml配置文件簡(jiǎn)單語法及小坑

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: 2022/07/13
  age: 20
 ##============行內(nèi)寫法
 user:{userName: "小明",boss: true,birth: 2022/07/13,age: 20}

數(shù)組(List,Set)

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

pets:
?? ?- cat
?? ?- dog
?? ?- pig
#=====行內(nèi)
pets: [cat,dog,pig]

舉個(gè)栗子:

pet實(shí)體

package cn.maggie.bussiness.entity;

import lombok.Data;
import lombok.ToString;

@Data
@ToString
public class Pet {
? ? /**
? ? ?* 名字
? ? ?*/
? ? private String name;
? ? /**
? ? ?* 體重w
? ? ?*/
? ? private String ?weight;
}

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

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
?# 數(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}]

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

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());
? ? }
}

配置文件注入值數(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>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-configuration-processor</artifactId>
      <optional>true</optional>
</dependency>

<!--打包時(shí)排除此包-->
<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
<!--                        打包時(shí),排除此包-->
                        <exclude>
                            <groupId>org.springframework.boot</groupId>
                            <artifactId>spring-boot-configuration-processor</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>

        </plugins>
    </build>

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論