Merge branch '3.2.x'

Closes gh-40863
This commit is contained in:
Andy Wilkinson
2024-05-22 09:16:57 +01:00
11 changed files with 174 additions and 8 deletions

View File

@@ -0,0 +1,35 @@
/*
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.testcontainers.lifecycle;
import org.testcontainers.containers.Container;
import org.springframework.context.ApplicationEvent;
/**
* Event published just before a Testcontainers {@link Container} is used.
*
* @author Andy Wilkinson
* @since 3.2.6
*/
public class BeforeTestcontainerUsedEvent extends ApplicationEvent {
public BeforeTestcontainerUsedEvent(Object source) {
super(source);
}
}

View File

@@ -39,7 +39,6 @@ import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor;
import org.springframework.boot.testcontainers.properties.BeforeTestcontainersPropertySuppliedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
@@ -61,7 +60,7 @@ import org.springframework.core.log.LogMessage;
*/
@Order(Ordered.LOWEST_PRECEDENCE)
class TestcontainersLifecycleBeanPostProcessor
implements DestructionAwareBeanPostProcessor, ApplicationListener<BeforeTestcontainersPropertySuppliedEvent> {
implements DestructionAwareBeanPostProcessor, ApplicationListener<BeforeTestcontainerUsedEvent> {
private static final Log logger = LogFactory.getLog(TestcontainersLifecycleBeanPostProcessor.class);
@@ -80,7 +79,7 @@ class TestcontainersLifecycleBeanPostProcessor
}
@Override
public void onApplicationEvent(BeforeTestcontainersPropertySuppliedEvent event) {
public void onApplicationEvent(BeforeTestcontainerUsedEvent event) {
initializeContainers();
}

View File

@@ -18,7 +18,7 @@ package org.springframework.boot.testcontainers.properties;
import java.util.function.Supplier;
import org.springframework.context.ApplicationEvent;
import org.springframework.boot.testcontainers.lifecycle.BeforeTestcontainerUsedEvent;
/**
* Event published just before the {@link Supplier value supplier} of a
@@ -26,8 +26,10 @@ import org.springframework.context.ApplicationEvent;
*
* @author Phillip Webb
* @since 3.2.2
* @deprecated since 3.2.6 in favor of {@link BeforeTestcontainerUsedEvent}
*/
public class BeforeTestcontainersPropertySuppliedEvent extends ApplicationEvent {
@Deprecated(since = "3.2.6", forRemoval = true)
public class BeforeTestcontainersPropertySuppliedEvent extends BeforeTestcontainerUsedEvent {
private final String propertyName;

View File

@@ -80,6 +80,7 @@ public class TestcontainersPropertySource extends MapPropertySource {
return (valueSupplier != null) ? getProperty(name, valueSupplier) : null;
}
@SuppressWarnings({ "removal", "deprecation" })
private Object getProperty(String name, Object valueSupplier) {
BeforeTestcontainersPropertySuppliedEvent event = new BeforeTestcontainersPropertySuppliedEvent(this, name);
this.eventPublishers.forEach((eventPublisher) -> eventPublisher.publishEvent(event));

View File

@@ -25,11 +25,16 @@ import org.testcontainers.containers.Container;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsRegistrar;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.autoconfigure.service.connection.ConnectionDetails;
import org.springframework.boot.autoconfigure.service.connection.ConnectionDetailsFactory;
import org.springframework.boot.origin.Origin;
import org.springframework.boot.origin.OriginProvider;
import org.springframework.boot.testcontainers.lifecycle.BeforeTestcontainerUsedEvent;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.core.ResolvableType;
import org.springframework.core.io.support.SpringFactoriesLoader;
import org.springframework.core.io.support.SpringFactoriesLoader.FailureHandler;
@@ -123,10 +128,12 @@ public abstract class ContainerConnectionDetailsFactory<C extends Container<?>,
* @param <C> the container type
*/
protected static class ContainerConnectionDetails<C extends Container<?>>
implements ConnectionDetails, OriginProvider, InitializingBean {
implements ConnectionDetails, OriginProvider, InitializingBean, ApplicationContextAware {
private final ContainerConnectionSource<C> source;
private volatile ApplicationEventPublisher eventPublisher;
private volatile C container;
/**
@@ -151,6 +158,7 @@ public abstract class ContainerConnectionDetailsFactory<C extends Container<?>,
protected final C getContainer() {
Assert.state(this.container != null,
"Container cannot be obtained before the connection details bean has been initialized");
this.eventPublisher.publishEvent(new BeforeTestcontainerUsedEvent(this));
return this.container;
}
@@ -159,6 +167,11 @@ public abstract class ContainerConnectionDetailsFactory<C extends Container<?>,
return this.source.getOrigin();
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.eventPublisher = applicationContext;
}
}
static class ContainerConnectionDetailsFactoriesRuntimeHints implements RuntimeHintsRegistrar {