Merge pull request #16627 from ielatif

* gh-16627:
  Polish "Set up MongoClient beans' dependencies by type rather than name"
  Set up MongoClient beans' dependencies by type rather than name

Closes gh-16627
This commit is contained in:
Andy Wilkinson 2019-07-10 13:19:54 +01:00
commit 50fe7a0da5
4 changed files with 70 additions and 8 deletions

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -74,6 +74,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 +211,31 @@ 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 {
public EmbeddedMongoDependencyConfiguration() {
super("embeddedMongoServer");
EmbeddedMongoDependencyConfiguration() {
super(MongodExecutable.class);
}
}
/**
* Additional configuration to ensure that {@link MongoClient} beans depend on the
* {@code embeddedMongoServer} bean.
* 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
extends ReactiveStreamsMongoClientDependsOnBeanFactoryPostProcessor {
public EmbeddedReactiveMongoDependencyConfiguration() {
super("embeddedMongoServer");
EmbeddedReactiveMongoDependencyConfiguration() {
super(MongodExecutable.class);
}
}

View File

@ -18,19 +18,23 @@ 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;
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;
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;
@ -50,6 +54,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 +176,16 @@ public class EmbeddedMongoAutoConfigurationTests {
assertThat(this.context.getBean(MongodExecutable.class).isRegisteredJobKiller()).isFalse();
}
@Test
public void customMongoServerConfiguration() {
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) {
this.context = new AnnotationConfigApplicationContext();
TestPropertyValues.of("spring.data.mongodb.port=0").applyTo(this.context);
@ -216,4 +231,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);
}
}
}