.properties相对常用,主要学习.yaml用法
补丁
在pom.xml加入补丁,可以使配置文件时候出现自动提示
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
基本语法学习
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]
测试
package com.example.demo.pojo;
import java.util.Arrays;
public class Child {
String name;
String[] hobby;
@Override
public String toString() {
return "Child{" +
"name='" + name + '\'' +
", hobby=" + Arrays.toString(hobby) +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String[] getHobby() {
return hobby;
}
public void setHobby(String[] hobby) {
this.hobby = hobby;
}
}
package com.example.demo.pojo;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import java.util.Map;
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
String name;
int age;
Child child;
Map<Integer,String> testMap;
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", child=" + child.toString() +
", testMap=" + testMap +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Child getChild() {
return child;
}
public void setChild(Child child) {
this.child = child;
}
public Map<Integer, String> getTestMap() {
return testMap;
}
public void setTestMap(Map<Integer, String> testMap) {
this.testMap = testMap;
}
}
server:
port: 8082
person:
age: 18
name: testName
testMap: {11: testMap1,22: testMap2}
child:
name: testChiled
hobby: [ball,food,sleep]
用测试类运行结果
package com.example.demo;
import com.example.demo.pojo.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;
@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {
@Autowired
Person person;
@Test
public void contextLoads() {
System.out.println(person);
}
}
占位符
随机数,引用符
server:
port: 8082
person:
age: ${random.int[10,18]}
name: ${random.uuid}
testMap: {11: testMap1,22: testMap2}
child:
name: ${person.name}_child
hobby: [ball,food,sleep]
指定默认值
配置文件出现实体类不存在属性时候,可以指定默认值
server:
port: 8082
person:
age: ${random.int[10,18]}
name: ${random.uuid}
testMap: {11: testMap1,22: testMap2}
child:
name: ${child.nickName:testNickName}
hobby: [ball,food,sleep]