Skip to content

Commit 1abe0f1

Browse files
committed
Apply gridFsDatabase to auto-configured ReactiveGridFsTemplate
Closes gh-18284
1 parent d5246f1 commit 1abe0f1

File tree

2 files changed

+105
-5
lines changed

2 files changed

+105
-5
lines changed

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

Lines changed: 82 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,15 @@
1616

1717
package org.springframework.boot.autoconfigure.data.mongo;
1818

19+
import java.util.Optional;
20+
21+
import com.mongodb.ClientSessionOptions;
22+
import com.mongodb.reactivestreams.client.ClientSession;
1923
import com.mongodb.reactivestreams.client.MongoClient;
24+
import com.mongodb.reactivestreams.client.MongoDatabase;
25+
import org.bson.codecs.Codec;
26+
import org.bson.codecs.configuration.CodecRegistry;
27+
import reactor.core.publisher.Mono;
2028

2129
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
2230
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
@@ -31,6 +39,8 @@
3139
import org.springframework.context.annotation.Import;
3240
import org.springframework.core.io.buffer.DataBufferFactory;
3341
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
42+
import org.springframework.dao.DataAccessException;
43+
import org.springframework.dao.support.PersistenceExceptionTranslator;
3444
import org.springframework.data.mongodb.ReactiveMongoDatabaseFactory;
3545
import org.springframework.data.mongodb.core.ReactiveMongoOperations;
3646
import org.springframework.data.mongodb.core.ReactiveMongoTemplate;
@@ -42,6 +52,7 @@
4252
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
4353
import org.springframework.data.mongodb.gridfs.ReactiveGridFsOperations;
4454
import org.springframework.data.mongodb.gridfs.ReactiveGridFsTemplate;
55+
import org.springframework.util.StringUtils;
4556

4657
/**
4758
* {@link EnableAutoConfiguration Auto-configuration} for Spring Data's reactive mongo
@@ -98,8 +109,77 @@ public DefaultDataBufferFactory dataBufferFactory() {
98109
@Bean
99110
@ConditionalOnMissingBean(ReactiveGridFsOperations.class)
100111
public ReactiveGridFsTemplate reactiveGridFsTemplate(ReactiveMongoDatabaseFactory reactiveMongoDatabaseFactory,
101-
MappingMongoConverter mappingMongoConverter, DataBufferFactory dataBufferFactory) {
102-
return new ReactiveGridFsTemplate(dataBufferFactory, reactiveMongoDatabaseFactory, mappingMongoConverter, null);
112+
MappingMongoConverter mappingMongoConverter, DataBufferFactory dataBufferFactory,
113+
MongoProperties properties) {
114+
return new ReactiveGridFsTemplate(dataBufferFactory,
115+
new GridFsReactiveMongoDatabaseFactory(reactiveMongoDatabaseFactory, properties), mappingMongoConverter,
116+
null);
117+
}
118+
119+
/**
120+
* {@link ReactiveMongoDatabaseFactory} decorator to use
121+
* {@link MongoProperties#getGridFsDatabase()} when set.
122+
*/
123+
static class GridFsReactiveMongoDatabaseFactory implements ReactiveMongoDatabaseFactory {
124+
125+
private final ReactiveMongoDatabaseFactory delegate;
126+
127+
private final MongoProperties properties;
128+
129+
GridFsReactiveMongoDatabaseFactory(ReactiveMongoDatabaseFactory delegate, MongoProperties properties) {
130+
this.delegate = delegate;
131+
this.properties = properties;
132+
}
133+
134+
@Override
135+
public boolean hasCodecFor(Class<?> type) {
136+
return this.delegate.hasCodecFor(type);
137+
}
138+
139+
@Override
140+
public Mono<MongoDatabase> getMongoDatabase() throws DataAccessException {
141+
String gridFsDatabase = this.properties.getGridFsDatabase();
142+
if (StringUtils.hasText(gridFsDatabase)) {
143+
return this.delegate.getMongoDatabase(gridFsDatabase);
144+
}
145+
return this.delegate.getMongoDatabase();
146+
}
147+
148+
@Override
149+
public Mono<MongoDatabase> getMongoDatabase(String dbName) throws DataAccessException {
150+
return this.delegate.getMongoDatabase(dbName);
151+
}
152+
153+
@Override
154+
public <T> Optional<Codec<T>> getCodecFor(Class<T> type) {
155+
return this.delegate.getCodecFor(type);
156+
}
157+
158+
@Override
159+
public PersistenceExceptionTranslator getExceptionTranslator() {
160+
return this.delegate.getExceptionTranslator();
161+
}
162+
163+
@Override
164+
public CodecRegistry getCodecRegistry() {
165+
return this.delegate.getCodecRegistry();
166+
}
167+
168+
@Override
169+
public Mono<ClientSession> getSession(ClientSessionOptions options) {
170+
return this.delegate.getSession(options);
171+
}
172+
173+
@Override
174+
public ReactiveMongoDatabaseFactory withSession(ClientSession session) {
175+
return this.delegate.withSession(session);
176+
}
177+
178+
@Override
179+
public boolean isTransactionActive() {
180+
return this.delegate.isTransactionActive();
181+
}
182+
103183
}
104184

105185
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/mongo/MongoReactiveDataAutoConfigurationTests.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,9 +21,12 @@
2121
import org.springframework.boot.autoconfigure.AutoConfigurations;
2222
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
2323
import org.springframework.boot.autoconfigure.mongo.MongoReactiveAutoConfiguration;
24+
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
2425
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
26+
import org.springframework.data.mongodb.ReactiveMongoDatabaseFactory;
2527
import org.springframework.data.mongodb.core.ReactiveMongoTemplate;
2628
import org.springframework.data.mongodb.gridfs.ReactiveGridFsTemplate;
29+
import org.springframework.test.util.ReflectionTestUtils;
2730

2831
import static org.assertj.core.api.Assertions.assertThat;
2932

@@ -45,8 +48,17 @@ void templateExists() {
4548
}
4649

4750
@Test
48-
void gridFsTemplateExists() {
49-
this.contextRunner.run((context) -> assertThat(context).hasSingleBean(ReactiveGridFsTemplate.class));
51+
void whenNoGridFsDatabaseIsConfiguredTheGridFsTemplateUsesTheMainDatabase() {
52+
this.contextRunner.run((context) -> {
53+
assertThat(grisFsTemplateDatabaseName(context)).isEqualTo("test");
54+
});
55+
}
56+
57+
@Test
58+
void whenGridFsDatabaseIsConfiguredThenGridFsTemplateUsesIt() {
59+
this.contextRunner.withPropertyValues("spring.data.mongodb.gridFsDatabase:grid").run((context) -> {
60+
assertThat(grisFsTemplateDatabaseName(context)).isEqualTo("grid");
61+
});
5062
}
5163

5264
@Test
@@ -56,4 +68,12 @@ void backsOffIfMongoClientBeanIsNotPresent() {
5668
runner.run((context) -> assertThat(context).doesNotHaveBean(MongoReactiveDataAutoConfiguration.class));
5769
}
5870

71+
private String grisFsTemplateDatabaseName(AssertableApplicationContext context) {
72+
assertThat(context).hasSingleBean(ReactiveGridFsTemplate.class);
73+
ReactiveGridFsTemplate template = context.getBean(ReactiveGridFsTemplate.class);
74+
ReactiveMongoDatabaseFactory factory = (ReactiveMongoDatabaseFactory) ReflectionTestUtils.getField(template,
75+
"dbFactory");
76+
return factory.getMongoDatabase().block().getName();
77+
}
78+
5979
}

0 commit comments

Comments
 (0)