第三方 @ConfigurationProperties 未被 Spring Boot 填充。

huangapple go评论50阅读模式
英文:

Third-party @ConfigurationProperties not being populated by spring-boot

问题

我有一个将第三方类作为@ConfigurationProperties bean进行绑定的bean,但Spring从不填充这些值。

代码看起来像这样:

Bean绑定部分:

@Bean
@ConfigurationProperties(prefix = "aws")
@ConditionalOnMissingBean(AwsServiceConfigurationProperties.class)
public AwsServiceConfigurationProperties defaultServiceProperties() {
    return new AwsServiceConfigurationProperties();
}

Bean类型部分:

@Data
public class AwsServiceConfigurationProperties {

  private URI endpoint;
  private Region region;

}

以及用于验证它是否工作的测试部分:

@Test
void shouldPickUpProperties() {
    contextRunner
        .withPropertyValues("aws.endpoint=http://fake-aws.test", "aws.region=test")
        .run(ctx -> assertThat(ctx.getBean(AwsServiceConfigurationProperties.class))
            .isNotNull()
            .hasAllNullFieldsOrPropertiesExcept("endpoint")
            .hasFieldOrPropertyWithValue("endpoint", URI.create("http://fake-aws.test"))
        );
}

我可以在文档和示例中找到的所有信息都说,使用这种设置应该可以工作,但字段始终为null

英文:

I have a bean wiring up a 3rd-party class as a @ConfigurationProperties bean, but Spring never populates the values.

the code looks something like:

The bean binding:

  @Bean
  @ConfigurationProperties(prefix = "aws")
  @ConditionalOnMissingBean(AwsServiceConfigurationProperties.class)
  public AwsServiceConfigurationProperties defaultServiceProperties() {
    return new AwsServiceConfigurationProperties();
  }

the bean type:

@Data
public class AwsServiceConfigurationProperties {

  private URI endpoint;
  private Region region;

}

and my test trying to validate it's working:

  @Test
  void shouldPickUpProperties() {
    contextRunner
        .withPropertyValues("aws.endpoint=http://fake-aws.test", "aws.region=test")
        .run(ctx -> assertThat(ctx.getBean(AwsServiceConfigurationProperties.class))
            .isNotNull()
            .hasAllNullFieldsOrPropertiesExcept("endpoint")
            .hasFieldOrPropertyWithValue("endpoint", URI.create("http://fake-aws.test"))
        );
  }

everything i can find in the docs and examples say it should be working with this setup, but the fields are all always null.

答案1

得分: 1

已解决。为了使其工作,您需要确保在@AutoConfiguration类上具有@EnableConfigurationProperties注释。

我发现,如果您在该注释中包括该类,它将引发错误,因为该类型没有@ConfigurationProperties注释,但如果您只包含注释,它会起作用。

不好的写法:

@AutoConfiguration
@EnableConfigurationProperties(AwsServiceConfigurationProperties.class)
class AwsAutoConfigure {
  ...
}

好的写法:

@AutoConfiguration
@EnableConfigurationProperties
class AwsAutoConfigure {
  ...
}
英文:

Figured it out. In order to work, you need to make sure that you have the @EnableConfigurationProperties annotation on the @AutoConfiguration class.

I found that if you include the class in that annotation, it will throw an error because the type doesn't have the @ConfigurationProperties annotation, but if you include just the annotation, it works.

Bad:

@AutoConfiguration
@EnableConfigurationProperties(AwsServiceConfigurationProperties.class)
class AwsAutoConfigure {
  ...
}

Good:

@AutoConfiguration
@EnableConfigurationProperties
class AwsAutoConfigure {
  ...
}

huangapple
  • 本文由 发表于 2023年6月29日 08:12:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76577394.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定