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.
Figured it out. In order to work, you need to make sure that you have the
@EnableConfigurationPropertiesannotation on the@AutoConfigurationclass.I found that if you include the class in that annotation, it will throw an error because the type doesn't have the
@ConfigurationPropertiesannotation, but if you include just the annotation, it works.Bad:
Good: