1.读取的方式
以读取单个属性为列
1.1.@Component+@Value
application.yaml
内容如下:
yaml
student:
age: 20
配置类
java
package com.netvine.robotdrs.test;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
//不需要set方法
@Getter
//交给spring容器
@Component
public class Test1 {
/**
* 读取属性
*/
@Value("${student.age:0}")
private Integer age;
}
测试类
java
import com.netvine.robotdrs.test.Test1;
import com.netvine.robotdrs.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@Api(tags = "yaml文件读取控制层")
@RequestMapping("/yaml/")
@RequiredArgsConstructor
public class YamlController {
private final Test1 test1;
@GetMapping("test1")
@ApiOperation("测试1")
public Result test1(){
Integer age = test1.getAge();
return new Result().ok(age);
}
}
测试结果如下
json
{
"code": 0,
"msg": "success",
"data": 20
}
1.2.@ConfigurationProperties+@Component
application.yaml
内容如下:
yaml
student:
name: 张三
配置类
java
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@ConfigurationProperties(prefix = "student" )
@Component
@Data
public class Test2 {
private String name;
}
测试类
java
import com.netvine.robotdrs.test.Test2;
import com.netvine.robotdrs.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@Api(tags = "yaml文件读取控制层")
@RequestMapping("/yaml/")
@RequiredArgsConstructor
public class YamlController {
private final Test2 test2;
@GetMapping("test2")
@ApiOperation("测试2")
public Result test2(){
String name = test2.getName();
return new Result().ok(name);
}
}
测试结果如下
json
{
"code": 0,
"msg": "success",
"data": "张三"
}
1.3.@ConfigurationProperties+@EnableConfigurationProperties
application.yaml
内容如下:
yaml
people:
sex: 男
配置类
java
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "people")
@Data
public class Test3 {
private String sex;
}
测试类
java
import com.netvine.robotdrs.test.Test3;
import com.netvine.robotdrs.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@Api(tags = "yaml文件读取控制层")
@RequestMapping("/yaml/")
@RequiredArgsConstructor
@EnableConfigurationProperties(Test3.class)
public class YamlController {
private final Test3 test3;
@GetMapping("test3")
@ApiOperation("测试3")
public Result test3(){
String sex = test3.getSex();
return new Result().ok(sex);
}
}
测试结果如下
json
{
"code": 0,
"msg": "success",
"data": "男"
}
@EnableConfigurationProperties的作用:让使用
@ConfigurationProperties注解的类生效,即如果一个配置类只配置
@ConfigurationProperties注解,而没有使用
@Component修饰,那么在IOC容器中是获取不到properties 配置文件转化的bean,而
@EnableConfigurationProperties相当于把使用
@ConfigurationProperties` 的类进行了一次注入。
2.读取的数据类型
2.1.读取单个属性
以@ConfigurationProperties
+@Component
方式测试
application.yaml
内容如下:
yaml
student:
name: 张三
配置类
java
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@ConfigurationProperties(prefix = "student" )
@Component
@Data
public class Test2 {
private String name;
}
测试类
java
import com.netvine.robotdrs.test.Test2;
import com.netvine.robotdrs.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@Api(tags = "yaml文件读取控制层")
@RequestMapping("/yaml/")
@RequiredArgsConstructor
public class YamlController {
private final Test2 test2;
@GetMapping("test2")
@ApiOperation("测试2")
public Result test2(){
String name = test2.getName();
return new Result().ok(name);
}
}
测试结果如下
json
{
"code": 0,
"msg": "success",
"data": "张三"
}
2.2.读取对象
方式1:使用类接收
以@ConfigurationProperties
+@Component
方式测试
application.yaml
内容如下:
yaml
student:
age: 20
name: 张三
sex: 男
配置类
java
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties("student")
@Data
public class Student {
private Integer age;
private String name;
private String sex;
}
测试类
java
import com.netvine.robotdrs.test.Student;
import com.netvine.robotdrs.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@Api(tags = "yaml文件读取控制层")
@RequestMapping("/yaml/")
@RequiredArgsConstructor
public class YamlController {
private final Student student;
@GetMapping("test3")
@ApiOperation("测试3")
public Result test3(){
return new Result().ok(student);
}
}
测试结果如下
json
{
"code": 0,
"msg": "success",
"data": {
"age": 20,
"name": "张三",
"sex": "男"
}
}
2.3.读取单个属性集合
以@ConfigurationProperties
+@Component
方式测试
application.yaml
内容如下:
yaml
student:
name:
- "张三"
- "李四"
- "王五"
配置类
java
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
@ConfigurationProperties("student")
@Data
public class Student {
private List<String> name;
}
测试类
java
import com.netvine.robotdrs.test.Student;
import com.netvine.robotdrs.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@Api(tags = "yaml文件读取控制层")
@RequestMapping("/yaml/")
@RequiredArgsConstructor
public class YamlController {
private final Student student;
@GetMapping("test3")
@ApiOperation("测试3")
public Result test3(){
return new Result().ok(student);
}
}
测试结果如下
java
{
"code": 0,
"msg": "success",
"data": {
"name": [
"张三",
"李四",
"王五"
]
}
}
2.4.读取对象集合
以@ConfigurationProperties
+@Component
方式测试
application.yaml
内容如下:
yaml
student:
list:
- name: "张三"
age: 20
sex: 女
- name: "李四"
age: 21
sex: 男
配置类
java
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
@ConfigurationProperties("student")
@Data
public class ClassInfo {
private List<Student> list;
@Data
public static class Student{
private String name;
private Integer age;
private String sex;
}
}
测试类
java
import com.netvine.robotdrs.test.ClassInfo;
import com.netvine.robotdrs.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@Api(tags = "yaml文件读取控制层")
@RequestMapping("/yaml/")
@RequiredArgsConstructor
public class YamlController {
private final ClassInfo classInfo;
@GetMapping("test3")
@ApiOperation("测试3")
public Result test3(){
return new Result().ok(classInfo);
}
}
测试结果如下
java
{
"code": 0,
"msg": "success",
"data": {
"list": [
{
"name": "张三",
"age": 20,
"sex": "女"
},
{
"name": "李四",
"age": 21,
"sex": "男"
}
]
}
}