● key: value;kv之间有空格
● 大小写敏感
● 使用缩进表示层级关系
● 缩进不允许使用tab,只允许空格
● 缩进的空格数不重要,只要相同层级的元素左对齐即可
● '
k: v
行内写法: k: {k1:v1,k2:v2,k3:v3}
#或
k:
k1: v1
k2: v2
k3: v3
一个 -代表着一个元素
行内写法: k: [v1,v2,v3]
#或者
k:
- v1
- v2
- v3
package com.item.boot.bean;
import lombok.Data;
import lombok.ToString;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
@ConfigurationProperties(prefix = "person")//绑定配置文件
@Component //实现bean的注入
@ToString //生成tosring方法
@Data //添加get set方法
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> salarys;
private Map<String, List<Pet>> allPets;
}
package com.item.boot.bean;
import lombok.Data;
@Data //添加get set方法
public class Pet {
private String name;
private Double weight;
}
person:
# 双引号回转义 单引号不会
userName: "zhangsan \n lisi"
boss: true
birth: 2019/12/9
age: 18
# interests:[篮球,足球] 数组
interests:
- 篮球
- 足球
- 18
# List
animal: [阿猫,阿狗]
# map的写法
# score:
# english:80
# math: 90
score: {english: 80,math: 90}
salarys:
- 99999
- 88888
# 表示对象
pet:
name: 阿狗
weight: 99.99
allPets:
sick:
- {name: 阿狗,weight: 9999}
#一个 -代表着一个元素
- name: 阿猫
weight: 123
- name: 啊从
weight: 222
health:
- {name: 阿w,weight: 9999}
- {name: 阿a,weight: 9999}
- {name: 阿b,weight: 9999}
package com.item.boot.controller;
import com.item.boot.bean.Person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Autowired
Person person;
@RequestMapping("/person")
public Person person(){
return person;
}
}
自定义的类和配置文件绑定一般没有提示。
导入依赖
<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>
要重新打包一次才有效果