springboot之YAML语法

[复制链接]
发表于 2025-11-5 20:07:24 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
目次
一、根本语法
写一个端标语和一个路径
Controller里的方法:
然后如许写才气访问到:
这是在yml内里写的,也可以写在properties里
再访问:
二、值的写法
1.平凡范例(数字、字符串、布尔)
例子1:
设置文件:
Controller
运行访问:
例子2:
设置文件:
Controller
运行访问:
例子3:
设置文件:
controller
运行访问
2.字符串默认不消加上单引号/双引号
""例子:
设置文件
controller
运行访问
''例子:
设置文件
controller
运行访问
3.对象、Map(属性和值)(键值对)
对象范例例子(第一种方式)
设置文件
实体类Person
Controller
运行访问
对象范例例子(第二种方式)
Person实体类
再访问:
对象范例例子(行内写法)
对象范例例子(properties)
Map范例例子
设置文件
实体类PersonMap
Controller
4.数组(List、Set)
数组范例例子List1
设置文件
设置类PetsList
Controller
数组范例例子数组2
设置文件
Pets类
PetController
运行:
5.对象内里含对象
设置文件
设置类PersonDog
Dog
Controller
运行
6.@Value获取值和@ConfigurationProperties获取值比力 
复杂范例封装
设置文件
PersonDog类
PersonDogController稳定
运行
SpEL
JSR303数据校验
例子@Value:
依赖
设置文件
Person类
Controller稳定
运行
现在把设置文件中的email: [email protected]改成email: 123
例子@ConfigurationProperties
设置文件
Person类
现在把设置文件中的email: [email protected]改成email: 123
复杂数据范例
7.编写设置提示
8.还支持运算
设置文件
Person类
运行:


一、根本语法

key空格)value :表现一对键值对(空格必须有);
以空格的缩进来控制层级关系;只要是左对齐的一列数据,都是同一个层级的
写一个端标语和一个路径

  1. server:
  2.   port: 8081
  3.   servlet:
  4.     context-path: /springboot05
复制代码

Controller里的方法:

  1. import org.springframework.stereotype.Controller;
  2. import org.springframework.web.bind.annotation.RequestMapping;
  3. import org.springframework.web.bind.annotation.ResponseBody;
  4. import javax.servlet.http.HttpServletResponse;
  5. import java.io.IOException;
  6. @Controller
  7. @RequestMapping("/index")
  8. public class IndexController {
  9.     @ResponseBody
  10.     @RequestMapping("/hello")
  11.     public String hello(){
  12.         return "hai!";
  13.     }
  14. }
复制代码
然后如许写才气访问到:


这是在yml内里写的,也可以写在properties里


再访问:

原来的路径报错

得如许写才行


二、值的写法

1.平凡范例(数字、字符串、布尔)

key:value  字面直接来写
例子1:

设置文件:


Controller

  1. @Controller
  2. @RequestMapping("/test")
  3. public class TestController {
  4.     @Value("${name}")
  5.     private String name;
  6.    
  7.     @RequestMapping("/hello")
  8.     @ResponseBody
  9.     public String sayHello(){
  10.         System.out.println(name);
  11.         return "say Hello";
  12.     }
  13. }
复制代码
运行访问:



例子2:

设置文件:


Controller

  1. @Controller
  2. @RequestMapping("/test")
  3. public class TestController {
  4.     @Value("${name}")
  5.     private String name;
  6.     @Value("${age}")
  7.     private String age;
  8.     @RequestMapping("/hello")
  9.     @ResponseBody
  10.     public String sayHello(){
  11.         System.out.println(name);
  12.         System.out.println(age);
  13.         return "say Hello";
  14.     }
  15. }
复制代码
运行访问:



例子3:

设置文件:


controller

  1. @Controller
  2. @RequestMapping("/test")
  3. public class TestController {
  4.     @Value("${name}")
  5.     private String name;
  6.     @Value("${age}")
  7.     private String age;
  8.     @Value("${boss}")
  9.     private String boss;
  10.     @RequestMapping("/hello")
  11.     @ResponseBody
  12.     public String sayHello(){
  13.         System.out.println(name);
  14.         System.out.println(age);
  15.         System.out.println(boss);
  16.         return "say Hello";
  17.     }
  18. }
复制代码
运行访问



2.字符串默认不消加上单引号/双引号

"":双引号;不会转义字符串内里的特别字符;特别字符会作为本身想表现的意思
name: "zhangsan \n lisi":输出;zhangsan 换行 lisi
""例子:

设置文件


controller

Controller跟 1.平凡的值例子1  内里的一样,没有变
运行访问


'':单引号;会转义特别字符,特别字符终极只是一个平凡的字符串数据
name: ‘zhangsan \n lisi’:输出;zhangsan \n lisi
''例子:

设置文件


controller

Controller跟 1.平凡的值例子1   内里的一样,没有变
运行访问


3.对象、Map(属性和值)(键值对)

key: value :在下一行来写对象的属性和值的关系;留意缩进
对象照旧k: v的方式

行内写法:

对象范例例子(第一种方式)

设置文件


实体类Person

  1. package com.qcby.springboot05.controller;
  2. import org.springframework.beans.factory.annotation.Value;
  3. import org.springframework.stereotype.Component;
  4. //@Value是spring的东西,new肯定不行,所以得把它交给spring去管理,所以加个@Component注解
  5. @Component
  6. public class Person {
  7.     @Value("${person.name}")
  8.     private String name;
  9.     @Value("${person.age}")
  10.     private Integer age;
  11.     @Value("${person.boss}")
  12.     private Boolean boss;
  13.     public Person() {
  14.     }
  15.     public Person(String name, Integer age, Boolean boss) {
  16.         this.name = name;
  17.         this.age = age;
  18.         this.boss = boss;
  19.     }
  20.     public String getName() {
  21.         return name;
  22.     }
  23.     public void setName(String name) {
  24.         this.name = name;
  25.     }
  26.     public Integer getAge() {
  27.         return age;
  28.     }
  29.     public void setAge(Integer age) {
  30.         this.age = age;
  31.     }
  32.     public Boolean getBoss() {
  33.         return boss;
  34.     }
  35.     public void setBoss(Boolean boss) {
  36.         this.boss = boss;
  37.     }
  38.     @Override
  39.     public String toString() {
  40.         return "Person{" +
  41.                 "name='" + name + '\'' +
  42.                 ", age=" + age +
  43.                 ", boss=" + boss +
  44.                 '}';
  45.     }
  46. }
复制代码
Controller

  1. @Controller
  2. @RequestMapping("/test")
  3. public class TestController {
  4.    
  5.     @Autowired
  6.     private Person person;
  7.     @RequestMapping("/hello")
  8.     @ResponseBody
  9.     public String sayHello(){
  10.         System.out.println(person);
  11.         return "say Hello";
  12.     }
  13. }
复制代码
运行访问



对象范例例子(第二种方式)

Person实体类

  1. import org.springframework.beans.factory.annotation.Value;
  2. import org.springframework.boot.context.properties.ConfigurationProperties;
  3. import org.springframework.stereotype.Component;
  4. //第一种方式:
  5. // @Value是spring的东西,new肯定不行,所以得把它交给spring去管理,所以加个@Component注解
  6. @Component
  7. //第二种方式
  8. // 加一个前缀,是springBoot的注解ConfigurationProperties;括号里面不加东西,默认是value
  9. //写上的话就是prefix  (prefix="person")
  10. //下面的@Value不用写了
  11. @ConfigurationProperties("person")
  12. public class Person {
  13.     //@Value("${person.name}")
  14.     private String name;
  15.     //@Value("${person.age}")
  16.     private Integer age;
  17.     //@Value("${person.boss}")
  18.     private Boolean boss;
  19.     public Person() {
  20.     }
  21.     public Person(String name, Integer age, Boolean boss) {
  22.         this.name = name;
  23.         this.age = age;
  24.         this.boss = boss;
  25.     }
  26.     public String getName() {
  27.         return name;
  28.     }
  29.     public void setName(String name) {
  30.         this.name = name;
  31.     }
  32.     public Integer getAge() {
  33.         return age;
  34.     }
  35.     public void setAge(Integer age) {
  36.         this.age = age;
  37.     }
  38.     public Boolean getBoss() {
  39.         return boss;
  40.     }
  41.     public void setBoss(Boolean boss) {
  42.         this.boss = boss;
  43.     }
  44.     @Override
  45.     public String toString() {
  46.         return "Person{" +
  47.                 "name='" + name + '\'' +
  48.                 ", age=" + age +
  49.                 ", boss=" + boss +
  50.                 '}';
  51.     }
  52. }
复制代码
再访问:


对象范例例子(行内写法)


如许也是能访问到的


对象范例例子(properties)

写到application.properties内里也是一样的

运行

第一次控制台输出的时间 张三 是 ??,编码标题,办理:

Map范例例子

用的@ConfigurationProperties的方式
设置文件


实体类PersonMap

  1. @Component
  2. @ConfigurationProperties(prefix = "my")
  3. public class PersonMap {
  4.     private Map<String, String> map;
  5.     public Map<String, String> getMap() {
  6.         return map;
  7.     }
  8.     public void setMap(Map<String, String> map) {
  9.         this.map = map;
  10.     }
  11.     @Override
  12.     public String toString() {
  13.         return "PersonMap{" +
  14.                 "map=" + map +
  15.                 '}';
  16.     }
  17. }
复制代码
Controller

  1. @Controller
  2. @RequestMapping("/api")
  3. public class PersonMapController {
  4.     @Autowired
  5.     private PersonMap personMap;
  6.     @RequestMapping("/map")
  7.     @ResponseBody
  8.     public String getMap() {
  9.         System.out.println(personMap);
  10.         return "map";
  11.     }
  12. }
复制代码
运行访问:


4.数组(List、Set)

用- 值表现数组中的一个元素
数组范例例子List1

设置文件


设置类PetsList

  1. @Component
  2. @ConfigurationProperties(prefix = "my1")
  3. public class PetsList {
  4.     private List<String> pets;
  5.     public PetsList() {
  6.     }
  7.     public PetsList(List<String> pets) {
  8.         this.pets = pets;
  9.     }
  10.     public List<String> getPets() {
  11.         return pets;
  12.     }
  13.     public void setPets(List<String> pets) {
  14.         this.pets = pets;
  15.     }
  16.     @Override
  17.     public String toString() {
  18.         return "PetsList{" +
  19.                 "pets=" + pets +
  20.                 '}';
  21.     }
  22. }
复制代码
Controller

  1. @Controller
  2. @RequestMapping("/pets")
  3. public class PetsListController {
  4.     @Autowired
  5.     private PetsList petsList;
  6.     @RequestMapping("/list")
  7.     @ResponseBody
  8.     public String petsList(){
  9.         System.out.println(petsList);
  10.         return "list";
  11.     }
  12. }
复制代码
运行访问:


数组范例例子数组2

设置文件

设置文件这里用{}不可,输出为null,必须用[]

Pets类

  1. @Component
  2. @ConfigurationProperties(prefix = "my2")
  3. public class Pets {
  4.     private String[] pets;
  5.     public String[] getPets() {
  6.         return pets;
  7.     }
  8.     public void setPets(String[] pets) {
  9.         this.pets = pets;
  10.     }
  11.     @Override
  12.     public String toString() {
  13.         return "Pets{" +
  14.                 "pets=" + Arrays.toString(pets) +
  15.                 '}';
  16.     }
  17. }
复制代码
PetController

  1. @Controller
  2. @RequestMapping("/pet")
  3. public class PetController {
  4.     @Autowired
  5.     private Pets pets;
  6.     @ResponseBody
  7.     @RequestMapping("/pets")
  8.     public String pets(){
  9.         System.out.println(pets);
  10.         return "数组";
  11.     }
  12. }
复制代码
运行:

 



5.对象内里含对象

设置文件


设置类PersonDog

  1. @Component
  2. @ConfigurationProperties(prefix = "person1")
  3. public class PersonDog {
  4.     private String name;
  5.     private int age;
  6.     private Dog dog;
  7.     public PersonDog() {
  8.     }
  9.     public PersonDog(String name, int age, Dog dog) {
  10.         this.name = name;
  11.         this.age = age;
  12.         this.dog = dog;
  13.     }
  14.     public String getName() {
  15.         return name;
  16.     }
  17.     public void setName(String name) {
  18.         this.name = name;
  19.     }
  20.     public int getAge() {
  21.         return age;
  22.     }
  23.     public void setAge(int age) {
  24.         this.age = age;
  25.     }
  26.     public Dog getDog() {
  27.         return dog;
  28.     }
  29.     public void setDog(Dog dog) {
  30.         this.dog = dog;
  31.     }
  32.     @Override
  33.     public String toString() {
  34.         return "PersonDog{" +
  35.                 "name='" + name + '\'' +
  36.                 ", age=" + age +
  37.                 ", dog=" + dog +
  38.                 '}';
  39.     }
  40. }
复制代码
Dog

  1. public class Dog {
  2.     private String name;
  3.     private int age;
  4.     public Dog() {
  5.     }
  6.     public Dog(String name, int age) {
  7.         this.name = name;
  8.         this.age = age;
  9.     }
  10.     public String getName() {
  11.         return name;
  12.     }
  13.     public void setName(String name) {
  14.         this.name = name;
  15.     }
  16.     public int getAge() {
  17.         return age;
  18.     }
  19.     public void setAge(int age) {
  20.         this.age = age;
  21.     }
  22.     @Override
  23.     public String toString() {
  24.         return "Dog{" +
  25.                 "name='" + name + '\'' +
  26.                 ", age=" + age +
  27.                 '}';
  28.     }
  29. }
复制代码
Controller

  1. @Controller
  2. @RequestMapping("/person")
  3. public class PersonDogController {
  4.     @Autowired
  5.     private PersonDog personDog;
  6.     @RequestMapping("/dog")
  7.     @ResponseBody
  8.     public String sayHello(){
  9.         System.out.println(personDog);
  10.         return "对象里面含对象";
  11.     }
  12. }
复制代码
运行



6.@Value获取值和@ConfigurationProperties获取值比力 


@ConfigurationProperties
@Value
功能
批量注入设置文件中的属性
一个个指定
疏松绑定(疏松语法)
支持
不支持
SpEL
不支持
支持
JSR303数据校验
支持
不支持
复杂范例封装
支持
不支持
复杂范例封装

支持的写过了,就是上面的对象内里含对象、数组那的也算、Map什么的,现在写不支持的
设置文件


PersonDog类

  1. @Component
  2. public class PersonDog {
  3.     @Value("${person1.name}")
  4.     private String name;
  5.     @Value("${person1.age}")
  6.     private int age;
  7.     @Value("${person1.dog}")
  8.     private Dog dog;
  9.     public PersonDog() {
  10.     }
  11.     public PersonDog(String name, int age, Dog dog) {
  12.         this.name = name;
  13.         this.age = age;
  14.         this.dog = dog;
  15.     }
  16.     public String getName() {
  17.         return name;
  18.     }
  19.     public void setName(String name) {
  20.         this.name = name;
  21.     }
  22.     public int getAge() {
  23.         return age;
  24.     }
  25.     public void setAge(int age) {
  26.         this.age = age;
  27.     }
  28.     public Dog getDog() {
  29.         return dog;
  30.     }
  31.     public void setDog(Dog dog) {
  32.         this.dog = dog;
  33.     }
  34.     @Override
  35.     public String toString() {
  36.         return "PersonDog{" +
  37.                 "name='" + name + '\'' +
  38.                 ", age=" + age +
  39.                 ", dog=" + dog +
  40.                 '}';
  41.     }
  42. }
复制代码
PersonDogController稳定

  1. @Controller
  2. @RequestMapping("/person")
  3. public class PersonDogController {
  4.     @Autowired
  5.     private PersonDog personDog;
  6.     @RequestMapping("/dog")
  7.     @ResponseBody
  8.     public String sayHello(){
  9.         System.out.println(personDog);
  10.         return "复杂类型封装";
  11.     }
  12. }
复制代码
运行

报错了,可见不支持

SpEL

@Value内里支持${person.name}这种写法
JSR303数据校验

比如有一个属性email(设置文件里也有哈),如果使用的是@Value,它是不支持@Email举行表单数据校验的
例子@Value:

依赖

要写数据校验,须要引入依赖
  1. <!-- 导入字段校验:hiberrate validator -->
  2. <dependency>
  3.     <groupId>org.hibernate</groupId>
  4.     <artifactId>hibernate-validator</artifactId>
  5.     <version>5.3.6.Final</version>
  6. </dependency>
复制代码
设置文件


Person类


Controller稳定

  1. import org.springframework.beans.factory.annotation.Autowired;
  2. import org.springframework.beans.factory.annotation.Value;
  3. import org.springframework.stereotype.Controller;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.ResponseBody;
  6. @Controller
  7. @RequestMapping("/test")
  8. public class TestController {
  9.     @Autowired
  10.     private Person person;
  11.     @RequestMapping("/hello")
  12.     @ResponseBody
  13.     public String sayHello(){
  14.         System.out.println(person);
  15.         return "say Hello";
  16.     }
  17. }
复制代码
运行



现在把设置文件中的email: [email protected]改成email: 123


没有报错,直接输出了,可见@Value是不支持数据校验的
例子@ConfigurationProperties

设置文件


Person类

须要加校验注解
  1. import org.springframework.beans.factory.annotation.Value;
  2. import org.springframework.boot.context.properties.ConfigurationProperties;
  3. import org.springframework.stereotype.Component;
  4. import org.springframework.validation.annotation.Validated;
  5. import javax.validation.constraints.Email;
  6. @Component
  7. @ConfigurationProperties("person")
  8. @Validated     //校验注解
  9. public class Person {
  10.     private String name;
  11.     private Integer age;
  12.     private Boolean boss;
  13.     @Email
  14.     private String email;
  15.     public Person() {
  16.     }
  17.     public Person(String name, Integer age, Boolean boss, String email) {
  18.         this.name = name;
  19.         this.age = age;
  20.         this.boss = boss;
  21.         this.email = email;
  22.     }
  23.     public String getEmail() {
  24.         return email;
  25.     }
  26.     public void setEmail(String email) {
  27.         this.email = email;
  28.     }
  29.     public String getName() {
  30.         return name;
  31.     }
  32.     public void setName(String name) {
  33.         this.name = name;
  34.     }
  35.     public Integer getAge() {
  36.         return age;
  37.     }
  38.     public void setAge(Integer age) {
  39.         this.age = age;
  40.     }
  41.     public Boolean getBoss() {
  42.         return boss;
  43.     }
  44.     public void setBoss(Boolean boss) {
  45.         this.boss = boss;
  46.     }
  47.     @Override
  48.     public String toString() {
  49.         return "Person{" +
  50.                 "name='" + name + '\'' +
  51.                 ", age=" + age +
  52.                 ", boss=" + boss +
  53.                 ", email=" + email +
  54.                 '}';
  55.     }
  56. }
复制代码
controller跟例子1一样,启动运行


现在把设置文件中的email: [email protected]改成email: 123


再启动:

复杂数据范例

比如数组、map等这些,@ Value都是不支持的
Map:

数组

7.编写设置提示

加一个依赖
  1. <dependency>
  2.     <groupId>org.springframework.boot</groupId>
  3.     <artifactId>spring-boot-configuration-processor</artifactId>
  4.     <optional>true</optional>
  5. </dependency>
复制代码
然后crtl+f9,把原来设置文件中的东西表明掉,现在重新写就有提示了:(就是得先有谁人实体类)

8.还支持运算

设置文件


Person类


运行:





回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表