1616
1717package org .springframework .experimental .boot .test .context ;
1818
19+ import java .util .ArrayList ;
20+ import java .util .List ;
21+
1922import org .springframework .beans .factory .BeanFactory ;
2023import org .springframework .beans .factory .annotation .AnnotatedBeanDefinition ;
2124import org .springframework .beans .factory .config .BeanDefinition ;
2225import org .springframework .beans .factory .config .ConfigurableListableBeanFactory ;
26+ import org .springframework .beans .factory .support .BeanDefinitionBuilder ;
2327import org .springframework .beans .factory .support .BeanDefinitionRegistry ;
2428import org .springframework .boot .testcontainers .properties .TestcontainersPropertySource ;
2529import org .springframework .context .annotation .ImportBeanDefinitionRegistrar ;
2630import org .springframework .core .annotation .MergedAnnotation ;
2731import org .springframework .core .env .Environment ;
2832import org .springframework .core .type .AnnotationMetadata ;
2933import org .springframework .core .type .MethodMetadata ;
34+ import org .springframework .test .context .DynamicPropertyRegistrar ;
3035import org .springframework .test .context .DynamicPropertyRegistry ;
36+ import org .springframework .util .ClassUtils ;
3137
3238/**
3339 * Finds beans annotated with {@link DynamicProperty} and adds the properties to the
@@ -52,11 +58,39 @@ class DynamicPropertyDefinitionRegistrar implements ImportBeanDefinitionRegistra
5258 @ Override
5359 public void registerBeanDefinitions (AnnotationMetadata importingClassMetadata , BeanDefinitionRegistry registry ) {
5460 if (this .beanFactory instanceof ConfigurableListableBeanFactory listableBeanFactory ) {
55- registerBeanDefinitions (listableBeanFactory , registry );
61+ if (ClassUtils .isPresent ("org.springframework.test.context.DynamicPropertyRegistrar" ,
62+ getClass ().getClassLoader ())) {
63+ registerDynamicPropertyRegistrar (listableBeanFactory , registry );
64+ }
65+ else {
66+ registerTestcontainersPropertySource (listableBeanFactory , registry );
67+ }
68+ }
69+ }
70+
71+ private void registerDynamicPropertyRegistrar (ConfigurableListableBeanFactory beanFactory ,
72+ BeanDefinitionRegistry registry ) {
73+ List <DynamicPropertyRegistryProperty > properties = new ArrayList <>();
74+ for (String dynamicPropertyBeanName : beanFactory .getBeanNamesForAnnotation (DynamicProperty .class )) {
75+ BeanDefinition dynamicPropertyBeanDefinition = registry .getBeanDefinition (dynamicPropertyBeanName );
76+ DynamicPropertyRegistryProperty property = createRegistryProperty (dynamicPropertyBeanDefinition ,
77+ dynamicPropertyBeanName );
78+ if (property == null ) {
79+ throw new IllegalStateException (
80+ "Missing @DynamicProperty annotation on BeanDefinition of " + dynamicPropertyBeanName );
81+ }
82+ properties .add (property );
5683 }
84+
85+ BeanDefinitionBuilder registrarBdb = BeanDefinitionBuilder
86+ .rootBeanDefinition (DynamicPropertyRegistryPropertyRegistrar .class );
87+ registrarBdb .addConstructorArgValue (properties );
88+ registry .registerBeanDefinition ("testjarsDynamicPropertyRegistryPropertyRegistrar" ,
89+ registrarBdb .getBeanDefinition ());
5790 }
5891
59- private void registerBeanDefinitions (ConfigurableListableBeanFactory beanFactory , BeanDefinitionRegistry registry ) {
92+ private void registerTestcontainersPropertySource (ConfigurableListableBeanFactory beanFactory ,
93+ BeanDefinitionRegistry registry ) {
6094 DynamicPropertyRegistry properties = TestcontainersPropertySource .attach (this .environment );
6195 for (String dynamicPropertyBeanName : beanFactory .getBeanNamesForAnnotation (DynamicProperty .class )) {
6296 BeanDefinition dynamicPropertyBeanDefinition = registry .getBeanDefinition (dynamicPropertyBeanName );
@@ -82,4 +116,21 @@ private DynamicPropertyRegistryProperty createRegistryProperty(BeanDefinition dy
82116 return null ;
83117 }
84118
119+ static class DynamicPropertyRegistryPropertyRegistrar implements DynamicPropertyRegistrar {
120+
121+ private final List <DynamicPropertyRegistryProperty > properties ;
122+
123+ DynamicPropertyRegistryPropertyRegistrar (List <DynamicPropertyRegistryProperty > properties ) {
124+ this .properties = properties ;
125+ }
126+
127+ @ Override
128+ public void accept (DynamicPropertyRegistry registry ) {
129+ for (DynamicPropertyRegistryProperty property : this .properties ) {
130+ registry .add (property .name (), property .value ());
131+ }
132+ }
133+
134+ }
135+
85136}
0 commit comments