1

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.

toadzky
  • 3,806
  • 1
  • 15
  • 26

1 Answers1

1

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 {
  ...
}
toadzky
  • 3,806
  • 1
  • 15
  • 26