springboot全局配置文件可以使用application.properties或者application.yaml或application.yml
yaml是配置文件 ,适合用来表达或编辑数据结构、各种配置文件
YAML 的配置文件后缀为 .yml/yaml,如:application.yml/application.yaml。
基本语法
数据类型
YAML 支持以下几种数据类型:
基本用法:
k: (空格) v 表示键值对
以缩进来表示层级关系 只要是左对齐的一列 都是同一个层级的
jdbc:
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/SpringBoot
username: root
password: 123456
application.yaml
person:
age: 18
name: 张三
birthday: 2003/9/18
isstudent: true
maps: {k1: v1,k2: v2}
lists:
- a
- b
dog:
name: 小狗
age: 1
Person.java
/**
* @Component 将该bean 加入到spring容器
* @ConfigurationProperties(prefix = "person") 告诉springboot 将本类中的所有的属性和配置文件中的相关的配置进行绑定
* prefix = "person" :配置文件中的那个属性进行映射绑定
*
*/
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
int age;
String name;
Date birthday;
boolean isstudent;
Map maps;
List lists;
Dog dog;
public Person(int age, String name, Date birthday, boolean isstudent, Map maps, List lists, Dog dog) {
this.age = age;
this.name = name;
this.birthday = birthday;
this.isstudent = isstudent;
this.maps = maps;
this.lists = lists;
this.dog = dog;
}
public Person() {
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public boolean isIsstudent() {
return isstudent;
}
public void setIsstudent(boolean isstudent) {
this.isstudent = isstudent;
}
public Map getMaps() {
return maps;
}
public void setMaps(Map maps) {
this.maps = maps;
}
public List getLists() {
return lists;
}
public void setLists(List lists) {
this.lists = lists;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
@Override
public String toString() {
return "Person{" +
"age=" + age +
", name='" + name + '\'' +
", birthday=" + birthday +
", isstudent=" + isstudent +
", maps=" + maps +
", lists=" + lists +
", dog=" + dog +
'}';
}
}
Dog.java
public class Dog {
int age;
String name;
public Dog(int age, String name) {
this.age = age;
this.name = name;
}
public Dog() {
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Dog{" +
"age=" + age +
", name='" + name + '\'' +
'}';
}
}
helloController.java
@RestController
public class helloController {
@Autowired
Person person;
@RequestMapping("/hello")
public String hello() {
return person.toString();
}
}
启动项目,访问:localhost:8080/hello