From 56af1569d3edde34d12ff9ba49b5810bca544f1e Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Thu, 4 May 2023 15:35:58 -0700 Subject: [PATCH] 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 --- .../context/ContainerFieldsImporter.java | 19 ++++++++++++------- .../ImportTestcontainersRegistrar.java | 11 ++++------- .../TestcontainerFieldBeanDefinition.java | 1 + 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/spring-boot-project/spring-boot-testcontainers/src/main/java/org/springframework/boot/testcontainers/context/ContainerFieldsImporter.java b/spring-boot-project/spring-boot-testcontainers/src/main/java/org/springframework/boot/testcontainers/context/ContainerFieldsImporter.java index f943092f8c..ed12fd8de1 100644 --- a/spring-boot-project/spring-boot-testcontainers/src/main/java/org/springframework/boot/testcontainers/context/ContainerFieldsImporter.java +++ b/spring-boot-project/spring-boot-testcontainers/src/main/java/org/springframework/boot/testcontainers/context/ContainerFieldsImporter.java @@ -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()); + } + } diff --git a/spring-boot-project/spring-boot-testcontainers/src/main/java/org/springframework/boot/testcontainers/context/ImportTestcontainersRegistrar.java b/spring-boot-project/spring-boot-testcontainers/src/main/java/org/springframework/boot/testcontainers/context/ImportTestcontainersRegistrar.java index 61d3373d7a..9e4dc4c97d 100644 --- a/spring-boot-project/spring-boot-testcontainers/src/main/java/org/springframework/boot/testcontainers/context/ImportTestcontainersRegistrar.java +++ b/spring-boot-project/spring-boot-testcontainers/src/main/java/org/springframework/boot/testcontainers/context/ImportTestcontainersRegistrar.java @@ -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 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); } diff --git a/spring-boot-project/spring-boot-testcontainers/src/main/java/org/springframework/boot/testcontainers/context/TestcontainerFieldBeanDefinition.java b/spring-boot-project/spring-boot-testcontainers/src/main/java/org/springframework/boot/testcontainers/context/TestcontainerFieldBeanDefinition.java index a6b199b96b..c5cf32d4b1 100644 --- a/spring-boot-project/spring-boot-testcontainers/src/main/java/org/springframework/boot/testcontainers/context/TestcontainerFieldBeanDefinition.java +++ b/spring-boot-project/spring-boot-testcontainers/src/main/java/org/springframework/boot/testcontainers/context/TestcontainerFieldBeanDefinition.java @@ -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