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

相关推荐
Nyarlathotep01135 小时前
SpringBoot Starter的用法以及原理
java·spring boot
dkbnull1 天前
深入理解Spring两大特性:IoC和AOP
spring boot
洋洋技术笔记1 天前
Spring Boot条件注解详解
java·spring boot
洋洋技术笔记2 天前
Spring Boot配置管理最佳实践
spring boot
用户8307196840823 天前
Spring Boot 项目中日期处理的最佳实践
java·spring boot
大道至简Edward3 天前
Spring Boot 2.7 + JDK 8 升级到 Spring Boot 3.x + JDK 17 完整指南
spring boot·后端
洋洋技术笔记3 天前
Spring Boot启动流程解析
spring boot·后端
怒放吧德德4 天前
Spring Boot 实战:RSA+AES 接口全链路加解密(防篡改 / 防重放)
java·spring boot·后端
李慕婉学姐4 天前
Springboot智慧社区系统设计与开发6n99s526(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
数据库·spring boot·后端
QQ5110082854 天前
python+springboot+django/flask的校园资料分享系统
spring boot·python·django·flask·node.js·php