Spring 引导加载嵌套 属性 以数字为键

Spring boot loading nested property with Number as key

我在 spring 启动应用程序中嵌套了 属性。例如:

 topics:
  target:
    abc_1:
      12345678:
        key: value_1
      default:
        key: value_1

我尝试在我的 class 中使用 @Value 访问该值,如下所示:

@Value("${topics.target.abc_1.12345678.key}")
private String key;

但是我在启动应用程序时遇到以下异常。

org.springframework.beans.factory.BeanCreationException: Error creating bean with name '***': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'topics.target.abc_1.12345678.key' in value "${topics.target.abc_1.12345678.key}"

at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:378)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1341)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:393)

但令人惊讶的是,当我尝试加载 topics.target.abc_1.default.key 时,它起作用了。在嵌套结构的属性中以数字作为键是否有任何限制? 我确信具有数字值的键在非嵌套结构中没有任何问题。

此问题是因为其中一个键 abc_1 中的 "_"

从 属性 中删除“_”后,一切似乎都正常。