【问题标题】:How to store properties into a Map [closed]如何将属性存储到地图中[关闭]
【发布时间】:2021-11-04 15:07:18
【问题描述】:

我有一个属性文件,例如 *.properties 键 1 = 值 1 key2 = value2

有没有办法将所有键值对存储在哈希映射中?

【问题讨论】:

  • 那么您想要特定属性文件中的所有属性还是所有属性文件中的所有属性?
  • 来自特定的属性文件
  • 这能回答你的问题吗? Spring: inject properties file into map

标签: java spring spring-boot


【解决方案1】:

如果问题与 Spring 框架有关,那么您可以基于 *.properties 或 *.yml 文件创建配置 Bean。

MyAppProperties.java 示例

@Getter
@Setter
@FieldDefaults(level = AccessLevel.PRIVATE)
@Configuration
@ConfigurationProperties(prefix = "props")
public class MyAppProperties {
    Map<String, String> map = new HashMap<>();
}

而您以props.map.* 开头的属性将通过此配置Bean 可用,可以从应用程序上下文中获取。

props.map.key1=value1
props.map.key2=value2

有关详细信息,请参阅文档: https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.external-config.typesafe-configuration-properties.java-bean-binding

【讨论】:

    猜你喜欢
    • 2021-03-11
    • 1970-01-01
    • 2016-03-02
    • 2017-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-20
    • 1970-01-01
    相关资源
    最近更新 更多