Python 官方文档:入门教程 => 点击学习
目录1.文件类型A.properties配置文件类型B.yaml基本语法数据类型2.配置提示1.文件类型 A.properties配置文件类型 同以前properties用法一样 B
同以前properties用法一样
简介:
YAML 是 "YAML ain't Markup Language"(YAML 不是一种标记语言)的递归缩写。在开发的这种语言时,YAML 的意思其实是:"Yet Another Markup Language"(仍是一种标记语言)。
非常适合用来做以数据为中心的配置文件
注意是:字符串不需要加引号,如果加了''单引号或""双引号内容会被转义【单引号转义】或不转义【双引号不转义】
A.字面量:
单个的,不可再分的值。date boolean string number null
K: V #键值对之间必须有一个空格
B.对象 键值对的集合
map Object hash
#行内写法:
K: {k1:v1,k2:v2,k3:v3}
#或者
K:
K1: v1 #键值对之间必须有一个空格
k2: v2
k3: v3
C.数组:一组按次排列的值。
array list set queue
#行内写法
K: [v1,v2,v3]
#或者
K:
- v1 # `-`与`value`值一定要有一个空格
- v2
- v3
示例:
POJO
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Component
public class Pet {
private String name;
private Double weight;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
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<Double> salary;
private Map<String,List<Pet>> allPets;
}
yaml配置文件
person:
#字面量
username: 海康
boss: true
birth: 2000/11/04
age: 21
#对象 键值对
pet:
name: 阿狗
weight: 20.28
#数组
# interests: [听歌,打代码,跑步] #行内写法
interests:
- 听歌
- 打代码
- 跑步
#List 集合【和数组写法一样】
# animal: [阿狸,阿猫,阿狗] #行内写法
animal:
- 阿狸
- 阿狗
- 阿猫
#set集合【和数组写法一样】
# salary: [8888.8,9999.9,28168.88] #行内写法
salary:
- 88988.99
- 978988.9
- 9999168.98
#Map<String,Object>集合
score:
java: 88.8
c++: 88.99#Map<String,List<Pet>> 集合
allPets:
haikang:
- name: 阿狸
weight: 20.9
- name: 阿狗
weight: 30.2
iaia: [{name: 阿联,weight: 20},{name: 阿哈,weight: 21}]
controller控制器
@RestController// 表示该类是一个控制器并且只响应浏览器不进行页面跳转
public class HelloController {
@Autowired
Person person;
@RequestMapping("/person")
public Person person(){
System.out.println(person);
return person;
}
}
由于在核心配置文件中,配置我们自定义配置信息【自定义的类和配置文件绑定】,idea没有提示
例如:上述示例一样没有提示
配置提示步骤:
步骤1:引入依赖
在pom.xml加入
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
步骤2:加入下面插件,排除在打包时,将configuration-processor的引入打包jar
在pom.xml加入
<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>
步骤3:重新运行RUN
例如:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-WEB</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<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 配置文件内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: SpringBoot超详细讲解yaml配置文件
本文链接: https://www.lsjlt.com/news/152244.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0