diff --git a/pom.xml b/pom.xml
index 018dd48e27..3887e15377 100644
--- a/pom.xml
+++ b/pom.xml
@@ -5,7 +5,7 @@
org.springframework.data
spring-data-mongodb-parent
- 4.2.0-SNAPSHOT
+ 4.2.x-1627-SNAPSHOT
pom
Spring Data MongoDB
diff --git a/spring-data-mongodb-benchmarks/pom.xml b/spring-data-mongodb-benchmarks/pom.xml
index 2de4b6b635..38a7ca6c04 100644
--- a/spring-data-mongodb-benchmarks/pom.xml
+++ b/spring-data-mongodb-benchmarks/pom.xml
@@ -7,7 +7,7 @@
org.springframework.data
spring-data-mongodb-parent
- 4.2.0-SNAPSHOT
+ 4.2.x-1627-SNAPSHOT
../pom.xml
diff --git a/spring-data-mongodb-distribution/pom.xml b/spring-data-mongodb-distribution/pom.xml
index 3bc1ab9df2..4cc7af18d6 100644
--- a/spring-data-mongodb-distribution/pom.xml
+++ b/spring-data-mongodb-distribution/pom.xml
@@ -15,7 +15,7 @@
org.springframework.data
spring-data-mongodb-parent
- 4.2.0-SNAPSHOT
+ 4.2.x-1627-SNAPSHOT
../pom.xml
diff --git a/spring-data-mongodb/pom.xml b/spring-data-mongodb/pom.xml
index 921254ca44..77626723d5 100644
--- a/spring-data-mongodb/pom.xml
+++ b/spring-data-mongodb/pom.xml
@@ -13,7 +13,7 @@
org.springframework.data
spring-data-mongodb-parent
- 4.2.0-SNAPSHOT
+ 4.2.x-1627-SNAPSHOT
../pom.xml
diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/gridfs/GridFsTemplate.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/gridfs/GridFsTemplate.java
index ebc72fb6bf..b9a6089c33 100644
--- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/gridfs/GridFsTemplate.java
+++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/gridfs/GridFsTemplate.java
@@ -22,6 +22,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
+import java.util.function.Supplier;
import org.bson.Document;
import org.bson.types.ObjectId;
@@ -30,6 +31,7 @@
import org.springframework.data.mongodb.core.convert.MongoConverter;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.util.BsonUtils;
+import org.springframework.data.util.Lazy;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
@@ -56,9 +58,7 @@
*/
public class GridFsTemplate extends GridFsOperationsSupport implements GridFsOperations, ResourcePatternResolver {
- private final MongoDatabaseFactory dbFactory;
-
- private final @Nullable String bucket;
+ private Supplier bucketSupplier;
/**
* Creates a new {@link GridFsTemplate} using the given {@link MongoDatabaseFactory} and {@link MongoConverter}.
@@ -78,13 +78,22 @@ public GridFsTemplate(MongoDatabaseFactory dbFactory, MongoConverter converter)
* @param bucket can be {@literal null}.
*/
public GridFsTemplate(MongoDatabaseFactory dbFactory, MongoConverter converter, @Nullable String bucket) {
+ this(converter, Lazy.of(() -> getGridFs(dbFactory, bucket)));
+ }
- super(converter);
+ /**
+ * Creates a new {@link GridFsTemplate} using the given {@link MongoConverter} and {@link Supplier} providing the
+ * required {@link GridFSBucket}.
+ *
+ * @param converter must not be {@literal null}.
+ * @param gridFSBucket must not be {@literal null}.
+ * @since 4.2
+ */
+ public GridFsTemplate(MongoConverter converter, Supplier gridFSBucket) {
- Assert.notNull(dbFactory, "MongoDbFactory must not be null");
+ super(converter);
- this.dbFactory = dbFactory;
- this.bucket = bucket;
+ this.bucketSupplier = gridFSBucket;
}
public ObjectId store(InputStream content, @Nullable String filename, @Nullable String contentType,
@@ -142,7 +151,7 @@ public void delete(Query query) {
}
public ClassLoader getClassLoader() {
- return dbFactory.getClass().getClassLoader();
+ return this.getClassLoader();
}
public GridFsResource getResource(String location) {
@@ -182,7 +191,11 @@ public GridFsResource[] getResources(String locationPattern) {
return new GridFsResource[] { getResource(locationPattern) };
}
- private GridFSBucket getGridFs() {
+ GridFSBucket getGridFs() {
+ return this.bucketSupplier.get();
+ }
+
+ private static GridFSBucket getGridFs(MongoDatabaseFactory dbFactory, @Nullable String bucket) {
MongoDatabase db = dbFactory.getMongoDatabase();
return bucket == null ? GridFSBuckets.create(db) : GridFSBuckets.create(db, bucket);
diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/gridfs/ReactiveGridFsTemplate.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/gridfs/ReactiveGridFsTemplate.java
index c71a1d4718..0c385d1b62 100644
--- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/gridfs/ReactiveGridFsTemplate.java
+++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/gridfs/ReactiveGridFsTemplate.java
@@ -27,7 +27,6 @@
import org.bson.Document;
import org.bson.types.ObjectId;
import org.reactivestreams.Publisher;
-
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
@@ -37,6 +36,7 @@
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.SerializationUtils;
import org.springframework.data.mongodb.util.BsonUtils;
+import org.springframework.data.util.Lazy;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
@@ -61,9 +61,8 @@
*/
public class ReactiveGridFsTemplate extends GridFsOperationsSupport implements ReactiveGridFsOperations {
- private final ReactiveMongoDatabaseFactory dbFactory;
private final DataBufferFactory dataBufferFactory;
- private final @Nullable String bucket;
+ private Mono bucketSupplier;
/**
* Creates a new {@link ReactiveGridFsTemplate} using the given {@link ReactiveMongoDatabaseFactory} and
@@ -100,15 +99,27 @@ public ReactiveGridFsTemplate(ReactiveMongoDatabaseFactory dbFactory, MongoConve
*/
public ReactiveGridFsTemplate(DataBufferFactory dataBufferFactory, ReactiveMongoDatabaseFactory dbFactory,
MongoConverter converter, @Nullable String bucket) {
+ this(converter, Mono.defer(Lazy.of(() -> doGetBucket(dbFactory, bucket))), dataBufferFactory);
+ }
+
+ /**
+ * Creates a new {@link ReactiveGridFsTemplate} using the given {@link MongoConverter}, {@link Mono} emitting a
+ * {@link ReactiveMongoDatabaseFactory} and {@link DataBufferFactory}.
+ *
+ * @param converter must not be {@literal null}.
+ * @param gridFSBucket must not be {@literal null}.
+ * @param dataBufferFactory must not be {@literal null}.
+ * @since 4.2
+ */
+ public ReactiveGridFsTemplate(MongoConverter converter, Mono gridFSBucket,
+ DataBufferFactory dataBufferFactory) {
super(converter);
Assert.notNull(dataBufferFactory, "DataBufferFactory must not be null");
- Assert.notNull(dbFactory, "ReactiveMongoDatabaseFactory must not be null");
+ this.bucketSupplier = gridFSBucket;
this.dataBufferFactory = dataBufferFactory;
- this.dbFactory = dbFactory;
- this.bucket = bucket;
}
@Override
@@ -247,11 +258,15 @@ public Flux createFlux(ReactiveBucketCallback callback) {
return doGetBucket().flatMapMany(callback::doInBucket);
}
- protected Mono doGetBucket() {
+ static Mono doGetBucket(ReactiveMongoDatabaseFactory dbFactory, @Nullable String bucket) {
return dbFactory.getMongoDatabase()
.map(db -> bucket == null ? GridFSBuckets.create(db) : GridFSBuckets.create(db, bucket));
}
+ protected Mono doGetBucket() {
+ return bucketSupplier;
+ }
+
/**
* @param
* @author Mathieu Ouellet
diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/config/MongoNamespaceTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/config/MongoNamespaceTests.java
index 7604a7d6c7..15d5b9443f 100644
--- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/config/MongoNamespaceTests.java
+++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/config/MongoNamespaceTests.java
@@ -20,6 +20,11 @@
import javax.net.ssl.SSLSocketFactory;
+import java.util.function.Supplier;
+
+import com.mongodb.client.MongoCollection;
+import com.mongodb.client.gridfs.GridFSBucket;
+import com.mongodb.client.gridfs.model.GridFSFile;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
@@ -168,8 +173,12 @@ public void testGridFsTemplateFactory() {
assertThat(ctx.containsBean("gridFsTemplate")).isTrue();
GridFsOperations operations = (GridFsOperations) ctx.getBean("gridFsTemplate");
- MongoDatabaseFactory dbf = (MongoDatabaseFactory) getField(operations, "dbFactory");
- assertThat(getField(dbf, "databaseName")).isEqualTo("database");
+ Supplier gridFSBucketSupplier = (Supplier) getField(operations, "bucketSupplier");
+ GridFSBucket gfsBucket = gridFSBucketSupplier.get();
+ assertThat(gfsBucket.getBucketName()).isEqualTo("fs"); // fs is the default
+
+ MongoCollection filesCollection = (MongoCollection) getField(gfsBucket, "filesCollection");
+ assertThat(filesCollection.getNamespace().getDatabaseName()).isEqualTo("database");
MongoConverter converter = (MongoConverter) getField(operations, "converter");
assertThat(converter).isNotNull();
@@ -181,9 +190,12 @@ public void testSecondGridFsTemplateFactory() {
assertThat(ctx.containsBean("secondGridFsTemplate")).isTrue();
GridFsOperations operations = (GridFsOperations) ctx.getBean("secondGridFsTemplate");
- MongoDatabaseFactory dbf = (MongoDatabaseFactory) getField(operations, "dbFactory");
- assertThat(getField(dbf, "databaseName")).isEqualTo("database");
- assertThat(getField(operations, "bucket")).isEqualTo(null);
+ Supplier gridFSBucketSupplier = (Supplier) getField(operations, "bucketSupplier");
+ GridFSBucket gfsBucket = gridFSBucketSupplier.get();
+ assertThat(gfsBucket.getBucketName()).isEqualTo("fs"); // fs is the default
+
+ MongoCollection filesCollection = (MongoCollection) getField(gfsBucket, "filesCollection");
+ assertThat(filesCollection.getNamespace().getDatabaseName()).isEqualTo("database");
MongoConverter converter = (MongoConverter) getField(operations, "converter");
assertThat(converter).isNotNull();
@@ -195,9 +207,12 @@ public void testThirdGridFsTemplateFactory() {
assertThat(ctx.containsBean("thirdGridFsTemplate")).isTrue();
GridFsOperations operations = (GridFsOperations) ctx.getBean("thirdGridFsTemplate");
- MongoDatabaseFactory dbf = (MongoDatabaseFactory) getField(operations, "dbFactory");
- assertThat(getField(dbf, "databaseName")).isEqualTo("database");
- assertThat(getField(operations, "bucket")).isEqualTo("bucketString");
+ Supplier gridFSBucketSupplier = (Supplier) getField(operations, "bucketSupplier");
+ GridFSBucket gfsBucket = gridFSBucketSupplier.get();
+ assertThat(gfsBucket.getBucketName()).isEqualTo("bucketString"); // fs is the default
+
+ MongoCollection filesCollection = (MongoCollection) getField(gfsBucket, "filesCollection");
+ assertThat(filesCollection.getNamespace().getDatabaseName()).isEqualTo("database");
MongoConverter converter = (MongoConverter) getField(operations, "converter");
assertThat(converter).isNotNull();