|
1 | 1 | /*
|
2 |
| - * Copyright 2011-2015 the original author or authors. |
| 2 | + * Copyright 2011-2016 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
15 | 15 | */
|
16 | 16 | package org.springframework.data.mongodb.config;
|
17 | 17 |
|
| 18 | +import java.util.Collection; |
18 | 19 | import java.util.Collections;
|
19 | 20 | import java.util.HashSet;
|
20 | 21 | import java.util.Set;
|
@@ -118,17 +119,33 @@ public MongoDbFactory mongoDbFactory() throws Exception {
|
118 | 119 | * Return the base package to scan for mapped {@link Document}s. Will return the package name of the configuration
|
119 | 120 | * class' (the concrete class, not this one here) by default. So if you have a {@code com.acme.AppConfig} extending
|
120 | 121 | * {@link AbstractMongoConfiguration} the base package will be considered {@code com.acme} unless the method is
|
121 |
| - * overriden to implement alternate behaviour. |
| 122 | + * overridden to implement alternate behavior. |
122 | 123 | *
|
123 | 124 | * @return the base package to scan for mapped {@link Document} classes or {@literal null} to not enable scanning for
|
124 | 125 | * entities.
|
| 126 | + * @deprecated use {@link #getMappingBasePackages()} instead. |
125 | 127 | */
|
| 128 | + @Deprecated |
126 | 129 | protected String getMappingBasePackage() {
|
127 | 130 |
|
128 | 131 | Package mappingBasePackage = getClass().getPackage();
|
129 | 132 | return mappingBasePackage == null ? null : mappingBasePackage.getName();
|
130 | 133 | }
|
131 | 134 |
|
| 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 | + |
132 | 149 | /**
|
133 | 150 | * Return {@link UserCredentials} to be used when connecting to the MongoDB instance or {@literal null} if none shall
|
134 | 151 | * be used.
|
@@ -204,26 +221,52 @@ public MappingMongoConverter mappingMongoConverter() throws Exception {
|
204 | 221 | }
|
205 | 222 |
|
206 | 223 | /**
|
207 |
| - * Scans the mapping base package for classes annotated with {@link Document}. |
| 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()}. |
208 | 226 | *
|
209 |
| - * @see #getMappingBasePackage() |
| 227 | + * @see #getMappingBasePackages() |
210 | 228 | * @return
|
211 | 229 | * @throws ClassNotFoundException
|
212 | 230 | */
|
213 | 231 | protected Set<Class<?>> getInitialEntitySet() throws ClassNotFoundException {
|
214 | 232 |
|
215 |
| - String basePackage = getMappingBasePackage(); |
| 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 | + |
216 | 257 | Set<Class<?>> initialEntitySet = new HashSet<Class<?>>();
|
217 | 258 |
|
218 | 259 | if (StringUtils.hasText(basePackage)) {
|
| 260 | + |
219 | 261 | ClassPathScanningCandidateComponentProvider componentProvider = new ClassPathScanningCandidateComponentProvider(
|
220 | 262 | false);
|
221 | 263 | componentProvider.addIncludeFilter(new AnnotationTypeFilter(Document.class));
|
222 | 264 | componentProvider.addIncludeFilter(new AnnotationTypeFilter(Persistent.class));
|
223 | 265 |
|
224 | 266 | for (BeanDefinition candidate : componentProvider.findCandidateComponents(basePackage)) {
|
225 |
| - initialEntitySet.add(ClassUtils.forName(candidate.getBeanClassName(), |
226 |
| - AbstractMongoConfiguration.class.getClassLoader())); |
| 267 | + |
| 268 | + initialEntitySet |
| 269 | + .add(ClassUtils.forName(candidate.getBeanClassName(), AbstractMongoConfiguration.class.getClassLoader())); |
227 | 270 | }
|
228 | 271 | }
|
229 | 272 |
|
|
0 commit comments