1、基本语法
k:(空格)v:表示一对键值对(一个空格必须有);
以空格的缩进来控制层级关系;只要是左对齐的一列数据,都是同一个层级的
属性和值也是大小写敏感;

  1. server:
  2. port: 8081
  3. path: /hello

2、值的写法
字面量:

普通的值(数字,字符串,布尔)
k: v:字面直接来写;
字符串:

默认不用加上单引号或者双引号;
"":双引号;不会转义字符串里面的特殊字符;特殊字符会作为本身想表示的意思
name: "zhangsan \n lisi":输出;zhangsan 换行 lisi
'':单引号;会转义特殊字符,特殊字符最终只是一个普通的字符串数据
name: ‘zhangsan \n lisi’:输出;zhangsan \n lisi

对象、Map(属性和值)(键值对):
k: v:在下一行来写对象的属性和值的关系;注意缩进
对象还是k: v的方式

  1. friends:
  2. lastName: zhangsan
  3. age: 20

行内写法:

  1. friends: {lastName: zhangsan,age: 18}

数组(List、Set):
用- 值表示数组中的一个元素

  1. pets:
  2. cat
  3. dog
  4. pig

行内写法:

  1. pets: [cat,dog,pig]

 3.springboot配置类

3.1pom文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5.  
  6. <groupId>com.zy</groupId>
  7. <artifactId>spring-boot-yaml-demo</artifactId>
  8. <version>0.0.1-SNAPSHOT</version>
  9. <packaging>jar</packaging>
  10.  
  11. <name>spring-boot-yaml-demo</name>
  12. <description>Demo project for Spring Boot</description>
  13.  
  14. <parent>
  15. <groupId>org.springframework.boot</groupId>
  16. <artifactId>spring-boot-starter-parent</artifactId>
  17. <version>1.5.14.RELEASE</version>
  18. <relativePath/> <!-- lookup parent from repository -->
  19. </parent>
  20.  
  21. <properties>
  22. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  23. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  24. <java.version>1.8</java.version>
  25. </properties>
  26.  
  27. <dependencies>
  28. <dependency>
  29. <groupId>org.springframework.boot</groupId>
  30. <artifactId>spring-boot-starter-web</artifactId>
  31. </dependency>
  32.  
  33. <dependency>
  34. <groupId>org.springframework.boot</groupId>
  35. <artifactId>spring-boot-starter-test</artifactId>
  36. <scope>test</scope>
  37. </dependency>
  38.  
  39. <dependency>
  40. <groupId>org.projectlombok</groupId>
  41. <artifactId>lombok</artifactId>
  42. <version>1.16.20</version>
  43. </dependency>
  44.  
  45. <dependency>
  46. <groupId>org.springframework.boot</groupId>
  47. <artifactId>spring-boot-configuration-processor</artifactId>
  48. <optional>true</optional>
  49. </dependency>
  50.  
  51. <dependency>
  52. <groupId>com.netflix.archaius</groupId>
  53. <artifactId>archaius-core</artifactId>
  54. <version>0.7.6</version>
  55. </dependency>
  56. </dependencies>
  57.  
  58. <build>
  59. <plugins>
  60. <plugin>
  61. <groupId>org.springframework.boot</groupId>
  62. <artifactId>spring-boot-maven-plugin</artifactId>
  63. </plugin>
  64. </plugins>
  65. </build>
  66.  
  67. </project>

3.2实体类

  1. package com.zy.model;
  2.  
  3. import lombok.AllArgsConstructor;
  4. import lombok.Builder;
  5. import lombok.Data;
  6. import lombok.NoArgsConstructor;
  7.  
  8. @Data
  9. @AllArgsConstructor
  10. @NoArgsConstructor
  11. @Builder
  12. public class Cat {
  13.  
  14. private String name;
  15.  
  16. private Integer age;
  17.  
  18. }

3.3配置类

  1. package com.zy.config;
  2.  
  3. import com.zy.model.Cat;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6.  
  7. @Configuration
  8. public class MyConfigBean {
  9.  
  10. @Bean
  11. //将方法的返回值添加到容器中;容器中这个组件默认的id就是方法名
  12. public Cat getCat(){
  13. return new Cat("tom",20);
  14. }
  15. }

3.4获得bean

  1. @RunWith(SpringRunner.class)
  2. @SpringBootTest
  3. public class SpringBootYamlDemoApplicationTests {
  4.  
  5. @Autowired
  6. private ApplicationContext applicationContext;
  7.  
  8. @Test
  9. public void fn(){
  10. Cat cat = (Cat) applicationContext.getBean("getCat");
  11. System.out.println(cat);
  12. }
  13.  
  14. }

4.springboot取yaml中的值(在上述代码基础上)

  1. package com.zy.model;
  2.  
  3. import lombok.AllArgsConstructor;
  4. import lombok.Builder;
  5. import lombok.Data;
  6. import lombok.NoArgsConstructor;
  7. import org.springframework.boot.context.properties.ConfigurationProperties;
  8. import org.springframework.stereotype.Component;
  9.  
  10. import java.util.Date;
  11. import java.util.List;
  12. import java.util.Map;
  13. @Data
  14. @NoArgsConstructor
  15. @AllArgsConstructor
  16. @Builder
  17. @Component
  18. //@PropertySource(value = {"classpath:person.properties"}) 另一种配置方式
  19. @ConfigurationProperties(prefix = "person") // 该prefix对应于yaml文件中的person
  20. /**
  21. * 将配置文件中配置的每一个属性的值,映射到这个组件中
  22. * @ConfigurationProperties:告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定;
  23. * prefix = "person":配置文件中哪个下面的所有属性进行一一映射
  24. *
  25. * 只有这个组件是容器中的组件,才能容器提供的@ConfigurationProperties功能;
  26. * @ConfigurationProperties(prefix = "person")默认从全局配置文件中获取值;
  27. *
  28. */
  29. public class Person {
  30.  
  31. private String lastName;
  32.  
  33. private Integer age;
  34.  
  35. private Boolean boss;
  36.  
  37. private Date birth;
  38.  
  39. private Map<String,Object> maps;
  40.  
  41. private List<Object> lists;
  42.  
  43. private Dog dog;
  44.  
  45. }
  1. package com.zy.model;
  2.  
  3. import lombok.AllArgsConstructor;
  4. import lombok.Builder;
  5. import lombok.Data;
  6. import lombok.NoArgsConstructor;
  7. import org.springframework.boot.context.properties.ConfigurationProperties;
  8. import org.springframework.stereotype.Component;
  9.  
  10. @Data
  11. @NoArgsConstructor
  12. @AllArgsConstructor
  13. @Builder
  14. @Component
  15. @ConfigurationProperties(prefix = "dog") // 该prefix对应于yaml文件中的dog
  16. public class Dog {
  17.  
  18. private String name;
  19.  
  20. private Integer age;
  21.  
  22. }
  1. server:
  2. port: 8080
  3. person:
  4. lastName: 张三
  5. age: 20
  6. boss: false
  7. birth: 1999/09/09
  8. maps: {k1: v1,k2: v2}
  9. lists:
  10. - z1
  11. - z2
  12. - z3
  13. dog:
  14. name: little dog
  15. age: 2
  1. package com.zy;
  2.  
  3. import com.zy.model.Cat;
  4. import com.zy.model.Person;
  5. import org.junit.Test;
  6. import org.junit.runner.RunWith;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.boot.test.context.SpringBootTest;
  9. import org.springframework.context.ApplicationContext;
  10. import org.springframework.test.context.junit4.SpringRunner;
  11.  
  12. @RunWith(SpringRunner.class)
  13. @SpringBootTest
  14. public class SpringBootYamlDemoApplicationTests {
  15.  
  16. @Autowired
  17. private Person person;
  18.  
  19. @Test
  20. public void contextLoads() {
  21. System.out.println(person);
  22. }
  23.  
  24. }

5.springboot配置文件加载优先级

7.外部配置加载顺序
SpringBoot也可以从以下位置加载配置; 优先级从高到低;高优先级的配置覆盖低优先级的配置,所有的配置会
形成互补配置
7.1.命令行参数
所有的配置都可以在命令行上进行指定
java -jar spring-boot-02-config-02-0.0.1-SNAPSHOT.jar --server.port=8087 --server.context-path=/abc
多个配置用空格分开; --配置项=值
7.2.来自java:comp/env的JNDI属性
7.3.Java系统属性(System.getProperties())
7.4.操作系统环境变量
7.5.RandomValuePropertySource配置的random.*属性值
由jar包外向jar包内进行寻找;
优先加载带profile
7.6.jar包外部的application-{profile}.properties或application.yml(带spring.profile)配置文件
7.7.jar包内部的application-{profile}.properties或application.yml(带spring.profile)配置文件
再来加载不带profile
7.8.jar包外部的application.properties或application.yml(不带spring.profile)配置文件
7.9.jar包内部的application.properties或application.yml(不带spring.profile)配置文件
7.10.@Configuration注解类上的@PropertySource
7.11.通过SpringApplication.setDefaultProperties指定的默认属性
所有支持的配置加载来源;

springboot的yaml基础语法与取值,配置类,配置文件加载优先级的更多相关文章

  1. SpringBoot使用@Value从yml文件取值为空--注入静态变量

    SpringBoot使用@Value从yml文件取值为空--注入静态变量     1.application.yml中配置内容如下:   pcacmgr:   publicCertFilePath: ...

  2. JAVA基础语法:函数(方法)、类和对象(转载)

    4.JAVA基础语法:函数(方法).类和对象 函数 在java中函数也称为方法,是一段具备某种功能的可重用代码块. 一个函数包括这几部分: 函数头 函数头包括函数访问修饰符,函数返回值类型, 函数名, ...

  3. springboot属性类自动加载配置文件中的值

    springboot属性类自动加载配置文件中的值,如Person类加载在yml中配置的name,age等属性值,可以通过如下步骤获取: 类上添加@ConfigurationProperties注解,p ...

  4. 【SpringBoot基础系列-实战】如何指定 bean 最先加载(应用篇)

    [基础系列-实战]如何指定 bean 最先加载(应用篇) 在日常的业务开发中,绝大多数我们都是不关注 bean 的加载顺序,然而如果在某些场景下,当我们希望某个 bean 优于其他的 bean 被实例 ...

  5. 配置文件中取值: spring配置文件中util:properties和context:property-placeholder

    转载大神 https://blog.csdn.net/n447194252/article/details/77498916 util:properties和context:property-plac ...

  6. DB数据源之SpringBoot+MyBatis踏坑过程(二)手工配置数据源与加载Mapper.xml扫描

    DB数据源之SpringBoot+MyBatis踏坑过程(二)手工配置数据源与加载Mapper.xml扫描 liuyuhang原创,未经允许进制转载  吐槽之后应该有所改了,该方式可以作为一种过渡方式 ...

  7. DB数据源之SpringBoot+MyBatis踏坑过程(三)手工+半自动注解配置数据源与加载Mapper.xml扫描

    DB数据源之SpringBoot+MyBatis踏坑过程(三)手工+半自动注解配置数据源与加载Mapper.xml扫描 liuyuhang原创,未经允许禁止转载    系列目录连接 DB数据源之Spr ...

  8. SpringBoot——配置文件加载位置及外部配置加载顺序

    声明 本文部分转自:SpringBoot配置文件加载位置与优先级 正文 1. 项目内部配置文件 spring boot 启动会扫描以下位置的application.properties或者applic ...

  9. Python 爬取单个网页所需要加载的地址和CSS、JS文件地址

    Python 爬取单个网页所需要加载的URL地址和CSS.JS文件地址 通过学习Python爬虫,知道根据正式表达式匹配查找到所需要的内容(标题.图片.文章等等).而我从测试的角度去使用Python爬 ...

随机推荐

  1. Nginx的启动、停止、重启

    启动 启动代码格式:nginx安装目录地址 -c nginx配置文件地址 例如: [root@LinuxServer sbin]# /usr/local/nginx/sbin/nginx -c /us ...

  2. javascript DOM扩展querySelector()和和querySelectorAll()

    选在符的API的核心有两个方法:querySelector()和querySelectorAll() querySelector(a):a是一个css选择符,返回与该模式匹配的第一个元素,如果没有匹配 ...

  3. 第6章 进程控制(3)_wait、exec和system函数

    5. 等待函数 (1)wait和waitpid 头文件 #include <sys/types.h> #include <sys/wait.h> 函数 pid_t wait(i ...

  4. Spark分析之Master

    override def preStart() { logInfo("Starting Spark master at " + masterUrl) webUi.bind() // ...

  5. JS监听浏览器事件

    Onunload与Onbeforeunload Onunload,onbeforeunload都是在刷新或关闭时调用,可以在<script>脚本中通过window.onunload来指定或 ...

  6. Ingress.yaml

    apiVersion: extensions/v1beta1 kind: Ingress metadata: name: test-ingress namespace: default annotat ...

  7. AJAX服务器返回数据 连接数据库查询数据

    getcustomer.asp" 中的源代码负责对数据库进行查询,然后用 HTML 表格返回结果: <% response.expires=-1 sql="SELECT * ...

  8. Dictionary 字典的使用

    Dim a, d, i             '创建几个变量Set d = CreateObject("Scripting.Dictionary")d.Add "a&q ...

  9. 如何选择 SQL Server 数据库跟操作系统版本

    简介: 今天老大提需求, 需要一台 Windows 服务器, 需要安装最新版的 SQL Server 数据库.额, 上次搞 Windows 服务器还是4年前的事. 一.啥也没查, 直接下载操作系统.做 ...

  10. unity Object-c交互

    一.unity 调用 Object-c C/C++可以直接与Object-c交互,只要把文件后缀.m直接改成.mm,成为C/C++与Object-c混编文件.C#又可以调用C/C++方法,所以C#就是 ...