相关推荐recommended
springboot yml文件自定义配置的读取、使用 字符串 数组 对象 集合
作者:mmseoamin日期:2023-12-25

.yml 配置

# 自定义开发环境端口 默认本地 9801
# serverPort: 9801
spring:
  profiles: dev
# 环境的配置
server:
  port: 9801
# 自定义对象/配置
student:
  name: 李四
  age: 25
  jobs: #数组方式1                                      @ConfigurationProperties 方式可以获取; @Value方式、Environment方式获取不到
    - java
      linux
      123
  loves: [看电影,玩游戏,游泳,aaaa,456] # 数组方式2        @ConfigurationProperties 方式可以获取; @Value方式、Environment方式获取不到
  books: 水浒,西游,红楼梦,三国 # 数组方式3                @所有方式都可获取
  frands: # List方式1                                  @ConfigurationProperties 方式可以获取; @Value方式、Environment方式获取不到
    - 李雷
      韩梅梅
      本山
  familys: [李父,李牧,李姐,李弟,456] # List方式2         @ConfigurationProperties 方式可以获取; @Value方式、Environment方式获取不到
  organ: 眼,#耳,.#口,鼻 # List方式3                        @所有方式都可获取
# 对象集合                                              @ConfigurationProperties 方式可以获取并封装成对象集合
users:
  user:
    - name: tom
      age: 18
    - name: 韩梅梅
      age: 15

controller代码

import com.tencent.gateway.entity.Student;
import com.tencent.gateway.entity.User;
import com.tencent.gateway.entity.Users;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.core.env.Environment;
import org.springframework.beans.factory.annotation.Value;
import java.util.Arrays;
import java.util.List;
@RestController
@RequestMapping("/test")
public class TestController {
    //方式1:对象中使用 @ConfigurationProperties 加载配置到对象属性中
    @Autowired
    private Student student;//配置文件对应的对象
    @Autowired
    private Users users;//配置文件对应的对象集合
    @GetMapping("/test1")
    public Object test1(){
        System.out.println("方式1:@ConfigurationProperties 获取配置:");
        System.out.println();
        System.out.println(student.name);
        System.out.println(student.age);
        System.out.println(Arrays.toString(student.jobs));
        System.out.println(Arrays.toString(student.loves));
        System.out.println(Arrays.toString(student.books));
        System.out.println(student.frands.toString());
        System.out.println(student.familys.toString());
        System.out.println(student.organ.toString());
        System.out.println();
        System.out.println("usersList:");
        for (User user: users.user){
            System.out.println(user.name);
            System.out.println(user.age);
        }
        return "success";
    }
    //方式2:使用 Environment
    @Autowired
    Environment env;
    @GetMapping("/test2")
    public Object test2(){
        System.out.println("方式2:Environment 获取配置:");
        System.out.println();
        System.out.println(env.getProperty("server.port"));
        System.out.println(env.getProperty("student.name"));
        System.out.println(env.getProperty("student.age"));
        System.out.println(env.getProperty("student.jobs"));    //获取不到
        System.out.println(env.getProperty("student.loves"));   //获取不到
        System.out.println(env.getProperty("student.books"));   //          可以获取
        System.out.println(env.getProperty("student.frands"));  //获取不到
        System.out.println(env.getProperty("student.familys")); //获取不到
        System.out.println(env.getProperty("student.organ"));   //          可以获取
        return "success";
    }
    //方式3:使用 @Value 映射 获取指定配置参数值, 冒号后的1111为默认值
    @Value("${server.port:1111}")
    private String port;
    @Value("${student.name}")
    public String name;
    @Value("${student.age}")
    private int age;
    @Value("${student.jobs:}")
    private String[] jobs;
    @Value("${student.loves:}")
    private String[] loves;
    @Value("${student.books:}")
    private String[] books;
    @Value("${student.frands:a,a,a,a}")//冒号后指定默认值
    private List frands;
    @Value("${student.familys:}")
    private List familys;
    @Value("#{'${student.organ}'.split(',')}") //使用指定分割符分割字符串数组(部分字符不支持,比如 .)
    private List organ;
    @Value("#{'${student.organ}'.split('#')}") //使用指定分割符分割字符串数组(部分字符不支持,比如 .)
    private List organ2;
    @GetMapping("/test3")
    public Object test3(){
        System.out.println("方式3:@Value 映射 获取配置:");
        System.out.println();
        System.out.println(port);
        System.out.println(name);
        System.out.println(age);
        System.out.println(Arrays.toString(jobs));  //获取不到
        System.out.println(Arrays.toString(loves)); //获取不到
        System.out.println(Arrays.toString(books)); //          可以获取
        System.out.println(frands.toString());      //获取不到
        System.out.println(familys.toString());     //获取不到
        System.out.println();
        System.out.println("organ:(逗号分割)");
        System.out.println(organ.toString());       //          可以获取
        for (String organSplit: organ){
            System.out.println(organSplit);
        }
        System.out.println();
        System.out.println("organ2:(号分割)");
        System.out.println(organ2.toString());      //          可以获取
        for (String organSplit: organ2){
            System.out.println(organSplit);
        }
        return "success";
    }
}

实体类

Student.java

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
@ConfigurationProperties(prefix = "student")
public class Student {
    public String name;
    public int age;
    public String[] jobs;
    public String[] loves;
    public String[] books;
    public List frands;
    public List familys;
    public List organ;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String[] getJobs() {
        return jobs;
    }
    public void setJobs(String[] jobs) {
        this.jobs = jobs;
    }
    public String[] getLoves() {
        return loves;
    }
    public void setLoves(String[] loves) {
        this.loves = loves;
    }
    public List getFrands() {
        return frands;
    }
    public void setFrands(List frands) {
        this.frands = frands;
    }
    public List getFamilys() {
        return familys;
    }
    public void setFamilys(List familys) {
        this.familys = familys;
    }
    public String[] getBooks() {
        return books;
    }
    public void setBooks(String[] books) {
        this.books = books;
    }
    public List getOrgan() {
        return organ;
    }
    public void setOrgan(List organ) {
        this.organ = organ;
    }
}

Users.java

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
@ConfigurationProperties(prefix = "users")
public class Users {
    public List user; //自动将users.user下的所有数组封装到User对象集合中
    public List getUser() {
        return user;
    }
    public void setUser(List user) {
        this.user = user;
    }
}

User.java

public class User {
    public String name;
    public int age;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
}

方式一、

http://localhost:9801/test/test1

springboot yml文件自定义配置的读取、使用 字符串 数组 对象 集合,第1张

方式二、

http://localhost:9801/test/test2

springboot yml文件自定义配置的读取、使用 字符串 数组 对象 集合,第2张

方式三、

http://localhost:9801/test/test3

springboot yml文件自定义配置的读取、使用 字符串 数组 对象 集合,第3张

参考:

SpringBoot:yml文件详解-CSDN博客

https://www.jb51.net/article/216769.htm