Polish "Set up MongoClient beans' dependencies by type rather than name"

See gh-16627
pull/17511/head
Andy Wilkinson 5 years ago
parent 39f1039425
commit f753c31988

@ -36,8 +36,25 @@ import org.springframework.data.mongodb.core.MongoClientFactoryBean;
@Order(Ordered.LOWEST_PRECEDENCE) @Order(Ordered.LOWEST_PRECEDENCE)
public class MongoClientDependsOnBeanFactoryPostProcessor extends AbstractDependsOnBeanFactoryPostProcessor { 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) { public MongoClientDependsOnBeanFactoryPostProcessor(String... dependsOn) {
super(MongoClient.class, MongoClientFactoryBean.class, 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);
}
} }

@ -37,8 +37,25 @@ import org.springframework.data.mongodb.core.ReactiveMongoClientFactoryBean;
public class ReactiveStreamsMongoClientDependsOnBeanFactoryPostProcessor public class ReactiveStreamsMongoClientDependsOnBeanFactoryPostProcessor
extends AbstractDependsOnBeanFactoryPostProcessor { 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) { public ReactiveStreamsMongoClientDependsOnBeanFactoryPostProcessor(String... dependsOn) {
super(MongoClient.class, ReactiveMongoClientFactoryBean.class, 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);
}
} }

@ -48,8 +48,6 @@ import de.flapdoodle.embed.process.store.ArtifactStoreBuilder;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; 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.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
@ -218,32 +216,26 @@ public class EmbeddedMongoAutoConfiguration {
*/ */
@Configuration @Configuration
@ConditionalOnClass({ MongoClient.class, MongoClientFactoryBean.class }) @ConditionalOnClass({ MongoClient.class, MongoClientFactoryBean.class })
protected static class EmbeddedMongoDependencyConfiguration { protected static class EmbeddedMongoDependencyConfiguration extends MongoClientDependsOnBeanFactoryPostProcessor {
@Bean EmbeddedMongoDependencyConfiguration() {
public MongoClientDependsOnBeanFactoryPostProcessor mongoClientDependsOnBeanFactoryPostProcessor( super(MongodExecutable.class);
ListableBeanFactory listableBeanFactory) {
String[] mongoExecutableBeanNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(listableBeanFactory,
MongodExecutable.class);
return new MongoClientDependsOnBeanFactoryPostProcessor(mongoExecutableBeanNames);
} }
} }
/** /**
* 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. * {@link MongodExecutable} beans.
*/ */
@Configuration @Configuration
@ConditionalOnClass({ com.mongodb.reactivestreams.client.MongoClient.class, ReactiveMongoClientFactoryBean.class }) @ConditionalOnClass({ com.mongodb.reactivestreams.client.MongoClient.class, ReactiveMongoClientFactoryBean.class })
protected static class EmbeddedReactiveMongoDependencyConfiguration { protected static class EmbeddedReactiveMongoDependencyConfiguration
extends ReactiveStreamsMongoClientDependsOnBeanFactoryPostProcessor {
@Bean EmbeddedReactiveMongoDependencyConfiguration() {
public ReactiveStreamsMongoClientDependsOnBeanFactoryPostProcessor reactiveStreamsMongoClientDependsOnBeanFactoryPostProcessor( super(MongodExecutable.class);
ListableBeanFactory listableBeanFactory) {
String[] mongoExecutableBeanNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(listableBeanFactory,
MongodExecutable.class);
return new ReactiveStreamsMongoClientDependsOnBeanFactoryPostProcessor(mongoExecutableBeanNames);
} }
} }

@ -18,6 +18,7 @@ package org.springframework.boot.autoconfigure.mongo.embedded;
import java.io.File; import java.io.File;
import java.util.EnumSet; import java.util.EnumSet;
import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import com.mongodb.MongoClient; import com.mongodb.MongoClient;
@ -33,6 +34,7 @@ import org.junit.After;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.factory.annotation.Value; 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.context.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration; import org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration;
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration; import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
@ -45,7 +47,6 @@ import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.util.FileSystemUtils; import org.springframework.util.FileSystemUtils;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
/** /**
* Tests for {@link EmbeddedMongoAutoConfiguration}. * Tests for {@link EmbeddedMongoAutoConfiguration}.
@ -177,7 +178,12 @@ public class EmbeddedMongoAutoConfigurationTests {
@Test @Test
public void customMongoServerConfiguration() { public void customMongoServerConfiguration() {
assertThatCode(() -> load(CustomMongoConfiguration.class)).doesNotThrowAnyException(); load(CustomMongoConfiguration.class);
Map<String, MongoClient> 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) { private void assertVersionConfiguration(String configuredVersion, String expectedVersion) {

Loading…
Cancel
Save