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

相关推荐
卓怡学长7 小时前
w176基于SpringBoot的医院管理系统
java·spring boot·spring·maven·intellij-idea
摇滚侠10 小时前
《SpringBoot 3:入门与应用实战》第 7 章 AOP 思想与实现 阅读笔记 15
java·spring boot·笔记
摇滚侠11 小时前
《SpringBoot 3:入门与应用实战》第 7 章 AOP 思想与实现 阅读笔记 14
spring boot·笔记·后端
脉动数据行情111 小时前
Java SpringBoot 对接知名台股 TWSE|批量采集上市股票行情实践
java·开发语言·spring boot
凤山老林13 小时前
Spring Boot 集成 ShardingSphere-Encrypt 实现字段级实时脱敏
java·spring boot·后端·数据脱敏
摇滚侠14 小时前
《SpringBoot 3:入门与应用实战》第 8 章 AOP 的进阶机制和应用 阅读笔记 16
java·spring boot·笔记
QQ_216962909614 小时前
【源码编号:project86570】SpringBoot电影院在线选座售票系统:电影排片、在线选座、订单购票、后台管理完整实战
java·spring boot·后端
谢亮_vipxieliang15 小时前
手机号验证技术方案:号段规则与正则表达式
java·服务器·spring boot·正则表达式·hibernate
爱码猿15 小时前
SpringAop通过el表达式动态获取方法入参的值
spring boot·spring·aop
孫治AllenSun15 小时前
【LangChain4J-03】Springboot 项目搭建框架
java·spring boot·后端