Skip to content

Commit 2d36fc3

Browse files
mp911dechristophstrobl
authored andcommitted
DATAMONGO-1919 - Allow reactive-only usage of ReactiveMongoTemplate.
We now support reactive-only usage of Spring Data MongoDB without the need to use the synchronous driver or even having it on the class path. We conditionally register CodeWScope/CodeWithScope types that are bundled with the particular driver distributions. NoOpDbRefResolver is now a top-level type to be used with a reactive-only MongoMappingContext. Original Pull Request: #572
1 parent 78c2ab2 commit 2d36fc3

File tree

8 files changed

+111
-81
lines changed

8 files changed

+111
-81
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/AbstractReactiveMongoConfiguration.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.springframework.data.mongodb.core.ReactiveMongoTemplate;
2323
import org.springframework.data.mongodb.core.SimpleReactiveMongoDatabaseFactory;
2424
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
25+
import org.springframework.data.mongodb.core.convert.NoOpDbRefResolver;
2526

2627
import com.mongodb.reactivestreams.client.MongoClient;
2728

@@ -80,7 +81,7 @@ public ReactiveMongoDatabaseFactory reactiveMongoDbFactory() {
8081
@Bean
8182
public MappingMongoConverter mappingMongoConverter() throws Exception {
8283

83-
MappingMongoConverter converter = new MappingMongoConverter(ReactiveMongoTemplate.NO_OP_REF_RESOLVER,
84+
MappingMongoConverter converter = new MappingMongoConverter(NoOpDbRefResolver.INSTANCE,
8485
mongoMappingContext());
8586
converter.setCustomConversions(customConversions());
8687

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReactiveMongoTemplate.java

Lines changed: 11 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@
3232
import java.util.function.Function;
3333
import java.util.stream.Collectors;
3434

35-
import javax.annotation.Nonnull;
36-
3735
import org.bson.BsonTimestamp;
3836
import org.bson.BsonValue;
3937
import org.bson.Document;
@@ -77,7 +75,16 @@
7775
import org.springframework.data.mongodb.core.aggregation.PrefixingDelegatingAggregationOperationContext;
7876
import org.springframework.data.mongodb.core.aggregation.TypeBasedAggregationOperationContext;
7977
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
80-
import org.springframework.data.mongodb.core.convert.*;
78+
import org.springframework.data.mongodb.core.convert.DbRefResolver;
79+
import org.springframework.data.mongodb.core.convert.JsonSchemaMapper;
80+
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
81+
import org.springframework.data.mongodb.core.convert.MongoConverter;
82+
import org.springframework.data.mongodb.core.convert.MongoCustomConversions;
83+
import org.springframework.data.mongodb.core.convert.MongoJsonSchemaMapper;
84+
import org.springframework.data.mongodb.core.convert.MongoWriter;
85+
import org.springframework.data.mongodb.core.convert.NoOpDbRefResolver;
86+
import org.springframework.data.mongodb.core.convert.QueryMapper;
87+
import org.springframework.data.mongodb.core.convert.UpdateMapper;
8188
import org.springframework.data.mongodb.core.index.MongoMappingEventPublisher;
8289
import org.springframework.data.mongodb.core.index.ReactiveIndexOperations;
8390
import org.springframework.data.mongodb.core.index.ReactiveMongoPersistentEntityIndexCreator;
@@ -96,7 +103,6 @@
96103
import org.springframework.data.mongodb.core.mapreduce.MapReduceOptions;
97104
import org.springframework.data.mongodb.core.query.Collation;
98105
import org.springframework.data.mongodb.core.query.Criteria;
99-
import org.springframework.data.mongodb.core.query.CriteriaDefinition;
100106
import org.springframework.data.mongodb.core.query.Meta;
101107
import org.springframework.data.mongodb.core.query.NearQuery;
102108
import org.springframework.data.mongodb.core.query.Query;
@@ -119,7 +125,6 @@
119125
import com.mongodb.CursorType;
120126
import com.mongodb.DBCollection;
121127
import com.mongodb.DBCursor;
122-
import com.mongodb.DBRef;
123128
import com.mongodb.Mongo;
124129
import com.mongodb.MongoException;
125130
import com.mongodb.ReadPreference;
@@ -156,7 +161,7 @@
156161
*/
157162
public class ReactiveMongoTemplate implements ReactiveMongoOperations, ApplicationContextAware {
158163

159-
public static final DbRefResolver NO_OP_REF_RESOLVER = new NoOpDbRefResolver();
164+
public static final DbRefResolver NO_OP_REF_RESOLVER = NoOpDbRefResolver.INSTANCE;
160165

161166
private static final Logger LOGGER = LoggerFactory.getLogger(ReactiveMongoTemplate.class);
162167
private static final String ID_FIELD = "_id";
@@ -3287,54 +3292,6 @@ private static List<? extends Document> toDocuments(final Collection<? extends D
32873292
return new ArrayList<>(documents);
32883293
}
32893294

3290-
/**
3291-
* No-Operation {@link org.springframework.data.mongodb.core.mapping.DBRef} resolver.
3292-
*
3293-
* @author Mark Paluch
3294-
*/
3295-
static class NoOpDbRefResolver implements DbRefResolver {
3296-
3297-
/*
3298-
* (non-Javadoc)
3299-
* @see org.springframework.data.mongodb.core.convert.DbRefResolver#resolveDbRef(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty, org.springframework.data.mongodb.core.convert.DbRefResolverCallback)
3300-
*/
3301-
@Override
3302-
@Nullable
3303-
public Object resolveDbRef(@Nonnull MongoPersistentProperty property, @Nonnull DBRef dbref,
3304-
@Nonnull DbRefResolverCallback callback, @Nonnull DbRefProxyHandler proxyHandler) {
3305-
return null;
3306-
}
3307-
3308-
/*
3309-
* (non-Javadoc)
3310-
* @see org.springframework.data.mongodb.core.convert.DbRefResolver#created(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty, org.springframework.data.mongodb.core.mapping.MongoPersistentEntity, java.lang.Object)
3311-
*/
3312-
@Override
3313-
@Nullable
3314-
public DBRef createDbRef(org.springframework.data.mongodb.core.mapping.DBRef annotation,
3315-
MongoPersistentEntity<?> entity, Object id) {
3316-
return null;
3317-
}
3318-
3319-
/*
3320-
* (non-Javadoc)
3321-
* @see org.springframework.data.mongodb.core.convert.DbRefResolver#fetch(com.mongodb.DBRef)
3322-
*/
3323-
@Override
3324-
public Document fetch(DBRef dbRef) {
3325-
return null;
3326-
}
3327-
3328-
/*
3329-
* (non-Javadoc)
3330-
* @see org.springframework.data.mongodb.core.convert.DbRefResolver#bulkFetch(java.util.List)
3331-
*/
3332-
@Override
3333-
public List<Document> bulkFetch(List<DBRef> dbRefs) {
3334-
return Collections.emptyList();
3335-
}
3336-
}
3337-
33383295
/**
33393296
* {@link MongoTemplate} extension bound to a specific {@link ClientSession} that is applied when interacting with the
33403297
* server through the driver API.

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/DbRefResolver.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
2323
import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;
2424
import org.springframework.lang.Nullable;
25+
import org.springframework.util.StringUtils;
2526

2627
import com.mongodb.DBRef;
2728

@@ -59,9 +60,15 @@ Object resolveDbRef(MongoPersistentProperty property, @Nullable DBRef dbref, DbR
5960
* @param id will never be {@literal null}.
6061
* @return
6162
*/
62-
DBRef createDbRef(@Nullable org.springframework.data.mongodb.core.mapping.DBRef annotation,
63-
MongoPersistentEntity<?> entity,
64-
Object id);
63+
default DBRef createDbRef(@Nullable org.springframework.data.mongodb.core.mapping.DBRef annotation,
64+
MongoPersistentEntity<?> entity, Object id) {
65+
66+
if (annotation != null && StringUtils.hasText(annotation.db())) {
67+
return new DBRef(annotation.db(), entity.getCollection(), id);
68+
}
69+
70+
return new DBRef(entity.getCollection(), id);
71+
}
6572

6673
/**
6774
* Actually loads the {@link DBRef} from the datasource.

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/DefaultDbRefResolver.java

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -104,21 +104,6 @@ public Object resolveDbRef(MongoPersistentProperty property, @Nullable DBRef dbr
104104
return callback.resolve(property);
105105
}
106106

107-
/*
108-
* (non-Javadoc)
109-
* @see org.springframework.data.mongodb.core.convert.DbRefResolver#created(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty, org.springframework.data.mongodb.core.mapping.MongoPersistentEntity, java.lang.Object)
110-
*/
111-
@Override
112-
public DBRef createDbRef(@Nullable org.springframework.data.mongodb.core.mapping.DBRef annotation,
113-
MongoPersistentEntity<?> entity, Object id) {
114-
115-
if (annotation != null && StringUtils.hasText(annotation.db())) {
116-
return new DBRef(annotation.db(), entity.getCollection(), id);
117-
}
118-
119-
return new DBRef(entity.getCollection(), id);
120-
}
121-
122107
/*
123108
* (non-Javadoc)
124109
* @see org.springframework.data.mongodb.core.convert.DbRefResolver#fetch(com.mongodb.DBRef)
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright 2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mongodb.core.convert;
17+
18+
import java.util.List;
19+
20+
import javax.annotation.Nonnull;
21+
22+
import org.bson.Document;
23+
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
24+
import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty;
25+
import org.springframework.lang.Nullable;
26+
import org.springframework.util.StringUtils;
27+
28+
import com.mongodb.DBRef;
29+
30+
/**
31+
* No-Operation {@link org.springframework.data.mongodb.core.mapping.DBRef} resolver throwing
32+
* {@link UnsupportedOperationException} when attempting to resolve database references.
33+
*
34+
* @author Mark Paluch
35+
* @since 2.1
36+
*/
37+
public enum NoOpDbRefResolver implements DbRefResolver {
38+
39+
INSTANCE;
40+
41+
/*
42+
* (non-Javadoc)
43+
* @see org.springframework.data.mongodb.core.convert.DbRefResolver#resolveDbRef(org.springframework.data.mongodb.core.mapping.MongoPersistentProperty, org.springframework.data.mongodb.core.convert.DbRefResolverCallback)
44+
*/
45+
@Override
46+
@Nullable
47+
public Object resolveDbRef(@Nonnull MongoPersistentProperty property, @Nonnull DBRef dbref,
48+
@Nonnull DbRefResolverCallback callback, @Nonnull DbRefProxyHandler proxyHandler) {
49+
throw new UnsupportedOperationException("DBRef resolution not supported!");
50+
}
51+
52+
/*
53+
* (non-Javadoc)
54+
* @see org.springframework.data.mongodb.core.convert.DbRefResolver#fetch(com.mongodb.DBRef)
55+
*/
56+
@Override
57+
public Document fetch(DBRef dbRef) {
58+
throw new UnsupportedOperationException("DBRef resolution not supported!");
59+
}
60+
61+
/*
62+
* (non-Javadoc)
63+
* @see org.springframework.data.mongodb.core.convert.DbRefResolver#bulkFetch(java.util.List)
64+
*/
65+
@Override
66+
public List<Document> bulkFetch(List<DBRef> dbRefs) {
67+
throw new UnsupportedOperationException("DBRef resolution not supported!");
68+
}
69+
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/MongoSimpleTypes.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
import org.bson.BsonObjectId;
2626
import org.bson.types.Binary;
27-
import org.bson.types.CodeWScope;
2827
import org.bson.types.ObjectId;
2928
import org.springframework.data.mapping.model.SimpleTypeHolder;
3029
import org.springframework.data.mongodb.util.MongoClientVersion;
@@ -37,6 +36,7 @@
3736
*
3837
* @author Oliver Gierke
3938
* @author Christoph Strobl
39+
* @author Mark Paluch
4040
*/
4141
public abstract class MongoSimpleTypes {
4242

@@ -53,15 +53,21 @@ public abstract class MongoSimpleTypes {
5353
simpleTypes.add(DBRef.class);
5454
simpleTypes.add(ObjectId.class);
5555
simpleTypes.add(BsonObjectId.class);
56-
simpleTypes.add(CodeWScope.class);
5756
simpleTypes.add(org.bson.Document.class);
5857
simpleTypes.add(Pattern.class);
5958
simpleTypes.add(Binary.class);
6059
simpleTypes.add(UUID.class);
6160

61+
if (ClassUtils.isPresent("org.bson.types.CodeWScope", MongoSimpleTypes.class.getClassLoader())) {
62+
simpleTypes.add(resolveClassName("org.bson.types.CodeWScope"));
63+
}
64+
65+
if (ClassUtils.isPresent("org.bson.types.CodeWithScope", MongoSimpleTypes.class.getClassLoader())) {
66+
simpleTypes.add(resolveClassName("org.bson.types.CodeWithScope"));
67+
}
68+
6269
if (MongoClientVersion.isMongo34Driver()) {
63-
simpleTypes
64-
.add(ClassUtils.resolveClassName("org.bson.types.Decimal128", MongoSimpleTypes.class.getClassLoader()));
70+
simpleTypes.add(resolveClassName("org.bson.types.Decimal128"));
6571
}
6672

6773
MONGO_SIMPLE_TYPES = Collections.unmodifiableSet(simpleTypes);
@@ -71,4 +77,8 @@ public abstract class MongoSimpleTypes {
7177
public static final SimpleTypeHolder HOLDER = new SimpleTypeHolder(MONGO_SIMPLE_TYPES, true);
7278

7379
private MongoSimpleTypes() {}
80+
81+
private static Class<?> resolveClassName(String className) {
82+
return ClassUtils.resolveClassName(className, MongoSimpleTypes.class.getClassLoader());
83+
}
7484
}

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/ReactiveMongoTemplateUnitTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@
4141
import org.springframework.beans.factory.annotation.Value;
4242
import org.springframework.data.annotation.Id;
4343
import org.springframework.data.mongodb.core.MongoTemplateUnitTests.AutogenerateableId;
44-
import org.springframework.data.mongodb.core.ReactiveMongoTemplate.NoOpDbRefResolver;
4544
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
45+
import org.springframework.data.mongodb.core.convert.NoOpDbRefResolver;
4646
import org.springframework.data.mongodb.core.mapping.Field;
4747
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
4848
import org.springframework.data.mongodb.core.query.BasicQuery;
@@ -107,7 +107,7 @@ public void setUp() {
107107
when(aggregatePublisher.first()).thenReturn(findPublisher);
108108

109109
this.mappingContext = new MongoMappingContext();
110-
this.converter = new MappingMongoConverter(new NoOpDbRefResolver(), mappingContext);
110+
this.converter = new MappingMongoConverter(NoOpDbRefResolver.INSTANCE, mappingContext);
111111
this.template = new ReactiveMongoTemplate(factory, converter);
112112

113113
}

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/ReactiveSessionBoundMongoTemplateUnitTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@
3939
import org.springframework.data.geo.Metrics;
4040
import org.springframework.data.geo.Point;
4141
import org.springframework.data.mongodb.ReactiveMongoDatabaseFactory;
42-
import org.springframework.data.mongodb.core.ReactiveMongoTemplate.NoOpDbRefResolver;
4342
import org.springframework.data.mongodb.core.ReactiveMongoTemplate.ReactiveSessionBoundMongoTemplate;
4443
import org.springframework.data.mongodb.core.aggregation.Aggregation;
4544
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
45+
import org.springframework.data.mongodb.core.convert.NoOpDbRefResolver;
4646
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
4747
import org.springframework.data.mongodb.core.query.NearQuery;
4848
import org.springframework.data.mongodb.core.query.Query;
@@ -64,6 +64,7 @@
6464
* Unit tests for {@link ReactiveSessionBoundMongoTemplate}.
6565
*
6666
* @author Christoph Strobl
67+
* @author Mark Paluch
6768
*/
6869
@SuppressWarnings("unchecked")
6970
@RunWith(MockitoJUnitRunner.Silent.class)
@@ -124,7 +125,7 @@ public void setUp() {
124125
factory = new SimpleReactiveMongoDatabaseFactory(client, "foo");
125126

126127
this.mappingContext = new MongoMappingContext();
127-
this.converter = new MappingMongoConverter(new NoOpDbRefResolver(), mappingContext);
128+
this.converter = new MappingMongoConverter(NoOpDbRefResolver.INSTANCE, mappingContext);
128129
this.template = new ReactiveSessionBoundMongoTemplate(clientSession, new ReactiveMongoTemplate(factory, converter));
129130
}
130131

0 commit comments

Comments
 (0)