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博客

相关推荐
刘一说5 小时前
深入理解 Spring Boot 嵌入式 Web 容器:从原理到性能调优
前端·spring boot·firefox
程序员小凯5 小时前
Spring Boot文件处理与存储详解
java·spring boot·后端
2501_938774297 小时前
Leaflet 弹出窗实现:Spring Boot 传递省级旅游口号信息的前端展示逻辑
前端·spring boot·旅游
ruleslol8 小时前
SpringBoot13-文件上传02-阿里云OSS
spring boot
蹦跑的蜗牛9 小时前
Spring Boot 使用 Redis 实现消息队列
spring boot·1024程序员节
兜兜风d'10 小时前
RabbitMQ事务机制详解
数据库·spring boot·分布式·rabbitmq·ruby·java-rabbitmq
勇往直前plus10 小时前
学习和掌握RabbitMQ及其与springboot的整合实践(篇二)
spring boot·学习·rabbitmq·java-rabbitmq
一枚码仔10 小时前
SpringBoot启动时执行自定义内容的5种方法
java·spring boot·后端
喜欢读源码的小白10 小时前
【Spring Boot + Spring Security】从入门到源码精通:藏经阁权限设计与过滤器链深度解析
java·开发语言·spring boot·spring security
摇滚侠10 小时前
Spring Boot3零基础教程,JVM 编译原理,笔记87
jvm·spring boot·笔记