@ -19,7 +19,6 @@ package org.springframework.boot.testcontainers.lifecycle;
import java.util.LinkedHashSet ;
import java.util.LinkedHashSet ;
import java.util.List ;
import java.util.List ;
import java.util.Set ;
import java.util.Set ;
import java.util.concurrent.atomic.AtomicBoolean ;
import org.apache.commons.logging.Log ;
import org.apache.commons.logging.Log ;
import org.apache.commons.logging.LogFactory ;
import org.apache.commons.logging.LogFactory ;
@ -28,6 +27,8 @@ import org.testcontainers.containers.GenericContainer;
import org.testcontainers.lifecycle.Startable ;
import org.testcontainers.lifecycle.Startable ;
import org.springframework.beans.BeansException ;
import org.springframework.beans.BeansException ;
import org.springframework.beans.factory.BeanCreationException ;
import org.springframework.beans.factory.BeanCurrentlyInCreationException ;
import org.springframework.beans.factory.NoSuchBeanDefinitionException ;
import org.springframework.beans.factory.NoSuchBeanDefinitionException ;
import org.springframework.beans.factory.config.BeanDefinition ;
import org.springframework.beans.factory.config.BeanDefinition ;
import org.springframework.beans.factory.config.BeanPostProcessor ;
import org.springframework.beans.factory.config.BeanPostProcessor ;
@ -57,7 +58,7 @@ class TestcontainersLifecycleBeanPostProcessor implements DestructionAwareBeanPo
private ConfigurableListableBeanFactory beanFactory ;
private ConfigurableListableBeanFactory beanFactory ;
private AtomicBoolean initializedContainers = new AtomicBoolean ( ) ;
private volatile boolean containersInitialized = false ;
TestcontainersLifecycleBeanPostProcessor ( ConfigurableListableBeanFactory beanFactory ) {
TestcontainersLifecycleBeanPostProcessor ( ConfigurableListableBeanFactory beanFactory ) {
this . beanFactory = beanFactory ;
this . beanFactory = beanFactory ;
@ -75,14 +76,27 @@ class TestcontainersLifecycleBeanPostProcessor implements DestructionAwareBeanPo
}
}
private void initializeContainers ( ) {
private void initializeContainers ( ) {
if ( this . initializedContainers . compareAndSet ( false , true ) ) {
if ( this . containersInitialized ) {
Set < String > beanNames = new LinkedHashSet < > ( ) ;
return ;
beanNames . addAll ( List . of ( this . beanFactory . getBeanNamesForType ( ContainerState . class , false , false ) ) ) ;
}
beanNames . addAll ( List . of ( this . beanFactory . getBeanNamesForType ( Startable . class , false , false ) ) ) ;
this . containersInitialized = true ;
for ( String beanName : beanNames ) {
Set < String > beanNames = new LinkedHashSet < > ( ) ;
logger . debug ( LogMessage . format ( "Initializing container bean '%s'" , beanName ) ) ;
beanNames . addAll ( List . of ( this . beanFactory . getBeanNamesForType ( ContainerState . class , false , false ) ) ) ;
beanNames . addAll ( List . of ( this . beanFactory . getBeanNamesForType ( Startable . class , false , false ) ) ) ;
for ( String beanName : beanNames ) {
try {
this . beanFactory . getBean ( beanName ) ;
this . beanFactory . getBean ( beanName ) ;
}
}
catch ( BeanCreationException ex ) {
if ( ex . contains ( BeanCurrentlyInCreationException . class ) ) {
this . containersInitialized = false ;
return ;
}
throw ex ;
}
}
if ( ! beanNames . isEmpty ( ) ) {
logger . debug ( LogMessage . format ( "Initialized container beans '%s'" , beanNames ) ) ;
}
}
}
}