本篇内容介绍了“SpringBoot中怎么使用yaml配置文件”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
1.基本语法
2.数据类型
1.字面量:单个的、不可再分的值。date、boolean、string、number、null
k: v
2.对象:键值对的集合。map、hash、set、object
#行内写法:
k: {k1:v1,k2:v2,k3:v3}
#或
k:
k1: v1
k2: v2
k3: v3
3.数组:一组按次序排列的值。array、list、queue
#行内写法:
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}
#对比字符串变量不使用引号、使用单引号、双引号的区别
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);
}
}测试结果

可以看到
4.开启补全提示
就是下图的效果

自定义的类和配置文件绑定一般没有提示。若要提示,添加如下依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!-- 下面插件作用是工程打包时,不将spring-boot-configuration-processor打进包内,让其只在编码的时候有用 -->
<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配置文件”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注天达云网站,小编将为大家输出更多高质量的实用文章!