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": "男"
      }
    ]
  }
}
相关推荐
成富6 分钟前
文本转SQL(Text-to-SQL),场景介绍与 Spring AI 实现
数据库·人工智能·sql·spring·oracle
计算机学长felix10 分钟前
基于SpringBoot的“校园交友网站”的设计与实现(源码+数据库+文档+PPT)
数据库·spring boot·毕业设计·交友
Re.不晚13 分钟前
Java入门15——抽象类
java·开发语言·学习·算法·intellij-idea
雷神乐乐19 分钟前
Maven学习——创建Maven的Java和Web工程,并运行在Tomcat上
java·maven
码农派大星。23 分钟前
Spring Boot 配置文件
java·spring boot·后端
顾北川_野30 分钟前
Android 手机设备的OEM-unlock解锁 和 adb push文件
android·java
江深竹静,一苇以航32 分钟前
springboot3项目整合Mybatis-plus启动项目报错:Invalid bean definition with name ‘xxxMapper‘
java·spring boot
confiself1 小时前
大模型系列——LLAMA-O1 复刻代码解读
java·开发语言
Wlq04151 小时前
J2EE平台
java·java-ee
XiaoLeisj1 小时前
【JavaEE初阶 — 多线程】Thread类的方法&线程生命周期
java·开发语言·java-ee