一.配置文件
SpringBoot可以使用两种类型的配置文件(文件名固定):
application.properties
application.yml
配置文件的作用就是来修改SpringBoot自动配置的默认值:SpringBoot在底层都给我们配置好了所有的配置信息
yml:YAML(YAML Ain't a Markup Language):新型配置文件.以前的配置文件大都使用到的是"xxx.xml"文件,YML以数据为中心,更适合做配置文件;
二.YML语法
1.基本语法
k:(空格)v --表示一对键值对(空格必须有);
以空格的缩进来控制层级关系: 只要是左对齐的一列数据,都是同一层级的,且属性和值都是大小写敏感的
2.值的写法
字面量: 普通的值(数字,字符串,布尔): key: value,字符串不用加引号:
""字符串:不会转义字符串中的特殊字符,特殊字符会作为本身想要表示的意思
"zhangsan \n lisi" 输出: zhangsan 换行 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.配置自定义类
1 package com.skykuqi.springboot.helloworld.entity; 2 3 import org.springframework.boot.context.properties.ConfigurationProperties; 4 import org.springframework.stereotype.Component; 5 6 import java.util.Date; 7 import java.util.List; 8 import java.util.Map; 9 10 /** 11 * @author : S K Y 12 * @version :0.0.1 13 */ 14 /*将yml配置文件中配置的每一个属性的值映射到属性文件中*/ 15 @Component 16 @ConfigurationProperties(prefix = "person") 17 public class Person { 18 private String lastName; 19 private Integer age; 20 private Boolean boss; 21 private Date birth; 22 23 private Map<String, Object> maps; 24 private List<Object> lists; 25 private Dog dog; 26 27 public String getLastName() { 28 return lastName; 29 } 30 31 public void setLastName(String lastName) { 32 this.lastName = lastName; 33 } 34 35 public Integer getAge() { 36 return age; 37 } 38 39 public void setAge(Integer age) { 40 this.age = age; 41 } 42 43 public Boolean getBoss() { 44 return boss; 45 } 46 47 public void setBoss(Boolean boss) { 48 this.boss = boss; 49 } 50 51 public Date getBirth() { 52 return birth; 53 } 54 55 public void setBirth(Date birth) { 56 this.birth = birth; 57 } 58 59 public Map<String, Object> getMaps() { 60 return maps; 61 } 62 63 public void setMaps(Map<String, Object> maps) { 64 this.maps = maps; 65 } 66 67 public List<Object> getLists() { 68 return lists; 69 } 70 71 public void setLists(List<Object> lists) { 72 this.lists = lists; 73 } 74 75 public Dog getDog() { 76 return dog; 77 } 78 79 public void setDog(Dog dog) { 80 this.dog = dog; 81 } 82 83 @Override 84 public String toString() { 85 return "Person{" + 86 "lastName='" + lastName + '\'' + 87 ", age=" + age + 88 ", boss=" + boss + 89 ", birth=" + birth + 90 ", maps=" + maps + 91 ", lists=" + lists + 92 ", dog=" + dog + 93 '}'; 94 } 95 }
1 package com.skykuqi.springboot.helloworld.entity; 2 3 import org.springframework.boot.context.properties.ConfigurationProperties; 4 import org.springframework.boot.context.properties.NestedConfigurationProperty; 5 import org.springframework.stereotype.Component; 6 7 /** 8 * @author : S K Y 9 * @version :0.0.1 10 */ 11 @Component 12 @ConfigurationProperties(prefix = "dog") 13 public class Dog { 14 private String name; 15 private Integer age; 16 17 public String getName() { 18 return name; 19 } 20 21 public void setName(String name) { 22 this.name = name; 23 } 24 25 public Integer getAge() { 26 return age; 27 } 28 29 public void setAge(Integer age) { 30 this.age = age; 31 } 32 33 @Override 34 public String toString() { 35 return "Dog{" + 36 "name='" + name + '\'' + 37 ", age=" + age + 38 '}'; 39 } 40 }
1 person: 2 lastName: zhangsan 3 age: 22 4 boss: true 5 birth: 2019/11/25 6 maps: {k1: 12,k2: 21} 7 lists: 8 - lisi 9 - zhaoliu 10 - wnangwu 11 dog: 12 name: 小狗 13 age: 3
1 package com.skykuqi.springboot.helloworld; 2 3 import com.skykuqi.springboot.helloworld.entity.Person; 4 import javafx.application.Application; 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.test.context.junit4.SpringJUnit4ClassRunner; 10 import org.springframework.test.context.junit4.SpringRunner; 11 12 /** 13 * @author : S K Y 14 * @version :0.0.1 15 * SpringBoot单元测试 16 * 可以在测试期间类似编码一样进行自动注入到容器的功能,目录结构必须和原路径相同 17 */ 18 @RunWith(SpringRunner.class) 19 @SpringBootTest 20 public class HelloWorldMainApplicationTests { 21 @Autowired 22 Person person; 23 24 @Test 25 public void testPerson() { 26 System.out.println(person); 27 } 28 }
1 <!--配置文件的处理器,编写自定义的属性则会出现提示--> 2 <dependency> 3 <groupId>org.springframework.boot</groupId> 4 <artifactId>spring-boot-configuration-processor</artifactId> 5 <optional>true</optional> 6 </dependency>
4.除了使用@ConfigurationProperties注解,我们还可以使用@Value注解来完成
1 package com.skykuqi.springboot.helloworld.entity; 2 3 import org.springframework.beans.factory.annotation.Value; 4 import org.springframework.boot.context.properties.ConfigurationProperties; 5 import org.springframework.stereotype.Component; 6 7 import java.util.Date; 8 import java.util.List; 9 import java.util.Map; 10 11 /** 12 * @author : S K Y 13 * @version :0.0.1 14 */ 15 /*将yml配置文件中配置的每一个属性的值映射到属性文件中*/ 16 @Component 17 //@ConfigurationProperties(prefix = "person") 18 public class Person { 19 @Value("${person.lastName}") 20 private String lastName; 21 @Value("#{11*2}") 22 private Integer age; 23 @Value("false") 24 private Boolean boss; 25 private Date birth; 26 27 private Map<String, Object> maps; 28 private List<Object> lists; 29 private Dog dog; 30 31 public String getLastName() { 32 return lastName; 33 } 34 35 public void setLastName(String lastName) { 36 this.lastName = lastName; 37 } 38 39 public Integer getAge() { 40 return age; 41 } 42 43 public void setAge(Integer age) { 44 this.age = age; 45 } 46 47 public Boolean getBoss() { 48 return boss; 49 } 50 51 public void setBoss(Boolean boss) { 52 this.boss = boss; 53 } 54 55 public Date getBirth() { 56 return birth; 57 } 58 59 public void setBirth(Date birth) { 60 this.birth = birth; 61 } 62 63 public Map<String, Object> getMaps() { 64 return maps; 65 } 66 67 public void setMaps(Map<String, Object> maps) { 68 this.maps = maps; 69 } 70 71 public List<Object> getLists() { 72 return lists; 73 } 74 75 public void setLists(List<Object> lists) { 76 this.lists = lists; 77 } 78 79 public Dog getDog() { 80 return dog; 81 } 82 83 public void setDog(Dog dog) { 84 this.dog = dog; 85 } 86 87 @Override 88 public String toString() { 89 return "Person{" + 90 "lastName='" + lastName + '\'' + 91 ", age=" + age + 92 ", boss=" + boss + 93 ", birth=" + birth + 94 ", maps=" + maps + 95 ", lists=" + lists + 96 ", dog=" + dog + 97 '}'; 98 } 99 }
5.@Value和@ConfigurationProperties获取值比较
功能上:@ConfigurationProperties可以批量注入配置文件中的属性,@Value必须一个一个指定
松散语法:@ConfigurationProperties支持松散绑定lastName last-name last_name 等都可以绑定到当前的属性
Spel(Spring 表达式): @Value支持Spring表达式
JSR303校验:@ConfigurationProperties支持JSR303数据校验
复杂类型封装(map):@ConfigurationProperties之处,@Value不支持
6.如果说我们只是在某个业务逻辑中只是需要获取一下配置文件中的某项值,那么我们只需要使用@Value:
1 @Value("${person.lastName}") 2 private String name; 3 4 @RequestMapping("/helloWithName") 5 public String helloWithName() { 6 return "Hello " + name; 7 }
--如果专门配置了一个JavaBean来和配置文件进行映射,我们就直接使用@ConfigurationProperties