diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/mongo/MongoClientDependsOnBeanFactoryPostProcessor.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/mongo/MongoClientDependsOnBeanFactoryPostProcessor.java index 89e1e0e6cb..96bfe1ee17 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/mongo/MongoClientDependsOnBeanFactoryPostProcessor.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/mongo/MongoClientDependsOnBeanFactoryPostProcessor.java @@ -36,8 +36,25 @@ import org.springframework.data.mongodb.core.MongoClientFactoryBean; @Order(Ordered.LOWEST_PRECEDENCE) public class MongoClientDependsOnBeanFactoryPostProcessor extends AbstractDependsOnBeanFactoryPostProcessor { + /** + * Creates a new {@code MongoClientDependsOnBeanFactoryPostProcessor} that will set up + * dependencies upon beans with the given names. + * @param dependsOn names of the beans to depend upon + * @deprecated since 2.1.7 in favor of + * {@link #MongoClientDependsOnBeanFactoryPostProcessor} + */ + @Deprecated public MongoClientDependsOnBeanFactoryPostProcessor(String... dependsOn) { super(MongoClient.class, MongoClientFactoryBean.class, dependsOn); } + /** + * Creates a new {@code MongoClientDependsOnBeanFactoryPostProcessor} that will set up + * dependencies upon beans with the given types. + * @param dependsOn types of the beans to depend upon + */ + public MongoClientDependsOnBeanFactoryPostProcessor(Class... dependsOn) { + super(MongoClient.class, MongoClientFactoryBean.class, dependsOn); + } + } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/mongo/ReactiveStreamsMongoClientDependsOnBeanFactoryPostProcessor.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/mongo/ReactiveStreamsMongoClientDependsOnBeanFactoryPostProcessor.java index 50b5aa5d87..52ee096906 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/mongo/ReactiveStreamsMongoClientDependsOnBeanFactoryPostProcessor.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/mongo/ReactiveStreamsMongoClientDependsOnBeanFactoryPostProcessor.java @@ -37,8 +37,25 @@ import org.springframework.data.mongodb.core.ReactiveMongoClientFactoryBean; public class ReactiveStreamsMongoClientDependsOnBeanFactoryPostProcessor extends AbstractDependsOnBeanFactoryPostProcessor { + /** + * Creates a new {@code ReactiveStreamsMongoClientDependsOnBeanFactoryPostProcessor} + * that will set up dependencies upon beans with the given names. + * @param dependsOn names of the beans to depend upon + * @deprecated since 2.1.7 in favor of + * {@link #ReactiveStreamsMongoClientDependsOnBeanFactoryPostProcessor} + */ + @Deprecated public ReactiveStreamsMongoClientDependsOnBeanFactoryPostProcessor(String... dependsOn) { super(MongoClient.class, ReactiveMongoClientFactoryBean.class, dependsOn); } + /** + * Creates a new {@code ReactiveStreamsMongoClientDependsOnBeanFactoryPostProcessor} + * that will set up dependencies upon beans with the given types. + * @param dependsOn types of the beans to depend upon + */ + public ReactiveStreamsMongoClientDependsOnBeanFactoryPostProcessor(Class... dependsOn) { + super(MongoClient.class, ReactiveMongoClientFactoryBean.class, dependsOn); + } + } 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 132d6ffa2c..6e54d456ae 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,8 +48,6 @@ 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; @@ -218,32 +216,26 @@ public class EmbeddedMongoAutoConfiguration { */ @Configuration @ConditionalOnClass({ MongoClient.class, MongoClientFactoryBean.class }) - protected static class EmbeddedMongoDependencyConfiguration { + protected static class EmbeddedMongoDependencyConfiguration extends MongoClientDependsOnBeanFactoryPostProcessor { - @Bean - public MongoClientDependsOnBeanFactoryPostProcessor mongoClientDependsOnBeanFactoryPostProcessor( - ListableBeanFactory listableBeanFactory) { - String[] mongoExecutableBeanNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(listableBeanFactory, - MongodExecutable.class); - return new MongoClientDependsOnBeanFactoryPostProcessor(mongoExecutableBeanNames); + EmbeddedMongoDependencyConfiguration() { + super(MongodExecutable.class); } } /** - * Additional configuration to ensure that {@link MongoClient} beans depend on any + * Additional configuration to ensure that + * {@link com.mongodb.reactivestreams.client.MongoClient} beans depend on any * {@link MongodExecutable} beans. */ @Configuration @ConditionalOnClass({ com.mongodb.reactivestreams.client.MongoClient.class, ReactiveMongoClientFactoryBean.class }) - protected static class EmbeddedReactiveMongoDependencyConfiguration { + protected static class EmbeddedReactiveMongoDependencyConfiguration + extends ReactiveStreamsMongoClientDependsOnBeanFactoryPostProcessor { - @Bean - public ReactiveStreamsMongoClientDependsOnBeanFactoryPostProcessor reactiveStreamsMongoClientDependsOnBeanFactoryPostProcessor( - ListableBeanFactory listableBeanFactory) { - String[] mongoExecutableBeanNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(listableBeanFactory, - MongodExecutable.class); - return new ReactiveStreamsMongoClientDependsOnBeanFactoryPostProcessor(mongoExecutableBeanNames); + EmbeddedReactiveMongoDependencyConfiguration() { + super(MongodExecutable.class); } } 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 2e26ffa401..702a3e374c 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 @@ -18,6 +18,7 @@ package org.springframework.boot.autoconfigure.mongo.embedded; import java.io.File; import java.util.EnumSet; +import java.util.Map; import java.util.stream.Collectors; import com.mongodb.MongoClient; @@ -33,6 +34,7 @@ import org.junit.After; import org.junit.Test; import org.springframework.beans.factory.annotation.Value; +import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration; import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration; @@ -45,7 +47,6 @@ 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}. @@ -177,7 +178,12 @@ public class EmbeddedMongoAutoConfigurationTests { @Test public void customMongoServerConfiguration() { - assertThatCode(() -> load(CustomMongoConfiguration.class)).doesNotThrowAnyException(); + load(CustomMongoConfiguration.class); + Map mongoClients = this.context.getBeansOfType(MongoClient.class); + for (String mongoClientBeanName : mongoClients.keySet()) { + BeanDefinition beanDefinition = this.context.getBeanFactory().getBeanDefinition(mongoClientBeanName); + assertThat(beanDefinition.getDependsOn()).contains("customMongoServer"); + } } private void assertVersionConfiguration(String configuredVersion, String expectedVersion) {