From 39f103942569bc8f69fa70b4455640b9c7e3ff42 Mon Sep 17 00:00:00 2001 From: ielatif Date: Tue, 23 Apr 2019 18:09:09 +0200 Subject: [PATCH] Set up MongoClient beans' dependencies by type rather than name See gh-16627 --- .../EmbeddedMongoAutoConfiguration.java | 32 ++++++++++++------- .../EmbeddedMongoAutoConfigurationTests.java | 20 ++++++++++++ 2 files changed, 41 insertions(+), 11 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoAutoConfiguration.java index d1eb622d3c..132d6ffa2c 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoAutoConfiguration.java @@ -48,6 +48,8 @@ import de.flapdoodle.embed.process.store.ArtifactStoreBuilder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.BeanFactoryUtils; +import org.springframework.beans.factory.ListableBeanFactory; import org.springframework.boot.autoconfigure.AutoConfigureBefore; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; @@ -74,6 +76,7 @@ import org.springframework.data.mongodb.core.ReactiveMongoClientFactoryBean; * @author Andy Wilkinson * @author Yogesh Lonkar * @author Mark Paluch + * @author Issam El-atif * @since 1.3.0 */ @Configuration @@ -210,30 +213,37 @@ public class EmbeddedMongoAutoConfiguration { } /** - * Additional configuration to ensure that {@link MongoClient} beans depend on the - * {@code embeddedMongoServer} bean. + * Additional configuration to ensure that {@link MongoClient} beans depend on any + * {@link MongodExecutable} beans. */ @Configuration @ConditionalOnClass({ MongoClient.class, MongoClientFactoryBean.class }) - protected static class EmbeddedMongoDependencyConfiguration extends MongoClientDependsOnBeanFactoryPostProcessor { + protected static class EmbeddedMongoDependencyConfiguration { - public EmbeddedMongoDependencyConfiguration() { - super("embeddedMongoServer"); + @Bean + public MongoClientDependsOnBeanFactoryPostProcessor mongoClientDependsOnBeanFactoryPostProcessor( + ListableBeanFactory listableBeanFactory) { + String[] mongoExecutableBeanNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(listableBeanFactory, + MongodExecutable.class); + return new MongoClientDependsOnBeanFactoryPostProcessor(mongoExecutableBeanNames); } } /** - * Additional configuration to ensure that {@link MongoClient} beans depend on the - * {@code embeddedMongoServer} bean. + * Additional configuration to ensure that {@link MongoClient} beans depend on any + * {@link MongodExecutable} beans. */ @Configuration @ConditionalOnClass({ com.mongodb.reactivestreams.client.MongoClient.class, ReactiveMongoClientFactoryBean.class }) - protected static class EmbeddedReactiveMongoDependencyConfiguration - extends ReactiveStreamsMongoClientDependsOnBeanFactoryPostProcessor { + protected static class EmbeddedReactiveMongoDependencyConfiguration { - public EmbeddedReactiveMongoDependencyConfiguration() { - super("embeddedMongoServer"); + @Bean + public ReactiveStreamsMongoClientDependsOnBeanFactoryPostProcessor reactiveStreamsMongoClientDependsOnBeanFactoryPostProcessor( + ListableBeanFactory listableBeanFactory) { + String[] mongoExecutableBeanNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(listableBeanFactory, + MongodExecutable.class); + return new ReactiveStreamsMongoClientDependsOnBeanFactoryPostProcessor(mongoExecutableBeanNames); } } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoAutoConfigurationTests.java index 9a11a97509..2e26ffa401 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoAutoConfigurationTests.java @@ -22,10 +22,12 @@ import java.util.stream.Collectors; import com.mongodb.MongoClient; import de.flapdoodle.embed.mongo.MongodExecutable; +import de.flapdoodle.embed.mongo.MongodStarter; import de.flapdoodle.embed.mongo.config.IMongodConfig; import de.flapdoodle.embed.mongo.config.Storage; import de.flapdoodle.embed.mongo.distribution.Feature; import de.flapdoodle.embed.mongo.distribution.Version; +import de.flapdoodle.embed.process.config.IRuntimeConfig; import org.bson.Document; import org.junit.After; import org.junit.Test; @@ -43,6 +45,7 @@ import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.util.FileSystemUtils; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatCode; /** * Tests for {@link EmbeddedMongoAutoConfiguration}. @@ -50,6 +53,7 @@ import static org.assertj.core.api.Assertions.assertThat; * @author Henryk Konsek * @author Andy Wilkinson * @author Stephane Nicoll + * @author Issam El-atif */ public class EmbeddedMongoAutoConfigurationTests { @@ -171,6 +175,11 @@ public class EmbeddedMongoAutoConfigurationTests { assertThat(this.context.getBean(MongodExecutable.class).isRegisteredJobKiller()).isFalse(); } + @Test + public void customMongoServerConfiguration() { + assertThatCode(() -> load(CustomMongoConfiguration.class)).doesNotThrowAnyException(); + } + private void assertVersionConfiguration(String configuredVersion, String expectedVersion) { this.context = new AnnotationConfigApplicationContext(); TestPropertyValues.of("spring.data.mongodb.port=0").applyTo(this.context); @@ -216,4 +225,15 @@ public class EmbeddedMongoAutoConfigurationTests { } + @Configuration + static class CustomMongoConfiguration { + + @Bean(initMethod = "start", destroyMethod = "stop") + public MongodExecutable customMongoServer(IRuntimeConfig runtimeConfig, IMongodConfig mongodConfig) { + MongodStarter mongodStarter = MongodStarter.getInstance(runtimeConfig); + return mongodStarter.prepare(mongodConfig); + } + + } + }