pringboot 读取properties中的list和map数据

本文详细介绍如何在SpringBoot中读取properties文件中的list和map数据。通过自定义配置文件和使用@ConfigurationProperties注解,可以轻松地将配置文件中的数据映射到Java对象中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

pringboot 读取properties中的list和map数据

 

1 自定义citycode.properties

#List properties
citycode.list[0]=www
citycode.list[1]=localhost
citycode.list[2]=wuhan
citycode.list[3]=tianjin


#Map Properties
citycode.map.www=4201
citycode.map.wuhan=4201
citycode.map.tianjin=1200
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

2 设置一个类去读取properties

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Data
@Configuration
@PropertySource("classpath:citycode.properties")
@ConfigurationProperties(prefix = "citycode")
public class CityCodeConfig {

    private List<String> list = new ArrayList<>();

    private Map<String, String> map = new HashMap<>();

}

上面的get set 方法。

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

3 直接使用即可

@Autowired
    private CityCodeConfig cityCodeConfig;
  • 1
  • 2

效果如图 
这里写图片描述

### 从YAML文件中读取 `Map<String, List<String>>` 类型的数据 为了实现这一目标,可以通过定义相应的 Java 实体类并利用 Spring Boot 的 `@ConfigurationProperties` 注解来简化配置项的绑定过程。 #### 定义实体类 创建一个名为 `PlayerAttributeConfig` 的类用于存储来自 YAML 文件中的配置: ```java import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; import java.util.Map; import java.util.List; @Component @ConfigurationProperties(prefix = "player.attribute") public class PlayerAttributeConfig { private Map<String, List<String>> attributes; public Map<String, List<String>> getAttributes() { return attributes; } public void setAttributes(Map<String, List<String>> attributes) { this.attributes = attributes; } } ``` 此段代码展示了如何声明一个包含 `Map<String, List<String>>` 属性的对象,并通过 `@ConfigurationProperties` 来指定前缀以便于匹配 YAML 中的相关键值[^1]。 #### 编写YAML配置文件 在项目的资源目录下编辑 `application.yml` 或者其他自定义名称的 YML 文件,按照如下方式设置属性: ```yaml player: attribute: attributes: 'skill-set': - "Dribbling" - "Shooting" - "Passing" 'fitness-levels': - "High Endurance" - "Agility" - "Strength" ``` 注意这里的 `'skill-set'` `'fitness-levels'` 是作为 map 的 key 存储,在某些情况下可能需要用单引号包裹以防止特殊字符引起解析错误[^3]。 #### 使用服务层访问配置数据 最后可以在 ServiceImpl.java 中注入上述配置对象从而获取到所需的配置信息: ```java @Service public class PlayerServiceImpl implements PlayerService { private final PlayerAttributeConfig playerAttributeConfig; @Autowired public PlayerServiceImpl(PlayerAttributeConfig playerAttributeConfig){ this.playerAttributeConfig = playerAttributeConfig; } // 方法逻辑... } ``` 这样就可以方便地在整个应用程序范围内共享这些配置参数了[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值