SpringBoot读取配置文件

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": "男"
      }
    ]
  }
}
相关推荐
信徒_1 分钟前
常用设计模式
java·单例模式·设计模式
神仙别闹7 分钟前
基于C#实现的(WinForm)模拟操作系统文件管理系统
java·git·ffmpeg
小爬虫程序猿7 分钟前
利用Java爬虫速卖通按关键字搜索AliExpress商品
java·开发语言·爬虫
m0_7482567812 分钟前
SpringBoot 依赖之Spring Web
前端·spring boot·spring
组合缺一13 分钟前
Solon v3.0.5 发布!(Spring 可以退休了吗?)
java·后端·spring·solon
程序猿零零漆15 分钟前
SpringCloud 系列教程:微服务的未来(二)Mybatis-Plus的条件构造器、自定义SQL、Service接口基本用法
java·spring cloud·mybatis-plus
猿来入此小猿17 分钟前
基于SpringBoot在线音乐系统平台功能实现十二
java·spring boot·后端·毕业设计·音乐系统·音乐平台·毕业源码
愤怒的代码30 分钟前
Spring Boot对访问密钥加解密——HMAC-SHA256
java·spring boot·后端
带多刺的玫瑰31 分钟前
Leecode刷题C语言之切蛋糕的最小总开销①
java·数据结构·算法
栗豆包1 小时前
w118共享汽车管理系统
java·spring boot·后端·spring·tomcat·maven