SpringBoot配置文件
SpringBoot的配置文件:
Spring Boot使用全局的配置文件来修改SpringBoot的默认的配置,配置文件名必须是application。有两种形式的配置文件如下:application.properties、application.yml | application.yaml。
注:properties和yaml文件可以共存,但两种后缀的yaml只能存在一个,要么是yaml要么是yml。
yaml文件介绍:
yaml(YAML Ain\’t Markup Language)它是一种直观的能够被电脑识别的数据序列化格式,是一个可读性高并且容易被人类阅读,容易和脚本语言交互,用来表达资料序列的编程语言。它是类似于标准通用标记语言的子集XML的数据描述语言,语法比XML简单很多。比json、xml等更适合做配置文件。
yaml语法:
– 使用缩进表示层级关系。
– 缩进时不允许使用Tab键,只允许使用空格。
– 缩进的空格数目不重要,只要相同层级的元素左侧对齐即可。
– 大小写敏感。
yaml支持的三种数据结构:
– 对象:键值对的集合。
– 复杂类型:一组按次序排列的值。
– 基本类型:单个的、不可再分的值。
三种结构的写法:
1.对象(Map):
对象的一组键值对,使用冒号分隔。如:username: admin 冒号后面跟空格来分开键值。{}为行内写法。假如user是一个对象。
1 user:
2 name: zhangsan
3 age: 22
4
5 user: {name: zhangsan,age: 22}
2.复杂类型(list,set):
一组连词线(-)开头的行,构成一个数组,[]为行内写法。数组,对象可以组合使用。假如nums是一个集合。
1 nums: 2 - 10 3 - 20 4 - 30 5 6 nums: [10,20,30]
3.基本类型(数字、字符串、布尔、日期):
字符串
– 默认可以不使用双引号或单引号。
– 也可以使用单引号或者双引号:
单引号:会转义特殊字符,特殊字符最终只是一个普通的字符串数据(也就是将字符串原样打印)。
双引号;不会转义字符串里面的特殊字符;特殊字符会作为本身想表示的意思输出。
– 字符串可以写成多行,从第二行开始,必须有一个单空格缩进。换行符会被转为空格。
1 name: zhangsan 字符串
2 age: 22 数字
3 result: true 布尔
自定义yaml文件配置:
1.自定义JavaBean跟配置文件映射
1 /** 2 * @ConfigurationProperties(prefix = "student") 3 * 可使用在类和方法上,如果是方法方法的返回值必须是类类型。 4 * 让spring将配置文件中的数据映射到该类。 5 * prefix:告诉spring配置文件哪一个节点的数据要映射到该类。 6 * 注:要映射的类必须交给spring管理。该注解可以映射properties or yaml文件的数据。 7 */ 8 @Component 9 @ConfigurationProperties(prefix = "student") 10 public class Student { 11 12 //@Value("${student.name}"),也可以使用spring底层注解@Value进行数据注入,详情请看spring笔记。 13 private String name; 14 private Integer age; 15 private boolean result; 16 private Date date; 17 private Map<String,String> map; 18 private List<Object> list; 19 private Dog dog; 20 21 public String getName() { 22 return name; 23 } 24 25 public void setName(String name) { 26 this.name = name; 27 } 28 29 public Integer getAge() { 30 return age; 31 } 32 33 public void setAge(Integer age) { 34 this.age = age; 35 } 36 37 public boolean getResult() { 38 return result; 39 } 40 41 public void setResult(boolean result) { 42 this.result = result; 43 } 44 45 public Date getDate() { 46 return date; 47 } 48 49 public void setDate(Date date) { 50 this.date = date; 51 } 52 53 public Map<String, String> getMap() { 54 return map; 55 } 56 57 public void setMap(Map<String, String> map) { 58 this.map = map; 59 } 60 61 public List<Object> getList() { 62 return list; 63 } 64 65 public void setList(List<Object> list) { 66 this.list = list; 67 } 68 69 public Dog getDog() { 70 return dog; 71 } 72 73 public void setDog(Dog dog) { 74 this.dog = dog; 75 } 76 77 @Override 78 public String toString() { 79 return "Student{" + 80 "name=\'" + name + \'\\'\' + 81 ", age=" + age + 82 ", result=" + result + 83 ", date=" + date + 84 ", map=" + map + 85 ", list=" + list + 86 ", dog=" + dog + 87 \'}\'; 88 } 89 }
2.编写yaml配置文件
1 #自定义配置,根名称根据@ConfigurationProperties注解的prefix属性来指定。 2 student: 3 name: zhangsan 4 age: 22 5 result: true 6 date: 2018/2/9 7 map: {k1: v1,k2: v2} 8 list: [1,zhangsan,true,2,3] 9 dog: {name: 拉布拉多,age: 22}
3.导入springboot的配置文件处理器
作用:编写yaml文件时有智能提示,根据指定的前缀即可提示。
1 <dependency>
2 <groupId>org.springframework.boot</groupId>
3 <artifactId>spring-boot-configuration-processor</artifactId>
4 <optional>true</optional>
5 </dependency>
4.@ConfigurationProperties和@Value注解获取值比较
这两个注解不管是 yaml or properties 文件都可以映射到值。
注:不是要在springboot的配置文件里面写才能映射,自定义的配置文件只要是yaml或properties后缀的文件都可以映射。
如果是跟springboot配置无关的建议不要写在springboot配置文件里,建议单独写一个配置文件。
自定义的配置文件springboot是不会加载的,需要通过@PropertySource注解来指定。@PropertySource详情请看spring笔记
5.@ConfigurationProperties和@Value注解的应用场景
如果说,我们只是在某个业务逻辑中需要获取一下配置文件中的某个值,使用@Value。
如果说,我们专门编写了一个javaBean来和配置文件进行映射,我们就直接使用@ConfigurationProperties。
配置文件占位符:
1.随机数值
SpringBoot自带的一个RandomValuePropertySource类,在配置文件中是可以使用占位符来生成该类的一些随机数值的。
1 student.name=张三${random.uuid}
2 student.age=${random.int(10)}
3
4 //可用的随机数值如下
5 ${random.value}、${random.int}、${random.long}
6 ${random.int(10)}、${random.int[1024,65536]}、${random.uuid}
2.属性配置占位符
在SpringBoot配置文件中可以使用占位符来获取之前配置的数据的。如果占位符使用的属性不存在,会将占位符变成字符串赋值给属性,当然也可以使用默认值。如下student.map.k1=${student.hello:hello}如果student.hello不存在会使用默认值hello。
1 student.name=张三 2 student.map.k1=${student.hello:hello} 3 student.dog.name=${student.name}
主配置文件加载顺序:
Spring Boot启动时会扫描以下位置的application.properties或者application.yaml文件作为Spring Boot的默认主配置文件
– 项目:/config/
– 项目:/
– classpath:/config/
– classpath:/
– 以上是按照优先级从高到低的顺序,所有位置的文件都会被加载,高优先级配置会覆盖低优先级配置内容。
– 我们也可以通过配置spring.config.location来指定外部配置文件的位置。