Yaml语法学习

编程入门 行业动态 更新时间:2024-10-26 22:27:22

Yaml<a href=https://www.elefans.com/category/jswz/34/1770552.html style=语法学习"/>

Yaml语法学习

SpringBoot使用一个全局的配置文件 , 配置文件名称是固定的

  • application.properties(官方不推荐)

        语法结构 : key=value

  • application.yml

        语法结构 :key:空格 value

server:port: 8081

配置文件的作用 :修改SpringBoot自动配置的默认值,因为SpringBoot在底层都给我们自动配置好了 

奇怪的yaml

xml和yaml的区别 

xml 

<server><port>8081<port>
</server>

yaml

server:port: 8081

yaml的基础语法 

说明:语法要求严格!

  1. 空格不能省略
  2. 以缩进来控制层级关系,只要是左边对齐的一列数据都是同一个层级的
  3. 属性和值的大小写都是十分敏感的。 

对象、Map(键值对) 

#对象、Map格式
k:v1:v2:
student:name: qinjiangage: 3

 行内写法

 student: {name: qinjiang,age: 3}

数组( List、set )

用 - 值表示数组中的一个元素,比如:

pets:- cat- dog- pig

行内写法

pets: [cat,dog,pig]

注入配置文件 

yaml文件更强大的地方在于,他可以给我们的实体类直接注入匹配值!

易错点: 

必须在主启动类的同级目录下建包(如:pojo),因为idea只会扫描他的同级目录! 

dog类 

package com.kuang.springboot01.pojo;import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;@Component  //@Component注解作用于类,可以把类自动装配到Spring容器中
public class Dog {@Value("旺财")private String name;@Value("3")private Integer age;public Dog() {}public Dog(String name, Integer age) {this.name = name;this.age = age;}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;}@Overridepublic String toString() {return "Dog{" +"name='" + name + '\'' +", age=" + age +'}';}
}

上面是旧的赋值方式!  

新方法 

注意:一个@Autowired下面只能对应一个注入属性

如果注入多个属性,要写多个@Autowired!

在编写 application.properties/yaml 配置文件时,由于要配置的 Person 对象属性是我们自定义的,SprimgBoot无法自动识别,所以不会有任何书写提示!在实际开发中,为了出现代码提示的效果来方便配直。在使用@ConfigurationProperties 注解进行配置文件属性值注入时,可以在pom.xml文件中添加一个Spring Boot 提供的配置处理器依赖,示例代码如下。 

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><optional>true</optional>
</dependency>

在pom.xml中添加上述配置依赖后,还需要重新运行项目启动类或者使用“Ctit+F9”组合键(即 Buildoject)重构当前Spring Boot项目方可生效!!!

package com.kuang.springboot01.pojo;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.Objects;
@Component
@ConfigurationProperties(prefix = "person")
public class Person {private String name;private Integer age;private Boolean happy;private Date birth;private Map<String, Object> maps;private List<Object> lists;private Dog dog;public Person() {}public Person(String name, Integer age, Boolean happy, Date birth, Map<String, Object> maps, List<Object> lists, Dog dog) {this.name = name;this.age = age;this.happy = happy;this.birth = birth;this.maps = maps;this.lists = lists;this.dog = dog;}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 Boolean getHappy() {return happy;}public void setHappy(Boolean happy) {this.happy = happy;}public Date getBirth() {return birth;}public void setBirth(Date birth) {this.birth = birth;}public Map<String, Object> getMaps() {return maps;}public void setMaps(Map<String, Object> maps) {this.maps = maps;}public List<Object> getLists() {return lists;}public void setLists(List<Object> lists) {this.lists = lists;}public Dog getDog() {return dog;}public void setDog(Dog dog) {this.dog = dog;}@Overridepublic String toString() {return "Person{" +"name='" + name + '\'' +", age=" + age +", happy=" + happy +", birth=" + birth +", maps=" + maps +", lists=" + lists +", dog=" + dog +'}';}
}
Person:name: hjmage: 18happy: truebirth: 2023/10/18lists:- code- music- girldog:name: 旺财age: 3maps:k1: v1k2: v2
package com.kuang.springboot01;import com.kuang.springboot01.pojo.Dog;
import com.kuang.springboot01.pojo.Person;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest
class SpringBoot01ApplicationTests {@Autowiredprivate Person person;@Autowiredprivate Dog dog;@Testvoid contextLoads() {System.out.println(person);System.out.println(dog);}}

运行结果

Person{name='hjm', age=18, happy=true, birth=Wed Oct 18 00:00:00 CST 2023, maps={k1=v1, k2=v2}, lists=[code, music, girl], dog=Dog{name='旺财', age=3}}
Dog{name='旺财', age=3}

所以我非常推荐使用yaml来进行配置!!!

注意点:

  • @configurationProperties:默认从全局配置文件(yaml)中获取值
  • @PropertySource :加载指定的配置文件

对比总结

@configurationProperties:@Value
功能批量注入配置文件中的属性一个个指定
松散绑定( 松散语法 )支持不支持
SpEL不支持支持
JSR303数据校验支持不支持
复杂类型封装支持不支持
  1.  @ConfigurationProperties只需要写一次即可 , @Value则需要每个字段都添加
  2. 松散绑定:这个什么意思呢? 比如我的yml中写的last-name,这个和lastName是一样的, - 后面跟着的字母默认是大写的。这就是松散绑定。
  3. JSR303数据校验 , 这个就是我们可以在字段是增加一层过滤器验证 , 可以保证数据的合法性
  4. 复杂类型封装,yml中可以封装对象 , 使用value就不支持

更多推荐

Yaml语法学习

本文发布于:2023-12-05 08:21:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1663655.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:语法   Yaml

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!