Skip to content

Commit 50fe7a0

Browse files
committed
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
2 parents 1678de7 + f753c31 commit 50fe7a0

File tree

4 files changed

+70
-8
lines changed

4 files changed

+70
-8
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/mongo/MongoClientDependsOnBeanFactoryPostProcessor.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,25 @@
3636
@Order(Ordered.LOWEST_PRECEDENCE)
3737
public class MongoClientDependsOnBeanFactoryPostProcessor extends AbstractDependsOnBeanFactoryPostProcessor {
3838

39+
/**
40+
* Creates a new {@code MongoClientDependsOnBeanFactoryPostProcessor} that will set up
41+
* dependencies upon beans with the given names.
42+
* @param dependsOn names of the beans to depend upon
43+
* @deprecated since 2.1.7 in favor of
44+
* {@link #MongoClientDependsOnBeanFactoryPostProcessor}
45+
*/
46+
@Deprecated
3947
public MongoClientDependsOnBeanFactoryPostProcessor(String... dependsOn) {
4048
super(MongoClient.class, MongoClientFactoryBean.class, dependsOn);
4149
}
4250

51+
/**
52+
* Creates a new {@code MongoClientDependsOnBeanFactoryPostProcessor} that will set up
53+
* dependencies upon beans with the given types.
54+
* @param dependsOn types of the beans to depend upon
55+
*/
56+
public MongoClientDependsOnBeanFactoryPostProcessor(Class<?>... dependsOn) {
57+
super(MongoClient.class, MongoClientFactoryBean.class, dependsOn);
58+
}
59+
4360
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/mongo/ReactiveStreamsMongoClientDependsOnBeanFactoryPostProcessor.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,25 @@
3737
public class ReactiveStreamsMongoClientDependsOnBeanFactoryPostProcessor
3838
extends AbstractDependsOnBeanFactoryPostProcessor {
3939

40+
/**
41+
* Creates a new {@code ReactiveStreamsMongoClientDependsOnBeanFactoryPostProcessor}
42+
* that will set up dependencies upon beans with the given names.
43+
* @param dependsOn names of the beans to depend upon
44+
* @deprecated since 2.1.7 in favor of
45+
* {@link #ReactiveStreamsMongoClientDependsOnBeanFactoryPostProcessor}
46+
*/
47+
@Deprecated
4048
public ReactiveStreamsMongoClientDependsOnBeanFactoryPostProcessor(String... dependsOn) {
4149
super(MongoClient.class, ReactiveMongoClientFactoryBean.class, dependsOn);
4250
}
4351

52+
/**
53+
* Creates a new {@code ReactiveStreamsMongoClientDependsOnBeanFactoryPostProcessor}
54+
* that will set up dependencies upon beans with the given types.
55+
* @param dependsOn types of the beans to depend upon
56+
*/
57+
public ReactiveStreamsMongoClientDependsOnBeanFactoryPostProcessor(Class<?>... dependsOn) {
58+
super(MongoClient.class, ReactiveMongoClientFactoryBean.class, dependsOn);
59+
}
60+
4461
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoAutoConfiguration.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
* @author Andy Wilkinson
7575
* @author Yogesh Lonkar
7676
* @author Mark Paluch
77+
* @author Issam El-atif
7778
* @since 1.3.0
7879
*/
7980
@Configuration
@@ -210,30 +211,31 @@ private ArtifactStoreBuilder getArtifactStore(Logger logger) {
210211
}
211212

212213
/**
213-
* Additional configuration to ensure that {@link MongoClient} beans depend on the
214-
* {@code embeddedMongoServer} bean.
214+
* Additional configuration to ensure that {@link MongoClient} beans depend on any
215+
* {@link MongodExecutable} beans.
215216
*/
216217
@Configuration
217218
@ConditionalOnClass({ MongoClient.class, MongoClientFactoryBean.class })
218219
protected static class EmbeddedMongoDependencyConfiguration extends MongoClientDependsOnBeanFactoryPostProcessor {
219220

220-
public EmbeddedMongoDependencyConfiguration() {
221-
super("embeddedMongoServer");
221+
EmbeddedMongoDependencyConfiguration() {
222+
super(MongodExecutable.class);
222223
}
223224

224225
}
225226

226227
/**
227-
* Additional configuration to ensure that {@link MongoClient} beans depend on the
228-
* {@code embeddedMongoServer} bean.
228+
* Additional configuration to ensure that
229+
* {@link com.mongodb.reactivestreams.client.MongoClient} beans depend on any
230+
* {@link MongodExecutable} beans.
229231
*/
230232
@Configuration
231233
@ConditionalOnClass({ com.mongodb.reactivestreams.client.MongoClient.class, ReactiveMongoClientFactoryBean.class })
232234
protected static class EmbeddedReactiveMongoDependencyConfiguration
233235
extends ReactiveStreamsMongoClientDependsOnBeanFactoryPostProcessor {
234236

235-
public EmbeddedReactiveMongoDependencyConfiguration() {
236-
super("embeddedMongoServer");
237+
EmbeddedReactiveMongoDependencyConfiguration() {
238+
super(MongodExecutable.class);
237239
}
238240

239241
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoAutoConfigurationTests.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,23 @@
1818

1919
import java.io.File;
2020
import java.util.EnumSet;
21+
import java.util.Map;
2122
import java.util.stream.Collectors;
2223

2324
import com.mongodb.MongoClient;
2425
import de.flapdoodle.embed.mongo.MongodExecutable;
26+
import de.flapdoodle.embed.mongo.MongodStarter;
2527
import de.flapdoodle.embed.mongo.config.IMongodConfig;
2628
import de.flapdoodle.embed.mongo.config.Storage;
2729
import de.flapdoodle.embed.mongo.distribution.Feature;
2830
import de.flapdoodle.embed.mongo.distribution.Version;
31+
import de.flapdoodle.embed.process.config.IRuntimeConfig;
2932
import org.bson.Document;
3033
import org.junit.After;
3134
import org.junit.Test;
3235

3336
import org.springframework.beans.factory.annotation.Value;
37+
import org.springframework.beans.factory.config.BeanDefinition;
3438
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
3539
import org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration;
3640
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
@@ -50,6 +54,7 @@
5054
* @author Henryk Konsek
5155
* @author Andy Wilkinson
5256
* @author Stephane Nicoll
57+
* @author Issam El-atif
5358
*/
5459
public class EmbeddedMongoAutoConfigurationTests {
5560

@@ -171,6 +176,16 @@ public void shutdownHookIsNotRegistered() {
171176
assertThat(this.context.getBean(MongodExecutable.class).isRegisteredJobKiller()).isFalse();
172177
}
173178

179+
@Test
180+
public void customMongoServerConfiguration() {
181+
load(CustomMongoConfiguration.class);
182+
Map<String, MongoClient> mongoClients = this.context.getBeansOfType(MongoClient.class);
183+
for (String mongoClientBeanName : mongoClients.keySet()) {
184+
BeanDefinition beanDefinition = this.context.getBeanFactory().getBeanDefinition(mongoClientBeanName);
185+
assertThat(beanDefinition.getDependsOn()).contains("customMongoServer");
186+
}
187+
}
188+
174189
private void assertVersionConfiguration(String configuredVersion, String expectedVersion) {
175190
this.context = new AnnotationConfigApplicationContext();
176191
TestPropertyValues.of("spring.data.mongodb.port=0").applyTo(this.context);
@@ -216,4 +231,15 @@ public MongoClient mongoClient(@Value("${local.mongo.port}") int port) {
216231

217232
}
218233

234+
@Configuration
235+
static class CustomMongoConfiguration {
236+
237+
@Bean(initMethod = "start", destroyMethod = "stop")
238+
public MongodExecutable customMongoServer(IRuntimeConfig runtimeConfig, IMongodConfig mongodConfig) {
239+
MongodStarter mongodStarter = MongodStarter.getInstance(runtimeConfig);
240+
return mongodStarter.prepare(mongodConfig);
241+
}
242+
243+
}
244+
219245
}

0 commit comments

Comments
 (0)