SpringBoot 之 配置文件、yaml语法、配置注入、松散绑定
配置文件
SpringBoot 有两种配置文件格式,二选一即可,官方推荐 yaml:
- application.properties key=value的格式
- application.yaml key: value的格式
配置文件位置
SpringBoot会从这四个位置全部加载主配置文件;互补配置。优先级从高到低。
- --spring.config.location=F:/application.yaml
- /config/application.yaml
- /application.yaml
- /src/main/resources/config/application.yaml
- /src/main/resources/application.yaml
yaml 格式特点
- 冒号后面必须有一个空格,不能省略
- 以缩进来控制层级关系,左边对齐的为同一层级
- 属性和值的大小写敏感
- 双引号中的特殊字符会转义输出,如"hello \n world"会发生换行
- 双引号中的特殊字符会原样输出,如‘hello \n world’所见即所得
配置示例
# src/main/resources/application.properties
server.port=8081
# src/main/resources/application.yaml
server:
port: 8081
# 对象形式
student:
name: zhangsan
age: 18
# 行内对象形式
student: {name: zhangsan,age: 18}
# 数组形式
pets:
- cat
- dog
- pig
# 行内数组形式
pets: [cat,dog,pig]
配置 banner
# src/main/resources/banner.txt
# https://www.bootschool.net/ascii-art
_ _
_(9(9)__ __/^\/^\__
/o o \/_ __\_\_/\_/_/_
\___, \/_ _\.' './_ _/\_
`---`\ \/_ _\/ \/_ _|.'_/
\ \/_\/ / \/_ |/ /
\ `-' | ';_:' /
/| \ \ .'
/_/ |,___.-`', /`'---`
/___/` /____/
注入 yaml 配置文件(方式一)
package com.wu.helloworld.pojo;
@Component
public class Dog {
@Value("阿黄")
private String name;
@Value("18")
private Integer age;
}
@SpringBootTest
class HelloworldApplicationTests {
@Autowired
private Dog dog;
@Test
public void contextLoads() {
System.out.println(dog)
}
}
注入 yaml 配置文件(方式二)
<!-- 导入配置文件处理器依赖,需要重启IDE -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
package com.wu.helloworld.pojo;
@Component
public class Dog {
private String name;
private Integer age;
//构造函数、get、set、toString 等方法
}
@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;
//构造函数、get、set、toString 等方法
}
person:
name: wu_${random.uuid}
age: ${random.int}
happy: false
birth: 2000/01/01
maps: {k1: v1,k2: v2}
lists:
- code
- girl
- music
dog:
name: ${person.dogName:默认名字}_旺财
age: 1
@SpringBootTest
class DemoApplicationTests {
@Autowired
Person person;
@Test
public void contextLoads() {
System.out.println(person);
}
}
注入 properties 配置文件
设置 properties 的编码格式为UTF-8:
File / Settings / File Encodings / Properties Files / UTF-8 && √ Transparent native-to-ascii conversion
# src/main/resources/person.properties
person.name=wu
person.age=18
person.sex=男
// 使用 PropertySource 注解加载制定的配置文件
@PropertySource(value = "classpath:person.properties")
@Component
public class Person {
@Value("${person.name}") // 从配置文件中取值
private String name;
@Value("#{9*2}") // #{SPEL} Spring表达式
private int age;
@Value("男") // 字面量
private String sex;
}
松散绑定
dog:
first-name: 旺财
age: 3
@Component
@ConfigurationProperties(prefix = "dog")
public class Dog {
private String firstName; // 可以绑定横杠的配置值
private Integer age;
}
SpringBoot 之 配置文件、yaml语法、配置注入、松散绑定的更多相关文章
- SpringBoot:配置文件及自动配置原理
西部开源-秦疆老师:基于SpringBoot 2.1.6 的博客教程 秦老师交流Q群号: 664386224 未授权禁止转载!编辑不易 , 转发请注明出处!防君子不防小人,共勉! SpringBoot ...
- springboot(3)——配置文件和自动配置原理详细讲解
原文地址 目录 概述 1. 配置文件作用 2.配置文件位置 3.配置文件的定义 3.1如果是定义普通变量(数字 字符串 布尔) 3.2如果是定义对象.Map 3.3如果是定义数组 4.配置文件的使用 ...
- JAVAEE——SpringBoot配置篇:配置文件、YAML语法、文件值注入、加载位置与顺序、自动配置原理
转载 https://www.cnblogs.com/xieyupeng/p/9664104.html @Value获取值和@ConfigurationProperties获取值比较 @Confi ...
- SpringBoot基础学习(二) SpringBoot全局配置文件及配置文件属性值注入
全局配置文件 全局配置文件能够对一些默认配置值进行修改.SpringBoot 使用一个名为 application.properties 或者 application.yaml的文件作为全局配置文件, ...
- SpringBoot 属性配置文件数据注入配置和yml与properties区别
前言 我们知道SpringBoot 通过配置类来解放一堆的xml文件配置,通属性配置文件,来进行,系统全局属性配置,这样极大的简化了我们开发过程,java web 也可以甜甜的从此 快速配置 Spri ...
- springboot----四、yaml配置注入
四.yaml配置注入 4.1.配置文件 SpringBoot使用一个全局的配置文件 , 配置文件名称是固定的 application.properties 语法结构 :key=value applic ...
- SpringBoot系列之YAML配置用法
1.全局配置 SpringBoot的全局配置文件有两种: application.properties application.yml 配置文件的作用:修改SpringBoot自动配置的默认值,主要是 ...
- SpringBoot读取yml中的配置,并分离配置文件
前言 在项目中经常遇到需要读取配置文件中的配置信息,这些配置信息之所以不写在代码中是因为实际项目发布或者部署之后会进行更改,而如果写在代码中编译之后没有办法进行修改. 之前使用的是properties ...
- SpringBoot之yaml语法及静态资源访问
配置文件-yaml 在spring Boot开发中推荐使用yaml来作为配置文件. 基本语法: key: value:kv之间有空格 大小写敏感 使用缩进表示层级关系 缩进不允许使用tab,只允许空格 ...
随机推荐
- 手写Starter
一. Starter工程的命名 Spring 官方定义的Starter通常命名遵循的格式为spring-boot-starter-{name},例如 spring-boot-starter-web.S ...
- 【Python】【Module】random
mport random print random.random() print random.randint(1,2) print random.randrange(1,10) 随机数 import ...
- 【Linux】【Basis】进程及作业管理
进程及作业管理 内核的功用:进程管理.文件系统.网络功能.内存管理.驱动程序.安全功能 Process: 运行中的程序的一个副本: 存在生命周期 L ...
- SSO(单点登录)示例
此文为转载文章,出处:https://www.cnblogs.com/jpfss/p/9273680.html SSO在我们的应用中非常常见,例如我们在OA系统登录了,我们就可以直接进入采购系统,不需 ...
- ANTLR 相关术语
下面介绍很多重要的与语言识别相关的术语. 语言(Language) A language is a set of valid sentences 一门语言是一个有效语句的集合. Sentences a ...
- shell脚本 阿里云基线检查一键配置
一.简介 源码地址 日期:2017/9/1 介绍:安全加固脚本,会符合阿里云基线检查.有幂等性,可重复执行 效果图: 二.使用 适用:centos6/7 语言:中文 注意:脚本是符合阿里云基线检查的配 ...
- Set数据结构基本介绍
构造 const set = new Set([1, 2, 3, 4, 4]); 可接受的参数为所有具有iterable 接口的数据 特性: 类似数组,无重复值. const set = new Se ...
- Node.js 中文乱码解决
Node.js 中文乱码解决 Node.js 支持中文不太好(实际上是Javascript支持),见<Node.js开发指南>. 要想Node.js正常显示中文,需要两点: 1.js文件保 ...
- 日程选项卡的设置(Project)
<Project2016 企业项目管理实践>张会斌 董方好 编著 在使用任何一个软件之前,都有一些默认的东东要改,比如在Excel里有人不待见单元格里的0,一定要设置成不显示零值:在Wor ...
- MH/T4029.3 IFPL报文解析
MH/T4029.3是民航业用来规定飞行计划相关数据交互的规范,今天我们先来解析下其中I类的IFPL报文. 我们先来看看IFPL报文长啥样. ZCZC -TITLE IFPL -FILTIM 0109 ...