文章目录
yaml语法规则
yaml、yml文件
字面值表示方式
数组表示方式
核心规则
数据前面加空格与冒号隔开
总结
读取yml数据->@Value
编写yml文件
country: china
provice: beijing
city: beijing
area: haidian
port: 8080
party: true
birthday: 1949-10-01
user:
name: liuliu
age: 16
user2:
name: honghong
age: 18
a:
b:
c:
d: 123
likes: [game,music]
users:
- name: zhangsan
age: 18
- name: lisi
age: 17
server:
port: 80
读取单一的数据
package com.taotao.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* create by 刘鸿涛
* 2022/4/28 16:39
*/
@SuppressWarnings({"all"})
@RestController
@RequestMapping("/reads")
public class ReaderController {
//读取yaml数据中的单一数据
@Value("${country}")
private String country1;
@GetMapping
public String getById(){
System.out.println("spring boot 2 is running");
System.out.println("country1====>" + country1);
return "spring boot is running";
}
}
读取成功
读取二级数据
package com.taotao.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* create by 刘鸿涛
* 2022/4/28 16:39
*/
@SuppressWarnings({"all"})
@RestController
@RequestMapping("/reads")
public class ReaderController {
//读取二级数据
@Value("${user2.name}")
private String name2;
@Value("${user.name}")
private String name;
@GetMapping
public String getById(){
System.out.println("spring boot 2 is running");
System.out.println("name1====>" + name2);
System.out.println("name====>" + name);
return "spring boot is running";
}
}
读取成功
读取数组数据
package com.taotao.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* create by 刘鸿涛
* 2022/4/28 16:39
*/
@SuppressWarnings({"all"})
@RestController
@RequestMapping("/reads")
public class ReaderController {
//读取二级数据
@Value("${likes[0]}")
private String likes0;
@Value("${likes[1]}")
private String likes1;
@GetMapping
public String getById(){
System.out.println("spring boot 2 is running");
System.out.println("name1====>" + likes0);
System.out.println("name====>" + likes1);
return "spring boot is running";
}
}
读取成功
读取对象属性
package com.taotao.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* create by 刘鸿涛
* 2022/4/28 16:39
*/
@SuppressWarnings({"all"})
@RestController
@RequestMapping("/reads")
public class ReaderController {
@Value("${users[0].name}")
private String name;
@Value("${users[0].age}")
private String age;
@GetMapping
public String getById(){
System.out.println("spring boot 2 is running");
System.out.println("name1====>" + name);
System.out.println("name====>" + age);
return "spring boot is running";
}
}
读取成功
读取服务器端口
package com.taotao.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* create by 刘鸿涛
* 2022/4/28 16:39
*/
@SuppressWarnings({"all"})
@RestController
@RequestMapping("/reads")
public class ReaderController {
@Value("${server.port}")
private String port;
@GetMapping
public String getById(){
System.out.println("spring boot 2 is running");
System.out.println("port====>" + port);
return "spring boot is running";
}
}
读取成功
注意
8080端口是默认端口,读取不到