Refine @ImportTestcontainers support

Refine logic so that bean names are no longer generated based on their
type and containers are started early.

See gh-35245
pull/35333/head
Phillip Webb 2 years ago
parent 1143396531
commit 56af1569d3

@ -22,9 +22,9 @@ import java.util.ArrayList;
import java.util.List;
import org.testcontainers.containers.Container;
import org.testcontainers.lifecycle.Startable;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.util.Assert;
import org.springframework.util.ReflectionUtils;
@ -35,12 +35,14 @@ import org.springframework.util.ReflectionUtils;
*/
class ContainerFieldsImporter {
void registerBeanDefinitions(BeanDefinitionRegistry registry, BeanNameGenerator importBeanNameGenerator,
Class<?> definitionClass) {
void registerBeanDefinitions(BeanDefinitionRegistry registry, Class<?> definitionClass) {
for (Field field : getContainerFields(definitionClass)) {
assertValid(field);
Container<?> container = getContainer(field);
registerBeanDefinition(registry, importBeanNameGenerator, field, container);
if (container instanceof Startable startable) {
startable.start();
}
registerBeanDefinition(registry, field, container);
}
}
@ -66,11 +68,14 @@ class ContainerFieldsImporter {
return container;
}
private void registerBeanDefinition(BeanDefinitionRegistry registry, BeanNameGenerator importBeanNameGenerator,
Field field, Container<?> container) {
private void registerBeanDefinition(BeanDefinitionRegistry registry, Field field, Container<?> container) {
TestcontainerFieldBeanDefinition beanDefinition = new TestcontainerFieldBeanDefinition(field, container);
String beanName = importBeanNameGenerator.generateBeanName(beanDefinition, registry);
String beanName = generateBeanName(field);
registry.registerBeanDefinition(beanName, beanDefinition);
}
private String generateBeanName(Field field) {
return "importTestContainer.%s.%s".formatted(field.getDeclaringClass().getName(), field.getName());
}
}

@ -17,7 +17,6 @@
package org.springframework.boot.testcontainers.context;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.annotation.MergedAnnotation;
import org.springframework.core.env.Environment;
@ -48,8 +47,7 @@ class ImportTestcontainersRegistrar implements ImportBeanDefinitionRegistrar {
}
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry,
BeanNameGenerator importBeanNameGenerator) {
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
MergedAnnotation<ImportTestcontainers> annotation = importingClassMetadata.getAnnotations()
.get(ImportTestcontainers.class);
Class<?>[] definitionClasses = annotation.getClassArray(MergedAnnotation.VALUE);
@ -57,13 +55,12 @@ class ImportTestcontainersRegistrar implements ImportBeanDefinitionRegistrar {
Class<?> importingClass = ClassUtils.resolveClassName(importingClassMetadata.getClassName(), null);
definitionClasses = new Class<?>[] { importingClass };
}
registerBeanDefinitions(registry, importBeanNameGenerator, definitionClasses);
registerBeanDefinitions(registry, definitionClasses);
}
private void registerBeanDefinitions(BeanDefinitionRegistry registry, BeanNameGenerator importBeanNameGenerator,
Class<?>[] definitionClasses) {
private void registerBeanDefinitions(BeanDefinitionRegistry registry, Class<?>[] definitionClasses) {
for (Class<?> definitionClass : definitionClasses) {
this.containerFieldsImporter.registerBeanDefinitions(registry, importBeanNameGenerator, definitionClass);
this.containerFieldsImporter.registerBeanDefinitions(registry, definitionClass);
if (this.dynamicPropertySourceMethodsImporter != null) {
this.dynamicPropertySourceMethodsImporter.registerDynamicPropertySources(definitionClass);
}

@ -40,6 +40,7 @@ class TestcontainerFieldBeanDefinition extends RootBeanDefinition implements Tes
this.annotations = MergedAnnotations.from(field);
this.setBeanClass(container.getClass());
setInstanceSupplier(() -> container);
setRole(ROLE_INFRASTRUCTURE);
}
@Override

Loading…
Cancel
Save