springboot的yaml基础语法与取值,配置类,配置文件加载优先级
1、基本语法
k:(空格)v:表示一对键值对(一个空格必须有);
以空格的缩进来控制层级关系;只要是左对齐的一列数据,都是同一个层级的
属性和值也是大小写敏感;
- server:
- port: 8081
- path: /hello
2、值的写法
字面量:
普通的值(数字,字符串,布尔)
k: v:字面直接来写;
字符串:
默认不用加上单引号或者双引号;
"":双引号;不会转义字符串里面的特殊字符;特殊字符会作为本身想表示的意思
name: "zhangsan \n lisi":输出;zhangsan 换行 lisi
'':单引号;会转义特殊字符,特殊字符最终只是一个普通的字符串数据
name: ‘zhangsan \n lisi’:输出;zhangsan \n lisi
对象、Map(属性和值)(键值对):
k: v:在下一行来写对象的属性和值的关系;注意缩进
对象还是k: v的方式
- friends:
- lastName: zhangsan
- age: 20
行内写法:
- friends: {lastName: zhangsan,age: 18}
数组(List、Set):
用- 值表示数组中的一个元素
- pets:
- ‐ cat
- ‐ dog
- ‐ pig
行内写法:
- pets: [cat,dog,pig]
3.springboot配置类
3.1pom文件
- <?xml version="1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>com.zy</groupId>
- <artifactId>spring-boot-yaml-demo</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <packaging>jar</packaging>
- <name>spring-boot-yaml-demo</name>
- <description>Demo project for Spring Boot</description>
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>1.5.14.RELEASE</version>
- <relativePath/> <!-- lookup parent from repository -->
- </parent>
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
- <java.version>1.8</java.version>
- </properties>
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>org.projectlombok</groupId>
- <artifactId>lombok</artifactId>
- <version>1.16.20</version>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-configuration-processor</artifactId>
- <optional>true</optional>
- </dependency>
- <dependency>
- <groupId>com.netflix.archaius</groupId>
- <artifactId>archaius-core</artifactId>
- <version>0.7.6</version>
- </dependency>
- </dependencies>
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- </plugins>
- </build>
- </project>
3.2实体类
- package com.zy.model;
- import lombok.AllArgsConstructor;
- import lombok.Builder;
- import lombok.Data;
- import lombok.NoArgsConstructor;
- @Data
- @AllArgsConstructor
- @NoArgsConstructor
- @Builder
- public class Cat {
- private String name;
- private Integer age;
- }
3.3配置类
- package com.zy.config;
- import com.zy.model.Cat;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- @Configuration
- public class MyConfigBean {
- @Bean
- //将方法的返回值添加到容器中;容器中这个组件默认的id就是方法名
- public Cat getCat(){
- return new Cat("tom",20);
- }
- }
3.4获得bean
- @RunWith(SpringRunner.class)
- @SpringBootTest
- public class SpringBootYamlDemoApplicationTests {
- @Autowired
- private ApplicationContext applicationContext;
- @Test
- public void fn(){
- Cat cat = (Cat) applicationContext.getBean("getCat");
- System.out.println(cat);
- }
- }
4.springboot取yaml中的值(在上述代码基础上)
- package com.zy.model;
- import lombok.AllArgsConstructor;
- import lombok.Builder;
- import lombok.Data;
- import lombok.NoArgsConstructor;
- import org.springframework.boot.context.properties.ConfigurationProperties;
- import org.springframework.stereotype.Component;
- import java.util.Date;
- import java.util.List;
- import java.util.Map;
- @Data
- @NoArgsConstructor
- @AllArgsConstructor
- @Builder
- @Component
- //@PropertySource(value = {"classpath:person.properties"}) 另一种配置方式
- @ConfigurationProperties(prefix = "person") // 该prefix对应于yaml文件中的person
- /**
- * 将配置文件中配置的每一个属性的值,映射到这个组件中
- * @ConfigurationProperties:告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定;
- * prefix = "person":配置文件中哪个下面的所有属性进行一一映射
- *
- * 只有这个组件是容器中的组件,才能容器提供的@ConfigurationProperties功能;
- * @ConfigurationProperties(prefix = "person")默认从全局配置文件中获取值;
- *
- */
- public class Person {
- private String lastName;
- private Integer age;
- private Boolean boss;
- private Date birth;
- private Map<String,Object> maps;
- private List<Object> lists;
- private Dog dog;
- }
- package com.zy.model;
- import lombok.AllArgsConstructor;
- import lombok.Builder;
- import lombok.Data;
- import lombok.NoArgsConstructor;
- import org.springframework.boot.context.properties.ConfigurationProperties;
- import org.springframework.stereotype.Component;
- @Data
- @NoArgsConstructor
- @AllArgsConstructor
- @Builder
- @Component
- @ConfigurationProperties(prefix = "dog") // 该prefix对应于yaml文件中的dog
- public class Dog {
- private String name;
- private Integer age;
- }
- server:
- port: 8080
- person:
- lastName: 张三
- age: 20
- boss: false
- birth: 1999/09/09
- maps: {k1: v1,k2: v2}
- lists:
- - z1
- - z2
- - z3
- dog:
- name: little dog
- age: 2
- package com.zy;
- import com.zy.model.Cat;
- import com.zy.model.Person;
- import org.junit.Test;
- import org.junit.runner.RunWith;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.test.context.SpringBootTest;
- import org.springframework.context.ApplicationContext;
- import org.springframework.test.context.junit4.SpringRunner;
- @RunWith(SpringRunner.class)
- @SpringBootTest
- public class SpringBootYamlDemoApplicationTests {
- @Autowired
- private Person person;
- @Test
- public void contextLoads() {
- System.out.println(person);
- }
- }
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基础语法与取值,配置类,配置文件加载优先级的更多相关文章
- SpringBoot使用@Value从yml文件取值为空--注入静态变量
SpringBoot使用@Value从yml文件取值为空--注入静态变量 1.application.yml中配置内容如下: pcacmgr: publicCertFilePath: ...
- JAVA基础语法:函数(方法)、类和对象(转载)
4.JAVA基础语法:函数(方法).类和对象 函数 在java中函数也称为方法,是一段具备某种功能的可重用代码块. 一个函数包括这几部分: 函数头 函数头包括函数访问修饰符,函数返回值类型, 函数名, ...
- springboot属性类自动加载配置文件中的值
springboot属性类自动加载配置文件中的值,如Person类加载在yml中配置的name,age等属性值,可以通过如下步骤获取: 类上添加@ConfigurationProperties注解,p ...
- 【SpringBoot基础系列-实战】如何指定 bean 最先加载(应用篇)
[基础系列-实战]如何指定 bean 最先加载(应用篇) 在日常的业务开发中,绝大多数我们都是不关注 bean 的加载顺序,然而如果在某些场景下,当我们希望某个 bean 优于其他的 bean 被实例 ...
- 配置文件中取值: spring配置文件中util:properties和context:property-placeholder
转载大神 https://blog.csdn.net/n447194252/article/details/77498916 util:properties和context:property-plac ...
- DB数据源之SpringBoot+MyBatis踏坑过程(二)手工配置数据源与加载Mapper.xml扫描
DB数据源之SpringBoot+MyBatis踏坑过程(二)手工配置数据源与加载Mapper.xml扫描 liuyuhang原创,未经允许进制转载 吐槽之后应该有所改了,该方式可以作为一种过渡方式 ...
- DB数据源之SpringBoot+MyBatis踏坑过程(三)手工+半自动注解配置数据源与加载Mapper.xml扫描
DB数据源之SpringBoot+MyBatis踏坑过程(三)手工+半自动注解配置数据源与加载Mapper.xml扫描 liuyuhang原创,未经允许禁止转载 系列目录连接 DB数据源之Spr ...
- SpringBoot——配置文件加载位置及外部配置加载顺序
声明 本文部分转自:SpringBoot配置文件加载位置与优先级 正文 1. 项目内部配置文件 spring boot 启动会扫描以下位置的application.properties或者applic ...
- Python 爬取单个网页所需要加载的地址和CSS、JS文件地址
Python 爬取单个网页所需要加载的URL地址和CSS.JS文件地址 通过学习Python爬虫,知道根据正式表达式匹配查找到所需要的内容(标题.图片.文章等等).而我从测试的角度去使用Python爬 ...
随机推荐
- Nginx的启动、停止、重启
启动 启动代码格式:nginx安装目录地址 -c nginx配置文件地址 例如: [root@LinuxServer sbin]# /usr/local/nginx/sbin/nginx -c /us ...
- javascript DOM扩展querySelector()和和querySelectorAll()
选在符的API的核心有两个方法:querySelector()和querySelectorAll() querySelector(a):a是一个css选择符,返回与该模式匹配的第一个元素,如果没有匹配 ...
- 第6章 进程控制(3)_wait、exec和system函数
5. 等待函数 (1)wait和waitpid 头文件 #include <sys/types.h> #include <sys/wait.h> 函数 pid_t wait(i ...
- Spark分析之Master
override def preStart() { logInfo("Starting Spark master at " + masterUrl) webUi.bind() // ...
- JS监听浏览器事件
Onunload与Onbeforeunload Onunload,onbeforeunload都是在刷新或关闭时调用,可以在<script>脚本中通过window.onunload来指定或 ...
- Ingress.yaml
apiVersion: extensions/v1beta1 kind: Ingress metadata: name: test-ingress namespace: default annotat ...
- AJAX服务器返回数据 连接数据库查询数据
getcustomer.asp" 中的源代码负责对数据库进行查询,然后用 HTML 表格返回结果: <% response.expires=-1 sql="SELECT * ...
- Dictionary 字典的使用
Dim a, d, i '创建几个变量Set d = CreateObject("Scripting.Dictionary")d.Add "a&q ...
- 如何选择 SQL Server 数据库跟操作系统版本
简介: 今天老大提需求, 需要一台 Windows 服务器, 需要安装最新版的 SQL Server 数据库.额, 上次搞 Windows 服务器还是4年前的事. 一.啥也没查, 直接下载操作系统.做 ...
- unity Object-c交互
一.unity 调用 Object-c C/C++可以直接与Object-c交互,只要把文件后缀.m直接改成.mm,成为C/C++与Object-c混编文件.C#又可以调用C/C++方法,所以C#就是 ...