SpringBoot詳細講解yaml配置文件的用法
1.基本語法
- key: value;kv之間有空格
- 大小寫敏感
- 使用縮進表示層級關系
- 縮進不允許使用tab,只允許空格
- 縮進的空格數(shù)不重要,只要相同層級的元素左對齊即可
- '#'表示注釋
- 字符串無需加引號,如果要加,單引號’'、雙引號""表示字符串內(nèi)容會被 轉(zhuǎn)義、不轉(zhuǎn)義
2.數(shù)據(jù)類型
1.字面量:單個的、不可再分的值。date、boolean、string、number、null
k: v
2.對象:鍵值對的集合。map、hash、set、object
#行內(nèi)寫法:
k: {k1:v1,k2:v2,k3:v3}
#或
k:
k1: v1
k2: v2
k3: v3
3.數(shù)組:一組按次序排列的值。array、list、queue
#行內(nèi)寫法:
k: [v1,v2,v3]
#或者
k:
- v1
- v2
- v3
3.代碼測試
User
package com.limi.springboottest2.entity; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @NoArgsConstructor @AllArgsConstructor public class User { private String userName; private Integer age; private String gender; }
Entity1
package com.limi.springboottest2.entity; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; import java.util.List; import java.util.Map; @ConfigurationProperties(prefix = "entity1") @Data @AllArgsConstructor @NoArgsConstructor @Component public class Entity1 { private Double number; private List<String> array; private User user; private Map<String, Integer> map; private String str0; private String str1; private String str2; }
application.yml
entity1:
number: 11
array: ["apple", "peach", "orange"]
user: {userName: "lily", }
map: {"Math": 100,"English": 98,"Art": 8}
#對比字符串變量不使用引號、使用單引號、雙引號的區(qū)別
str0: \n 666
str1: '\n 666'
str2: "\n 666"
HelloController
package com.limi.springboottest2.controller; import com.limi.springboottest2.entity.Entity1; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class HelloController { @Autowired private Entity1 entity1; @GetMapping("/test1") @ResponseBody void test1() { System.out.println(entity1); } }
測試結(jié)果
可以看到
- 不使用引號和使用單引號的字符串: n 666 中的\n是直接輸出\n
- 使用雙引號的字符串: \n 666 中的\n是輸出為換行符
4.開啟補全提示
就是下圖的效果
自定義的類和配置文件綁定一般沒有提示。若要提示,添加如下依賴:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <!-- 下面插件作用是工程打包時,不將spring-boot-configuration-processor打進包內(nèi),讓其只在編碼的時候有用 --> <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>
到此這篇關于SpringBoot詳細講解yaml配置文件的用法的文章就介紹到這了,更多相關SpringBoot yaml配置文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Spring整合Quartz分布式調(diào)度的示例代碼
本篇文章主要介紹了Spring整合Quartz分布式調(diào)度的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-04-04springcloud如何用Redlock實現(xiàn)分布式鎖
本文主要介紹了springcloud如何用Redlock實現(xiàn)分布式鎖,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-11-11mybatis中association和collection的使用與區(qū)別
在 MyBatis 中,<association>?和?<collection>?是用于配置結(jié)果映射中關聯(lián)關系的兩個元素,本文主要介紹了mybatis中<association>和<collection>的使用與區(qū)別,具有一定的參考價值,感興趣的可以了解一下2024-01-01