基础2-application.properties或者application.yml
yml配置文件
Spring Boot使用一个全局的配置文件:application.properties或者application.yml,配置文件名字是固定的,不可随便修改;且配置文件存放路径也是固定的,要么在src/main/resources目录,要么在类路径/config下;
yml是YAML语言的文件,以数据为中心,比json、xml等更适合做配置文件;
全局配置文件可以对一些默认配置值进行修改;
YAML语法
YAML基本语法如下:
①使用缩进来表示层级关系;
②缩进时不允许使用Tab键,只允许使用空格;
③缩进的空格数量不重要,只要相同层级的元素左对齐即可;
④大小写敏感;
YAML支持3种数据结构:
①对象:键值对的集合,也可以用作Map;这里可以用行内写法{},来把属性和值放在一行内;
②数组:一组按次序排列的值,包括:Array,List,Set等;行内写法用[]来把元素放到一行内;其他写法则用- 元素的形式来表示,注意:-与元素之间必须存在空格;
③字面量:单个,不可分割的值;字符串默认不用加上单引号或者双引号;这里的单引号与双引号有特殊含义:双引号不会转义字符串里面的特殊字符,特殊字符会作为本身想表示的意思;而单引号会转义特殊字符,特殊字符最终作为一个普通的字符串输出;
例如:”\n”输出时会有换行效果;’\n’这里输出时为\n字符;
yml代码
package com.springboot.bean; public class Car { private String type; @Override public String toString() { return "Car{" + "type='" + type + '\'' + '}'; } public String getType() { return type; } public void setType(String type) { this.type = type; } } |
package com.springboot.bean; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; import java.util.Date; import java.util.List; import java.util.Map; /** * 2020 - 08 - 05 * 9:15 * @ConfigurationProperties * 只有Spring容器中的组件,才能使用该注解,因此,该注解必须注入到Spring容器中,否则会提示错误 * 该注解就是告诉Spring Boot,当前类的属性值与properties/yml文件中的属性值进行绑定 * prefix:可以理解为整个属性树的根节点,解析该根节点下所有的属性,来与java类的属性进行映射,这里是person * */ @Component @ConfigurationProperties(prefix = "person") public class Person { private String name; private String address; private int age; private String telphone; private Date birthday; private boolean isSingle; private Map<String,Object> map; private List<Object> list; private Car car; @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", address='" + address + '\'' + ", age=" + age + ", telphone='" + telphone + '\'' + ", birthday=" + birthday + ", isSingle=" + isSingle + ", map=" + map + ", list=" + list + ", car=" + car + '}'; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getTelphone() { return telphone; } public void setTelphone(String telphone) { this.telphone = telphone; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public boolean isSingle() { return isSingle; } public void setSingle(boolean single) { isSingle = single; } public Map<String, Object> getMap() { return map; } public void setMap(Map<String, Object> map) { this.map = map; } public List<Object> getList() { return list; } public void setList(List<Object> list) { this.list = list; } public Car getCar() { return car; } public void setCar(Car car) { this.car = car; } } |
person: name: LiSi address: 山东省潍坊寿光市 age: 30 telphone: 11111111111 isSingle: true birthday: 1989/02/01 map: {k1: v1,k2: v2,k3: v3} list: - a1 - a2 - a3 - a4 car: type: BMW |
package com.springboot; import com.springboot.bean.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.test.context.junit4.SpringRunner; import static org.junit.jupiter.api.Assertions.*; /** * 2020 - 08 - 05 * 9:53 */ @RunWith(SpringRunner.class) @SpringBootTest public class HelloWordMainApplicationTest { @Autowired Person person; @Test public void contextLoads(){ System.out.println("person = " + person); } } |
<?xml version="1.0" encoding="UTF-8"?> |
yml注意事项
①value与:之间必须有空格,即Key:空格value;
②JAVA类必须对属性生成setter/getter方法,否则属性值还是null;
③日期格式为:XX/XX/XX,是用/来分割,否则Date类型会报错;
④POM文件需要依赖spring-boot-configuration-processor;
⑤@ConfigurationProperties注解的类,必须添加到Spring容器中,否则编译报错;
@Value与@ConfigurationProperties比较
①@ConfigurationProperties可以一次性批量注入配置文件中的属性,而@Value只能一个个来指定;
②@ConfigurationProperties相对来说,比较宽松,例如:last-name与lastName在@ConfigurationProperties中是一样的,但@Value却不能;
③@ConfigurationProperties不支持SpEL,而@Value则支持SpEL;
④@ConfigurationProperties支持JSP303数据校验,可以直接使用@Validated注解,而@Value则不支持;
⑤@ConfigurationProperties支持复杂数据类型,例如:Map、List等,而@Value仅仅支持基本数据类型,不支持复杂数据类型;
总结:
如果单纯就是为了某个变量赋值,可以使用@Value;
如果有一个专门的JAVA Bean来接收properties里的属性,那还是建议使用@ConfigurationProperties注解;
properties文件代码
person.name=LiSi |
properties中文乱码
设置完之后,如果中文还是乱码,则重启IDEA下,然后把properties中的中文全部删掉,然后再重新写一遍就行了