英文:
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 {
  ...
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论