|
1 | 1 | /*
|
2 |
| - * Copyright 2010-2011 the original author or authors. |
| 2 | + * Copyright 2011-2012 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.Collections; |
18 | 19 | import java.util.HashSet;
|
19 | 20 | import java.util.Set;
|
20 | 21 |
|
21 |
| -import com.mongodb.Mongo; |
22 | 22 | import org.springframework.beans.factory.config.BeanDefinition;
|
23 | 23 | import org.springframework.context.annotation.Bean;
|
24 | 24 | import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
|
25 | 25 | import org.springframework.context.annotation.Configuration;
|
| 26 | +import org.springframework.core.convert.converter.Converter; |
26 | 27 | import org.springframework.core.type.filter.AnnotationTypeFilter;
|
27 | 28 | import org.springframework.data.annotation.Persistent;
|
28 | 29 | import org.springframework.data.authentication.UserCredentials;
|
29 |
| -import org.springframework.data.mongodb.MongoDbFactory; |
30 | 30 | import org.springframework.data.mongodb.core.MongoTemplate;
|
31 | 31 | import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
|
| 32 | +import org.springframework.data.mongodb.core.convert.CustomConversions; |
32 | 33 | import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
|
33 | 34 | import org.springframework.data.mongodb.core.mapping.Document;
|
34 | 35 | import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
|
35 | 36 | import org.springframework.util.ClassUtils;
|
36 | 37 | import org.springframework.util.StringUtils;
|
37 | 38 |
|
| 39 | +import com.mongodb.Mongo; |
| 40 | + |
| 41 | +/** |
| 42 | + * Base class for Spring Data Mongo configuration using JavaConfig. |
| 43 | + * |
| 44 | + * @author Mark Pollack |
| 45 | + * @author Oliver Gierke |
| 46 | + */ |
38 | 47 | @Configuration
|
39 | 48 | public abstract class AbstractMongoConfiguration {
|
40 | 49 |
|
41 |
| - public abstract String getDatabaseName(); |
| 50 | + /** |
| 51 | + * Return the name of the database to connect to. |
| 52 | + * |
| 53 | + * @return must not be {@literal null}. |
| 54 | + */ |
| 55 | + protected abstract String getDatabaseName(); |
42 | 56 |
|
| 57 | + /** |
| 58 | + * Return the {@link Mongo} instance to connect to. |
| 59 | + * |
| 60 | + * @return |
| 61 | + * @throws Exception |
| 62 | + */ |
43 | 63 | @Bean
|
44 | 64 | public abstract Mongo mongo() throws Exception;
|
45 | 65 |
|
| 66 | + /** |
| 67 | + * Creates a {@link MongoTemplate}. |
| 68 | + * |
| 69 | + * @return |
| 70 | + * @throws Exception |
| 71 | + */ |
46 | 72 | @Bean
|
47 | 73 | public MongoTemplate mongoTemplate() throws Exception {
|
48 | 74 | return new MongoTemplate(mongoDbFactory(), mappingMongoConverter());
|
49 | 75 | }
|
50 | 76 |
|
| 77 | + /** |
| 78 | + * Creates a {@link SimpleMongoDbFactory} to be used by the {@link MongoTemplate}. Will use the {@link Mongo} instance |
| 79 | + * configured in {@link #mongo()}. |
| 80 | + * |
| 81 | + * @see #mongo() |
| 82 | + * @see #mongoTemplate() |
| 83 | + * @return |
| 84 | + * @throws Exception |
| 85 | + */ |
51 | 86 | @Bean
|
52 |
| - public MongoDbFactory mongoDbFactory() throws Exception { |
53 |
| - if (getUserCredentials() == null) { |
| 87 | + public SimpleMongoDbFactory mongoDbFactory() throws Exception { |
| 88 | + |
| 89 | + UserCredentials creadentials = getUserCredentials(); |
| 90 | + |
| 91 | + if (creadentials == null) { |
54 | 92 | return new SimpleMongoDbFactory(mongo(), getDatabaseName());
|
55 | 93 | } else {
|
56 |
| - return new SimpleMongoDbFactory(mongo(), getDatabaseName(), getUserCredentials()); |
| 94 | + return new SimpleMongoDbFactory(mongo(), getDatabaseName(), creadentials); |
57 | 95 | }
|
58 | 96 | }
|
59 | 97 |
|
60 |
| - public String getMappingBasePackage() { |
61 |
| - return ""; |
| 98 | + /** |
| 99 | + * Return the base package to scan for mapped {@link Document}s. |
| 100 | + * |
| 101 | + * @return |
| 102 | + */ |
| 103 | + protected String getMappingBasePackage() { |
| 104 | + return null; |
62 | 105 | }
|
63 | 106 |
|
64 |
| - public UserCredentials getUserCredentials() { |
| 107 | + /** |
| 108 | + * Return {@link UserCredentials} to be used when connecting to the MongoDB instance or {@literal null} if none shall |
| 109 | + * be used. |
| 110 | + * |
| 111 | + * @return |
| 112 | + */ |
| 113 | + protected UserCredentials getUserCredentials() { |
65 | 114 | return null;
|
66 | 115 | }
|
67 | 116 |
|
| 117 | + /** |
| 118 | + * Creates a {@link MongoMappingContext} equipped with entity classes scanned from the mapping base package. |
| 119 | + * |
| 120 | + * @see #getMappingBasePackage() |
| 121 | + * @return |
| 122 | + * @throws ClassNotFoundException |
| 123 | + */ |
68 | 124 | @Bean
|
69 |
| - public MongoMappingContext mongoMappingContext() throws ClassNotFoundException, LinkageError { |
| 125 | + public MongoMappingContext mongoMappingContext() throws ClassNotFoundException { |
| 126 | + |
70 | 127 | MongoMappingContext mappingContext = new MongoMappingContext();
|
71 |
| - String basePackage = getMappingBasePackage(); |
72 |
| - if (StringUtils.hasText(basePackage)) { |
73 |
| - ClassPathScanningCandidateComponentProvider componentProvider = new ClassPathScanningCandidateComponentProvider( |
74 |
| - false); |
75 |
| - componentProvider.addIncludeFilter(new AnnotationTypeFilter(Document.class)); |
76 |
| - componentProvider.addIncludeFilter(new AnnotationTypeFilter(Persistent.class)); |
| 128 | + mappingContext.setInitialEntitySet(getInitialEntitySet()); |
| 129 | + mappingContext.setSimpleTypeHolder(customConversions().getSimpleTypeHolder()); |
| 130 | + mappingContext.afterPropertiesSet(); |
77 | 131 |
|
78 |
| - Set<Class<?>> initialEntitySet = new HashSet<Class<?>>(); |
79 |
| - for (BeanDefinition candidate : componentProvider.findCandidateComponents(basePackage)) { |
80 |
| - initialEntitySet.add(ClassUtils.forName(candidate.getBeanClassName(), mappingContext.getClass() |
81 |
| - .getClassLoader())); |
82 |
| - } |
83 |
| - mappingContext.setInitialEntitySet(initialEntitySet); |
84 |
| - } |
85 | 132 | return mappingContext;
|
86 | 133 | }
|
87 | 134 |
|
| 135 | + /** |
| 136 | + * Register custom {@link Converter}s in a {@link CustomConversions} object if required. These |
| 137 | + * {@link CustomConversions} will be registered with the {@link #mappingMongoConverter()} and |
| 138 | + * {@link #mongoMappingContext()}. Returns an empty {@link CustomConversions} instance by default. |
| 139 | + * |
| 140 | + * @return must not be {@literal null}. |
| 141 | + */ |
| 142 | + @Bean |
| 143 | + public CustomConversions customConversions() { |
| 144 | + return new CustomConversions(Collections.emptyList()); |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Creates a {@link MappingMongoConverter} using the configured {@link #mongoDbFactory()} and |
| 149 | + * {@link #mongoMappingContext()}. Will get {@link #customConversions()} applied. |
| 150 | + * |
| 151 | + * @see #customConversions() |
| 152 | + * @see #mongoMappingContext() |
| 153 | + * @see #mongoDbFactory() |
| 154 | + * @return |
| 155 | + * @throws Exception |
| 156 | + */ |
88 | 157 | @Bean
|
89 | 158 | public MappingMongoConverter mappingMongoConverter() throws Exception {
|
90 | 159 | MappingMongoConverter converter = new MappingMongoConverter(mongoDbFactory(), mongoMappingContext());
|
91 |
| - afterMappingMongoConverterCreation(converter); |
| 160 | + converter.setCustomConversions(customConversions()); |
92 | 161 | return converter;
|
93 | 162 | }
|
94 | 163 |
|
95 | 164 | /**
|
96 |
| - * Hook that allows post-processing after the MappingMongoConverter has been successfully created. |
| 165 | + * Scans the mapping base package for classes annotated with {@link Document}. |
97 | 166 | *
|
98 |
| - * @param converter |
| 167 | + * @see #getMappingBasePackage() |
| 168 | + * @return |
| 169 | + * @throws ClassNotFoundException |
99 | 170 | */
|
100 |
| - protected void afterMappingMongoConverterCreation(MappingMongoConverter converter) { |
| 171 | + protected Set<Class<?>> getInitialEntitySet() throws ClassNotFoundException { |
| 172 | + |
| 173 | + String basePackage = getMappingBasePackage(); |
| 174 | + Set<Class<?>> initialEntitySet = new HashSet<Class<?>>(); |
| 175 | + |
| 176 | + if (StringUtils.hasText(basePackage)) { |
| 177 | + ClassPathScanningCandidateComponentProvider componentProvider = new ClassPathScanningCandidateComponentProvider( |
| 178 | + false); |
| 179 | + componentProvider.addIncludeFilter(new AnnotationTypeFilter(Document.class)); |
| 180 | + componentProvider.addIncludeFilter(new AnnotationTypeFilter(Persistent.class)); |
| 181 | + |
| 182 | + for (BeanDefinition candidate : componentProvider.findCandidateComponents(basePackage)) { |
| 183 | + initialEntitySet.add(ClassUtils.forName(candidate.getBeanClassName(), |
| 184 | + AbstractMongoConfiguration.class.getClassLoader())); |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + return initialEntitySet; |
101 | 189 | }
|
102 | 190 | }
|
0 commit comments