15
15
*/
16
16
package org .springframework .data .mongodb .config ;
17
17
18
- import java .util .Collection ;
19
- import java .util .Collections ;
20
- import java .util .HashSet ;
21
- import java .util .Set ;
22
-
23
- import org .springframework .beans .factory .config .BeanDefinition ;
24
18
import org .springframework .context .annotation .Bean ;
25
- import org .springframework .context .annotation .ClassPathScanningCandidateComponentProvider ;
26
19
import org .springframework .context .annotation .Configuration ;
27
- import org .springframework .core .convert .converter .Converter ;
28
- import org .springframework .core .type .filter .AnnotationTypeFilter ;
29
- import org .springframework .data .annotation .Persistent ;
30
20
import org .springframework .data .authentication .UserCredentials ;
31
- import org .springframework .data .mapping .context .MappingContextIsNewStrategyFactory ;
32
- import org .springframework .data .mapping .model .CamelCaseAbbreviatingFieldNamingStrategy ;
33
- import org .springframework .data .mapping .model .FieldNamingStrategy ;
34
- import org .springframework .data .mapping .model .PropertyNameFieldNamingStrategy ;
35
21
import org .springframework .data .mongodb .MongoDbFactory ;
36
22
import org .springframework .data .mongodb .core .MongoTemplate ;
37
23
import org .springframework .data .mongodb .core .SimpleMongoDbFactory ;
38
- import org .springframework .data .mongodb .core .convert .CustomConversions ;
39
24
import org .springframework .data .mongodb .core .convert .DbRefResolver ;
40
25
import org .springframework .data .mongodb .core .convert .DefaultDbRefResolver ;
41
26
import org .springframework .data .mongodb .core .convert .MappingMongoConverter ;
42
27
import org .springframework .data .mongodb .core .mapping .Document ;
43
- import org .springframework .data .mongodb .core .mapping .MongoMappingContext ;
44
- import org .springframework .data .support .CachingIsNewStrategyFactory ;
45
- import org .springframework .data .support .IsNewStrategyFactory ;
46
- import org .springframework .util .ClassUtils ;
47
- import org .springframework .util .StringUtils ;
48
28
49
29
import com .mongodb .Mongo ;
50
30
import com .mongodb .MongoClient ;
57
37
* @author Thomas Darimont
58
38
* @author Ryan Tenney
59
39
* @author Christoph Strobl
40
+ * @author Mark Paluch
41
+ * @see MongoConfigurationSupport
60
42
*/
61
43
@ Configuration
62
- public abstract class AbstractMongoConfiguration {
63
-
64
- /**
65
- * Return the name of the database to connect to.
66
- *
67
- * @return must not be {@literal null}.
68
- */
69
- protected abstract String getDatabaseName ();
44
+ public abstract class AbstractMongoConfiguration extends MongoConfigurationSupport {
70
45
71
46
/**
72
47
* Return the name of the authentication database to use. Defaults to {@literal null} and will turn into the value
@@ -120,7 +95,7 @@ public MongoDbFactory mongoDbFactory() throws Exception {
120
95
* class' (the concrete class, not this one here) by default. So if you have a {@code com.acme.AppConfig} extending
121
96
* {@link AbstractMongoConfiguration} the base package will be considered {@code com.acme} unless the method is
122
97
* overridden to implement alternate behavior.
123
- *
98
+ *
124
99
* @return the base package to scan for mapped {@link Document} classes or {@literal null} to not enable scanning for
125
100
* entities.
126
101
* @deprecated use {@link #getMappingBasePackages()} instead.
@@ -132,20 +107,6 @@ protected String getMappingBasePackage() {
132
107
return mappingBasePackage == null ? null : mappingBasePackage .getName ();
133
108
}
134
109
135
- /**
136
- * Returns the base packages to scan for MongoDB mapped entities at startup. Will return the package name of the
137
- * configuration class' (the concrete class, not this one here) by default. So if you have a
138
- * {@code com.acme.AppConfig} extending {@link AbstractMongoConfiguration} the base package will be considered
139
- * {@code com.acme} unless the method is overridden to implement alternate behavior.
140
- *
141
- * @return the base packages to scan for mapped {@link Document} classes or an empty collection to not enable scanning
142
- * for entities.
143
- * @since 1.10
144
- */
145
- protected Collection <String > getMappingBasePackages () {
146
- return Collections .singleton (getMappingBasePackage ());
147
- }
148
-
149
110
/**
150
111
* Return {@link UserCredentials} to be used when connecting to the MongoDB instance or {@literal null} if none shall
151
112
* be used.
@@ -159,47 +120,6 @@ protected UserCredentials getUserCredentials() {
159
120
return null ;
160
121
}
161
122
162
- /**
163
- * Creates a {@link MongoMappingContext} equipped with entity classes scanned from the mapping base package.
164
- *
165
- * @see #getMappingBasePackage()
166
- * @return
167
- * @throws ClassNotFoundException
168
- */
169
- @ Bean
170
- public MongoMappingContext mongoMappingContext () throws ClassNotFoundException {
171
-
172
- MongoMappingContext mappingContext = new MongoMappingContext ();
173
- mappingContext .setInitialEntitySet (getInitialEntitySet ());
174
- mappingContext .setSimpleTypeHolder (customConversions ().getSimpleTypeHolder ());
175
- mappingContext .setFieldNamingStrategy (fieldNamingStrategy ());
176
-
177
- return mappingContext ;
178
- }
179
-
180
- /**
181
- * Returns a {@link MappingContextIsNewStrategyFactory} wrapped into a {@link CachingIsNewStrategyFactory}.
182
- *
183
- * @return
184
- * @throws ClassNotFoundException
185
- */
186
- @ Bean
187
- public IsNewStrategyFactory isNewStrategyFactory () throws ClassNotFoundException {
188
- return new CachingIsNewStrategyFactory (new MappingContextIsNewStrategyFactory (mongoMappingContext ()));
189
- }
190
-
191
- /**
192
- * Register custom {@link Converter}s in a {@link CustomConversions} object if required. These
193
- * {@link CustomConversions} will be registered with the {@link #mappingMongoConverter()} and
194
- * {@link #mongoMappingContext()}. Returns an empty {@link CustomConversions} instance by default.
195
- *
196
- * @return must not be {@literal null}.
197
- */
198
- @ Bean
199
- public CustomConversions customConversions () {
200
- return new CustomConversions (Collections .emptyList ());
201
- }
202
-
203
123
/**
204
124
* Creates a {@link MappingMongoConverter} using the configured {@link #mongoDbFactory()} and
205
125
* {@link #mongoMappingContext()}. Will get {@link #customConversions()} applied.
@@ -219,79 +139,4 @@ public MappingMongoConverter mappingMongoConverter() throws Exception {
219
139
220
140
return converter ;
221
141
}
222
-
223
- /**
224
- * Scans the mapping base package for classes annotated with {@link Document}. By default, it scans for entities in
225
- * all packages returned by {@link #getMappingBasePackages()}.
226
- *
227
- * @see #getMappingBasePackages()
228
- * @return
229
- * @throws ClassNotFoundException
230
- */
231
- protected Set <Class <?>> getInitialEntitySet () throws ClassNotFoundException {
232
-
233
- Set <Class <?>> initialEntitySet = new HashSet <Class <?>>();
234
-
235
- for (String basePackage : getMappingBasePackages ()) {
236
- initialEntitySet .addAll (scanForEntities (basePackage ));
237
- }
238
-
239
- return initialEntitySet ;
240
- }
241
-
242
- /**
243
- * Scans the given base package for entities, i.e. MongoDB specific types annotated with {@link Document} and
244
- * {@link Persistent}.
245
- *
246
- * @param basePackage must not be {@literal null}.
247
- * @return
248
- * @throws ClassNotFoundException
249
- * @since 1.10
250
- */
251
- protected Set <Class <?>> scanForEntities (String basePackage ) throws ClassNotFoundException {
252
-
253
- if (!StringUtils .hasText (basePackage )) {
254
- return Collections .emptySet ();
255
- }
256
-
257
- Set <Class <?>> initialEntitySet = new HashSet <Class <?>>();
258
-
259
- if (StringUtils .hasText (basePackage )) {
260
-
261
- ClassPathScanningCandidateComponentProvider componentProvider = new ClassPathScanningCandidateComponentProvider (
262
- false );
263
- componentProvider .addIncludeFilter (new AnnotationTypeFilter (Document .class ));
264
- componentProvider .addIncludeFilter (new AnnotationTypeFilter (Persistent .class ));
265
-
266
- for (BeanDefinition candidate : componentProvider .findCandidateComponents (basePackage )) {
267
-
268
- initialEntitySet
269
- .add (ClassUtils .forName (candidate .getBeanClassName (), AbstractMongoConfiguration .class .getClassLoader ()));
270
- }
271
- }
272
-
273
- return initialEntitySet ;
274
- }
275
-
276
- /**
277
- * Configures whether to abbreviate field names for domain objects by configuring a
278
- * {@link CamelCaseAbbreviatingFieldNamingStrategy} on the {@link MongoMappingContext} instance created. For advanced
279
- * customization needs, consider overriding {@link #mappingMongoConverter()}.
280
- *
281
- * @return
282
- */
283
- protected boolean abbreviateFieldNames () {
284
- return false ;
285
- }
286
-
287
- /**
288
- * Configures a {@link FieldNamingStrategy} on the {@link MongoMappingContext} instance created.
289
- *
290
- * @return
291
- * @since 1.5
292
- */
293
- protected FieldNamingStrategy fieldNamingStrategy () {
294
- return abbreviateFieldNames () ? new CamelCaseAbbreviatingFieldNamingStrategy ()
295
- : PropertyNameFieldNamingStrategy .INSTANCE ;
296
- }
297
142
}
0 commit comments