https://www.jianshu.com/p/97222440cd08
配置文件一般使用
导入maven
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
application.yaml
一、基础语法
1、存普通key-vale
name:wt
2、存对象
person:
name:wt
age:10
行内写法
student:{name:wt, age:10}
3、数组
pets:
- cat
- dpg
- pig
行内写法
pet:{cat, dog, pig}
二、使用yaml作为配置文件
实体类
package com.wt.boot.pojo; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component @ConfigurationProperties(prefix = "dog") public class Dog { private String name; private int age; public Dog() { } public Dog(String name, int age) { this.name = name; this.age = age; } 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; } @Override public String toString() { return "Dog{" + "name=‘" + name + ‘\‘‘ + ", age=" + age + ‘}‘; } }
application.yaml
server: port: 8083 dog: name: 二哈 age: 10
测试类
package com.wt.boot; import com.wt.boot.pojo.Dog; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest class BootApplicationTests { @Autowired private Dog dog; @Test void contextLoads() { System.out.println(dog.toString()); } }
三、特殊语法
${random.uuid}
四、松散绑定
userName user-name
五、JSR303
参考网址:
http://www.mamicode.com/info-detail-2905245.html1
@Validated 类的注解
@Email 属性的注解
六、config优先级
原文:https://www.cnblogs.com/wt7018/p/13346187.html