Spring Boot 优雅配置yml配置文件定义集合、数组和Map

一、@value 获取配置文件

在平时的yml配置文件中,我们经常使用到配置基本数据类型的字符串,比如配置日志文件的写法如下:

yaml 复制代码
# 配置日志输出级别
logging:
  # 指定logback配置文件的位置 
  config: classpath:logback-spring.xml
  # 文件日志要输出的路径
  path: E:/logs/springboot_server
  # 日志的输出级别
  level:
    root: info

获取属性值的话可以通过@value 注解来实现,如下:

java 复制代码
@Value("${logging.path}")
private String path;    // 获取日志文件的输出路径

二、 List集合获取配置文件

第一种方法

yaml 复制代码
# 拦截器路径拦截或者不拦截配置
interceptorconfig:
  path:
    #该路径下任何类型请求均拦截
    include:
      - /api/v1/token/api_token
      - /api/v1/yibaotong/save

第二种方法

yaml 复制代码
# 拦截器路径拦截或者不拦截配置
interceptorconfig:
  path:
    #该路径下任何类型请求均拦截
    include: [/api/v1/token/api_token,/api/v1/yibaotong/save]

总结

这里需要注意的是,定义List集合不能用@value 注解来获取List集合的所有值,需要定义一个配置类bean,然后使用 @ConfigurationProperties 注解来获取list集合值,做法如下:

java 复制代码
@Data
@Component
@ConfigurationProperties(prefix = "interceptorconfig.path") // 配置文件的前缀
public class InterceptorPathBean
{
    /*
     * 需要拦截的路径
     */
    private List<String> include;
}

三、定义对象list集合获取配置文件

单个对象List

首先创建一个user对象如下:

java 复制代码
@Data
public class User implements Serializable
{  
        
    private static final long serialVersionUID = 1L;
    
    private String appId;
    
    private String password;
 
}

然后yml配置文件的写法如下:

yaml 复制代码
jwt:
  userlist:
    - appId: YiBaoTong
      password: 123456
    - appId: ZhiKe
      password: 123456

定义配置bean使用@ConfigurationProperties注解获取对象集合值:

java 复制代码
@Data
@Component
@ConfigurationProperties(prefix = "jwt") // 配置 文件的前缀
public class JwtConfigBean
{
    /**
     * 用户列表
     */
    private List<User> userlist;
}

List对象中包含List

定义配置bean使用@ConfigurationProperties注解获取对象集合值:

java 复制代码
@Data
@Component
@ConfigurationProperties(prefix = "jwt") // 配置 文件的前缀
public class JwtConfigBean {
    /**
     * 用户列表
     */
    private List<UserTest> userList;

    @Data
    private static class UserTest {
        private String appId;
        private List<String> passwordList;
    }


}

yml 文件配置

yaml 复制代码
jwt:
  userList:
    - appId: '121212'
      passwordList: '活动时间流程节点-PromoTimeValidNode,活动时间流程节点-PromoTimeValidNode2'
    - appId: 'werw3313'
      passwordList: '活动时间流程节点-PromoTimeValidNode,活动时间流程节点-PromoTimeValidNode2'

四、数组获取配置文件

yaml 格式如下:

yaml 复制代码
interceptorconfig:
  path:
    includes: /api/v1,/api/v2  #注意要用逗号分隔开

可以通过@value注解获取数组值,如下:

java 复制代码
@Value("${interceptorconfig.path.includes}")
private String[] includes;

也可以通过创建配置类bean,使用@ConfigurationProperties注解获取,如下:

java 复制代码
@Data
@Component
@ConfigurationProperties(prefix = "interceptorconfig.path") // 配置 文件的前缀
public class InterceptorPathBean
{  
    private String[] includes;
}

五、定义Map集合配置文件

yaml 格式如下:

yaml 复制代码
interceptorconfig:
  path:
    maps: {name: 小明,age: 24}

或者写成:

yaml 复制代码
interceptorconfig:
  path:
    maps:
      name: 小明
      age: 24

通过创建配置类bean,使用@ConfigurationProperties注解获取map值,如下:

java 复制代码
@Data
@Component
@ConfigurationProperties(prefix = "interceptorconfig.path") // 配置 文件的前缀
public class InterceptorPathBean
{
    private Map<String , String> maps;
}

以上就是Spring Boot yml配置文件定义基本数据类型和引用数据类型的方式;

六、参考链接

spring boot 的yml配置文件定义list集合、数组和map以及使用中出现的错误_yml配置map类型的数据注入失败-CSDN博客

相关推荐
evans在进步12 小时前
Spring Boot 核心机制详解:可执行 JAR、CORS、静态资源与配置绑定
spring boot·后端·jar
Sayuanni%312 小时前
SpringBoot 从注解到源码:核心知识点总结
java·spring boot·后端
m0_5873830014 小时前
社区家政系统开发实战:从需求分析到上线部署全指南
java·spring boot·spring·需求分析
凤山老林14 小时前
动态 i18n 体系落地:Spring Boot 多租户热加载与前后端协同实践
java·spring boot·后端·i18n
【赫兹威客】浩哥14 小时前
基于SpringBoot+Vue3的健身运动管理系统|WebSocket实时消息 健身房毕设项目
spring boot·websocket·课程设计
凤山老林14 小时前
数据库读写分离与动态路由实战:Spring Boot + ShardingSphere-JDBC 生产配置
数据库·spring boot·后端·分库分表·sharding-jdbc
paopaokaka_luck16 小时前
基于springboot3+vue3的支教志愿者管理系统(AI问答、协同过滤算法、Echarts图形化分析)
spring boot·echarts
2601_9638701818 小时前
【计算机毕业设计】基于Vue+Spring Boot的助农电商系统的设计与实现
spring boot·后端·课程设计
野生技术架构师18 小时前
Spring Boot 实现数据脱敏:自定义注解 + Jackson 序列化器
java·spring boot·后端
冻柠檬飞冰走茶19 小时前
《数据结构实验指导-C++语言版》 在顺序表 list 中查找元素 x
开发语言·数据结构·c++·算法·list