- 大小写敏感
- 属性层级关系
- 使用缩进表示层级关系,同层级左侧对齐,只允许使用空格(不能使用tab)
- 属性值前面添加空格(属性名与属性值之间使用冒号+空格作为分隔)
- # 表示注释
- 使用 - 来表示数据开始符号(数组)
server:port: 82logging:level:root: infolikes:- music- game- PE
YAML的数据读取方法:首先我们先给出我们在yml文件中所列出的属性:
lesson: SpringBootserver:port: 80enterprise:name: itcastage: 16tel: 4006184000subject:- Java- 前端- 大数据
下面我们来介绍yaml数据读取的三种方法:- ${属性名},${属性名.属性名},${属性名.属性名[数组下标]}
package com.itheima.controller;import com.itheima.domain.Enterprise;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.core.env.Environment;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController@RequestMapping("/books")public class BookController {//使用@Value读取单一属性数据@Value("${lesson}")private String lesson;@Value("${server.port}")private Integer port;@Value("${enterprise.subject[0]}")private String subject_00;@GetMapping("/{id}")public String getById(@PathVariable Integer id){System.out.println(lesson);System.out.println(port);System.out.println(subject_00);return "hello , spring boot!";}}
- Environment对象匹配方法
package com.itheima.controller;import com.itheima.domain.Enterprise;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.core.env.Environment;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController@RequestMapping("/books")public class BookController {//使用Environment封装全配置数据(自动装配封装Environment,里面会包含yaml中所有属性和属性值)@Autowiredprivate Environment environment;@GetMapping("/{id}")public String getById(@PathVariable Integer id){// 我们采用environment的getProperty方法,根据属性名,获得属性值System.out.println(environment.getProperty("lesson"));System.out.println(environment.getProperty("server.port"));System.out.println(environment.getProperty("enterprise.age"));System.out.println(environment.getProperty("enterprise.subject[1]"));return "hello , spring boot!";}}
- 自定义对象封装指定数据
// 自定义对象Enterprise实现类(属于Domain)package com.itheima.domain;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.stereotype.Component;import java.util.Arrays;//封装yaml对象格式数据必须先声明当前实体类受Spring管控@Component//使用@ConfigurationProperties注解定义当前实体类读取配置属性信息 , 通过prefix属性设置读取哪个数据@ConfigurationProperties(prefix = "enterprise")public class Enterprise {private String name;private Integer age;private String tel;private String[] subject;@Overridepublic String toString() {return "Enterprise{" +"name='" + name + '\'' +", age=" + age +", tel='" + tel + '\'' +", subject=" + Arrays.toString(subject) +'}';}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public String getTel() {return tel;}public void setTel(String tel) {this.tel = tel;}public String[] getSubject() {return subject;}public void setSubject(String[] subject) {this.subject = subject;}}
// 服务层Controllerpackage com.itheima.controller;import com.itheima.domain.Enterprise;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.core.env.Environment;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController@RequestMapping("/books")public class BookController {// 自动装配实现类即可@Autowiredprivate Enterprise enterprise;@GetMapping("/{id}")public String getById(@PathVariable Integer id){System.out.println(enterprise);return "hello , spring boot!";}}
<!--实现自定义对象封装时会产生警告,我们需要添加以下依赖--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><optional>true</optional></dependency>
推荐阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- DNF怎么获取徽章(dnf徽章毕业要多少钱)
- 荣耀pro60手机怎么跟电脑连接
- 手机如何连接电脑界面(手机连接电脑设置界面)
- 手机与电脑怎么连接(vivo手机连接电脑方法)
- 我的世界中怎么驯服马(马吃什么驯服我的世界)
- 手机连接电脑怎么看手机里的文档(手机用数据线连接电脑)
- 手机怎样连接电脑方法(vivo手机连接电脑方法)
- envoy开发调试环境搭建
- 我的世界如何驯马(我的世界里马如何驯骑)
- 我的世界怎么控制马(我的世界驯服大全)