yaml在线转json:http://www.bejson.com/validators/yaml/
参考教程:https://www.runoob.com/w3cnote/yaml-intro.html
Person:
id: 1
name: Lisa
teacher:
name: Mr.Lee
age: 40
对应json:
{
Person: {
id: 1,
name: 'Lisa',
teacher: {
name: 'Mr.Lee',
age: 40
}
}
}
另一种写法:
Person: {name: Lisa,age: 18}
对应的json
{
Person: {
name: 'Lisa',
age: 18
}
}
简单数组:
students:
- stu1
- stu2
- stu3
teachers: [tea1,tea2,tea3]
对应json
{
students: [
'stu1',
'stu2',
'stu3'
],
teachers: [
'tea1',
'tea2',
'tea3'
]
}
对象数组:
Teacher:
- id: 1
name: stu1
age: 18
- id: 2
name: stu2
age: 19
对应json
{
Teacher: [
{
id: 1,
name: 'stu1',
age: 18
},
{
id: 2,
name: 'stu2',
age: 19
}
]
}
application.yml
文件给SpringBoot属性赋值其中lombok用于简化pojo实体类的编写,spring-boot-starter-test用于测试
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.2.3.RELEASEversion>
parent>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
dependency>
dependencies>
person:
id: 1
name: Lisa
happy: true
weight: 93.2
info: {school: qust,grade: 100}
students:
- id: 100
name: LiJing
- id: 200
name: XiaoMing
注意,要将其注入的Spring的容器中
package com.qianyu.pojo;
import lombok.*;
import org.springframework.stereotype.*;
@Data
@Component
public class Student {
private Integer id;
private String name;
}
package com.qianyu.pojo;
import lombok.*;
import org.springframework.boot.context.properties.*;
import org.springframework.stereotype.*;
import java.util.*;
@Data
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
private Integer id;
private String name;
private boolean happy;
private Double weight;
private Map<String,Object> info;
private List<Student> students;
}
@ConfigurationProperties注解可以将配置文件中的每一个属性的值,映射到指定组件中。即告诉SpringBoot将指定类中所有属性和配置文件中的属性进行绑定
主类:
package com.qianyu;
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
测试类:
package com.qianyu;
import com.qianyu.pojo.*;
import org.junit.*;
import org.junit.runner.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.boot.test.context.*;
import org.springframework.test.context.junit4.*;
@RunWith(SpringRunner.class)
@SpringBootTest
public class ApplicationTest {
@Autowired
private Person person;
@Test
public void test() {
System.out.println(person);
}
}
运行结果:
Person(id=1, name=Lisa, happy=true, weight=93.2, info={school=qust, grade=100}, students=[Student(id=100, name=LiJing), Student(id=200, name=XiaoMing)])
说明属性注入成功!
*.properties
文件配置属性
如果要使用*.properties
类型文件注入的话,需要在类上添加@PropertySource(value="classpath:XXX.properties")
指定配置文件路径,还要在类的属性上使用@Value()
注解才能注入
不同位置application.yml文件的优先级
SpringBoot多环境
实现方式一:
application-dev.yml
、application-pro.yml
两个文件,分别表示开发环境和测试环境application.yml
中指定环境spring:
profiles:
active: dev
实现方式二:
将所有环境写在同一个application.yml
中使用---
分割
spring:
profiles:
active: dev
---
spring:
profiles: dev
server:
port: 8081
---
spring:
profiles: pro
server:
port: 8082