diff --git a/spring-boot-project/spring-boot-testcontainers/src/main/java/org/springframework/boot/testcontainers/lifecycle/TestcontainersLifecycleApplicationContextInitializer.java b/spring-boot-project/spring-boot-testcontainers/src/main/java/org/springframework/boot/testcontainers/lifecycle/TestcontainersLifecycleApplicationContextInitializer.java index 4a2b52ce2c..13c40c5cc6 100644 --- a/spring-boot-project/spring-boot-testcontainers/src/main/java/org/springframework/boot/testcontainers/lifecycle/TestcontainersLifecycleApplicationContextInitializer.java +++ b/spring-boot-project/spring-boot-testcontainers/src/main/java/org/springframework/boot/testcontainers/lifecycle/TestcontainersLifecycleApplicationContextInitializer.java @@ -16,6 +16,8 @@ package org.springframework.boot.testcontainers.lifecycle; +import java.util.Collections; +import java.util.Set; import java.util.WeakHashMap; import org.testcontainers.lifecycle.Startable; @@ -34,12 +36,12 @@ import org.springframework.context.ConfigurableApplicationContext; public class TestcontainersLifecycleApplicationContextInitializer implements ApplicationContextInitializer { - private static WeakHashMap applied = new WeakHashMap<>(); + private static Set applied = Collections.newSetFromMap(new WeakHashMap<>()); @Override public void initialize(ConfigurableApplicationContext applicationContext) { synchronized (applied) { - if (applied.put(applicationContext, Boolean.TRUE) == Boolean.TRUE) { + if (!applied.add(applicationContext)) { return; } }