【问题标题】:ConfigurationProperties binding a property name with a number in from a yaml fileConfigurationProperties 将属性名称与 yaml 文件中的数字绑定
【发布时间】:2019-01-02 16:44:47
【问题描述】:

我是 Spring-Boot 新手,在将 application.yml 文件中的属性值绑定到使用 @ConfigurationProperies 注释的类时遇到问题。

在application.yml中:

aaa:
  what-1word-is: true

@ConfigurationProperties注解的类中:

@Data
@Configuration
@ConfigurationProperties(prefix = "aaa")
public class Test
{
    private boolean what1WordIs;
}

我尝试使用不同的属性名称, 但它们都不起作用; what1WordIs 始终是 false。 我试过的名字, what-1-word-is, what-1word-is, what1-word-is, what-1Word-is。 只有what1-word-is 有效(将配置类中的what1WordIs 设置为true

Spring 是否能够绑定名称中带有数字的属性?

【问题讨论】:

  • 正如@Alexander Polozov 回答中提到的那样,实际的属性名称(因为您使用的是 YAML 配置)是 aaa.what-1-word-is。前缀很重要。
  • 感谢您的建议。我忘了在问题中添加prefix = "aaa",但我确实在代码中有它

标签: java spring spring-boot yaml


【解决方案1】:

它应该工作。 我已经尝试过了,它对我有用。即使是“what-1-word-is”。

application.yml

aaa:
  what-1-word-is: true

配置类

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
@ConfigurationProperties(prefix = "aaa")
@Data
public class Config {
  private boolean what1WordIs;

  @PostConstruct
  public void init(){
      System.out.println("CONFIG :: "+this);
  }
}

结果我们可以看到:

CONFIG :: Config(what1WordIs=true)

我认为您遇到此问题是因为您使用了@Configuration 注释而不是@Component。

顺便说一句:Spring 允许我们在配置文件中使用不同的属性名称。我已经尝试了您提到的所有选项并且它有效。 (更多:https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-external-config-relaxed-binding

BTW2:如果你想看看 Spring 期望哪些属性可以添加到依赖项中:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>

它将在描述所有属性的/target/classes/META-INF 目录中生成spring-configuration-metadata.json。 示例:

{
  "groups": [
    {
      "name": "aaa",
      "type": "com.supra89kren.test_spring.configurtion.Config",
      "sourceType": "com.supra89kren.test_spring.configurtion.Config"
    }
  ],
  "properties": [
    {
      "name": "aaa.what1-word-is",
      "type": "java.lang.Boolean",
      "sourceType": "com.supra89kren.test_spring.configurtion.Config",
      "defaultValue": false
    }
  ],
  "hints": []
}

此外,IDE 将能够为您自动完成 :)

BTW3:请检查您是否使用了 Lombok 库中的 @Data。

我希望它仍然是真实的:)

【讨论】:

  • spring-configuration-metadata.json 的引用(以及如何获得它:-p)是救命稻草! :-)
【解决方案2】:

免责声明
这不是您要寻找的答案, 但可能是解决问题的方法。

关于我的一切
我不喜欢@ConfigurationProperties 绑定技术 (您正在使用)。 为了我, 这似乎是“太神奇了”。 反而, 我更喜欢在每个属性上使用@Value 注解。

试试看
尝试在您的 what1WordIs 字段上使用以下注释:

@Value("${aaa.what-1word-is}")
private boolean what1WordIs;

【讨论】:

    【解决方案3】:

    我将提出一些与所要求的不同的建议。我可能建议不要在属性文件中使用数字。

    如果可能,您可以尝试将您的属性名称设置为类似以下内容:

    aaa: what-single-word-is

    或者更短一点的:

    aaa: single-word

    Can't read yaml's complex object using @ConfigurationProperties. Integer cannot be cast to String

    【讨论】:

    • 感谢您的建议!我用其他字符替换了数字,它仍然有效。可能需要更多地挖掘它的实际工作原理
    猜你喜欢
    • 2017-06-02
    • 2021-09-22
    • 2018-06-09
    • 2017-11-16
    • 1970-01-01
    • 2019-12-15
    • 2011-09-01
    • 1970-01-01
    • 2017-11-29
    相关资源
    最近更新 更多