In Spring Boot until 3.3.x I was able to inject an instance of DynamicPropertyRegistry into a @Bean configuration in my Test classes like this:
@TestConfiguration
public class DemoTestConfig {
@Bean
DummyService dummyService(DynamicPropertyRegistry registry) {
registry.add("demo.test.prop", () -> "Hello World");
return new DummyService();
}
}
In Spring Boot 3.4.0-M1 this also does work, but only as long as I do not have spring-boot-testcontainers in classpath/pom.xml. With Testcontainers in classpath, Spring Boots complains that two instances of DynamicPropertyRegistry are found:
Parameter 0 of method dummyService in com.example.demo.DemoTestConfig required a single bean, but 2 were found:
- dynamicPropertyRegistry: defined by method 'dynamicPropertyRegistry' in class path resource [org/springframework/boot/testcontainers/properties/TestcontainersPropertySourceAutoConfiguration.class]
- org.springframework.test.context.support.DynamicPropertiesContextCustomizer.dynamicPropertyRegistry: a programmatically registered singleton
In 3.3.x it works with and without spring-boot-testcontainers in pom.xml.
I created an example project that shows the behaviour with Spring Boot 3.3.2 and 3.4.0-M1 here: https://github.com/nilshartmann/spring-boot-3-4-dynamic_properties. See README.md there how to run the test in 3.3.2 and the failing one in 3.4.0 using Maven.
In Spring 6.2.0-M1 I saw this addition recently https://github.com/spring-projects/spring-framework/issues/32271, maybe that is related?
Comment From: wilkinsona
Thanks for the report.
In Spring 6.2.0-M1 I saw this addition recently https://github.com/spring-projects/spring-framework/issues/32271, maybe that is related?
Yes, that's the underlying cause of the regression. Hopefully we can adapt to it in Boot, but we may need a Framework change.
Comment From: wilkinsona
FYI, @sbrannen. It's not immediately clear to me how to address this one as there's no easy way to compose DynamicPropertyRegistry implementations and the event publishing that we require for Testcontainers isn't supported by the Framework implementation.
Comment From: philwebb
I've put a work-around in for M2, but I think we might need a better long-term solution. In hindsight, we should probably not have used DynamicPropertyRegistry but instead have our own interface.
Comment From: wilkinsona
I've opened #41996 to follow up on a better long-term solution so that we can close this one and have it appear in the changelog.
Comment From: nilshartmann
@wilkinsona @philwebb thanks a lot for the fix! Will try the workaround when M2 is available. 😊