SpringBoot读取配置文件

1.读取的方式

以读取单个属性为列

1.1.@Component+@Value

application.yaml内容如下:

student:
  age: 20

配置类

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;
}

测试类

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);
    }
}

测试结果如下

{
  "code": 0,
  "msg": "success",
  "data": 20
}

1.2.@ConfigurationProperties+@Component

application.yaml内容如下:

student:
  name: 张三

配置类

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;
}

测试类

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);
    }
}

测试结果如下

{
  "code": 0,
  "msg": "success",
  "data": "张三"
}

1.3.@ConfigurationProperties+@EnableConfigurationProperties

application.yaml内容如下:

people:
  sex:

配置类

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "people")
@Data
public class Test3 {

    private String sex;
}

测试类

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);
    }
}

测试结果如下

{
  "code": 0,
  "msg": "success",
  "data": "男"
}

@EnableConfigurationProperties的作用:让使用@ConfigurationProperties注解的类生效,即如果一个配置类只配置@ConfigurationProperties注解,而没有使用@Component修饰,那么在IOC容器中是获取不到properties 配置文件转化的bean,而@EnableConfigurationProperties 相当于把使用 @ConfigurationProperties` 的类进行了一次注入。

2.读取的数据类型

2.1.读取单个属性

@ConfigurationProperties+@Component方式测试

application.yaml内容如下:

student:
  name: 张三

配置类

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;
}

测试类

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);
    }
}

测试结果如下

{
  "code": 0,
  "msg": "success",
  "data": "张三"
}

2.2.读取对象

方式1:使用类接收

@ConfigurationProperties+@Component方式测试

application.yaml内容如下:

student:
  age: 20
  name: 张三
  sex:

配置类

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;
}

测试类

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);
    }
}

测试结果如下

{
  "code": 0,
  "msg": "success",
  "data": {
    "age": 20,
    "name": "张三",
    "sex": "男"
  }
}

2.3.读取单个属性集合

@ConfigurationProperties+@Component方式测试

application.yaml内容如下:

student:
  name:
    - "张三"
    - "李四"
    - "王五"

配置类

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;
}

测试类

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);
    }
}

测试结果如下

{
  "code": 0,
  "msg": "success",
  "data": {
    "name": [
      "张三",
      "李四",
      "王五"
    ]
  }
}

2.4.读取对象集合

@ConfigurationProperties+@Component方式测试

application.yaml内容如下:

student:
  list:
    - name: "张三"
      age: 20
      sex:- name: "李四"
      age: 21
      sex:

配置类

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;
    }
}

测试类

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);
    }
}

测试结果如下

{
  "code": 0,
  "msg": "success",
  "data": {
    "list": [
      {
        "name": "张三",
        "age": 20,
        "sex": "女"
      },
      {
        "name": "李四",
        "age": 21,
        "sex": "男"
      }
    ]
  }
}
  • 25
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值