【问题标题】:spring boot use Environment load yml to mapspring boot 使用环境加载 yml 来映射
【发布时间】:2018-11-22 16:18:07
【问题描述】:

我有 yml 之类的:

application:
  address:
   '0xb7716e1f5690a841d698dd64ae13727bf719b2cd': '4'
   '0x0f020200ad0729f19be98ddb26685d80359428b7': '5'

Autowired环境变量如下-

@Autowired
Environment environment;

现在getProperty 的环境返回null

Map<String,String> maps = environment.getProperty("application.address",HashMap.class); //got null

如何获取地图类型的地址?

【问题讨论】:

  • 尝试声明这个变量@Value("${application.address}") Map&lt;String,String&gt; maps;。您的 yml 是否命名为 application.yml(spring boot 用于查找您的配置的默认名称)?
  • Could not resolve placeholder 'application.address' in value "${application.address}" 同样没用。
  • 你可以试试@ConfigurationProperties (mkyong.com/spring-boot/…)
  • 正在处理它@ConfigurationProperties("application")谢谢你

标签: java spring spring-boot yaml


【解决方案1】:

@ConfigurationProperties("application")

并设置一个变量来修改 private Map&lt;String,String&gt; address;

并添加get和set方法

public Map<String, String> getAddress() { return address; }

public void setAddress(Map<String, String> address) { this.address = address; }

你明白了

【讨论】:

    猜你喜欢
    • 2021-03-25
    • 1970-01-01
    • 2018-08-05
    • 2016-06-25
    • 2020-06-09
    • 2017-06-18
    • 2021-09-23
    • 1970-01-01
    • 2021-08-12
    相关资源
    最近更新 更多