【问题标题】:Cannot instantiate yaml variable list with evironment variables: Could not resolve placeholder in value无法使用环境变量实例化 yaml 变量列表:无法解析值中的占位符
【发布时间】:2021-01-29 16:39:17
【问题描述】:

我试图在我的application.yaml 文件的列表中设置一些环境变量,但是我收到以下错误消息:

java.lang.IllegalArgumentException:无法解析占位符“SOME_ID”的值 "${SOME_ID},${ANOTHER_ID}"

我的application.yaml 文件中有以下值:

com:
  foo:
    product-ids: ${SOME_ID},${ANOTHER_ID}

这些值是在其他一些 yaml 文件中设置的,例如:

application:
  env: test

subEnv:
  - name: SOME_ID
    value: "foo"
  - name: ANOTHER_ID
    value: "bar"

我的服务如下所示:

@Service
public class Service {
  private final List<String> productIds;

  public Service(
          @Value("#{'${com.foo.product-ids}'.split(',')}") List<String> productIds) {
    this.productIds = productIds;
  }
  ...
}

我的测试用初始化器看起来像这样:

@ContextConfiguration(
    classes = {Service.class},
    initializers = {SpringBootComponentTest.Initializer.class})
@SpringBootTest(
    webEnvironment = WebEnvironment.RANDOM_PORT,
    classes = {Service.class})
@SpringJUnitConfig
@ActiveProfiles("test")
public abstract class SpringBootComponentTest {

  protected static final String FIRST_VALUE = "abc";
  protected static final String SECOND_VALUE = "xyz";
...
  static class Initializer
      implements ApplicationContextInitializer<ConfigurableApplicationContext> {
    @Override
    public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
      TestPropertyValues.of(
              "com.foo.product-ids[0]=" + FIRST_VALUE,
              "com.foo.product-ids[1]=" + SECOND_VALUE)
          .applyTo(configurableApplicationContext.getEnvironment());
    }
  }
}

此外,如果我只是将变量引用替换为硬编码值,则问题似乎会消退

com:
  foo:
    product-ids: foo,bar

但这不是我正在寻找的解决方案;我希望能够将我的变量添加到此列表中。

编辑:这是来自我的application.yaml的大多数/相关数据

spring:
  application:
    name: my-service
server:
  port: 8080
management:
  endpoint:
    metrics:
      enabled: true
    prometheus:
      enabled: true
  endpoints.web.exposure.include: "*"
  server:
    port: 9080
  security:
    enabled: false
  metrics:
    export:
      prometheus:
        enabled: true
    distribution:
      sla:
        http.server.requests: ${CORE_COMMON_BACKEND_SLA:PT1S}
com:
  foo:
    product-ids: ${SOME_ID},${ANOTHER_ID}

编辑 2:

我应该注意,如果我不使用列表,我可以很好地访问这些变量。

所以如果我这样做:

com:
  foo:
    product-id1: ${SOME_ID}
    proudct-id2: ${ANOTHER_ID}

并相应地修改代码,这是可行的,但我仍然不希望将其作为我的解决方案。

由于某种原因,在我的products-ids 列表中引用一个变量似乎是个问题...

【问题讨论】:

  • 你应该发布application.yml的全部内容

标签: java spring-boot yaml


【解决方案1】:

它与 Spring Boot 加载 YAML 配置文件的顺序有关。 Detail here

在您的情况下,应用了 13 和 15 的顺序。

因此,您应该将特定于环境的配置文件命名为:application-test.yaml,因为您在测试用例中使用了 @ActiveProfiles("test")。

然后您的 application.yaml 应该正确地从特定于环境的配置文件加载属性。

【讨论】:

  • 抱歉,不确定我是否理解正确,...我已经有一个 application-test.yaml 文件,但它只包含有关日志级别的内容。另外,我想我应该提到“初始化”方法中有很多其他字段,我正在为其他属性字段分配随机测试值,我省略了这些字段并且与这个问题无关,所以我认为这个部分应该没问题。
猜你喜欢
  • 2019-07-20
  • 2018-08-14
  • 2016-07-24
  • 1970-01-01
  • 2021-04-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-30
  • 1970-01-01
相关资源
最近更新 更多