Merge branch '2.1.x'

Closes gh-17481
This commit is contained in:
Andy Wilkinson
2019-07-10 13:30:10 +01:00
4 changed files with 69 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

@@ -77,6 +77,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(proxyBeanMethods = false)
@@ -211,30 +212,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(proxyBeanMethods = false)
@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(proxyBeanMethods = false)
@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

@@ -20,10 +20,12 @@ import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
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;
@@ -37,6 +39,7 @@ import org.junit.jupiter.api.io.TempDir;
import org.springframework.beans.DirectFieldAccessor;
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;
@@ -56,6 +59,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Henryk Konsek
* @author Andy Wilkinson
* @author Stephane Nicoll
* @author Issam El-atif
*/
class EmbeddedMongoAutoConfigurationTests {
@@ -186,6 +190,16 @@ class EmbeddedMongoAutoConfigurationTests {
assertThat(this.context.getBean(MongodExecutable.class).isRegisteredJobKiller()).isFalse();
}
@Test
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);
@@ -248,4 +262,15 @@ class EmbeddedMongoAutoConfigurationTests {
}
@Configuration(proxyBeanMethods = false)
static class CustomMongoConfiguration {
@Bean(initMethod = "start", destroyMethod = "stop")
MongodExecutable customMongoServer(IRuntimeConfig runtimeConfig, IMongodConfig mongodConfig) {
MongodStarter mongodStarter = MongodStarter.getInstance(runtimeConfig);
return mongodStarter.prepare(mongodConfig);
}
}
}