|
17 | 17 | package org.springframework.boot.autoconfigure.mongo;
|
18 | 18 |
|
19 | 19 | import java.net.UnknownHostException;
|
| 20 | +import java.util.Collection; |
| 21 | +import java.util.Collections; |
| 22 | +import java.util.HashSet; |
| 23 | +import java.util.Set; |
20 | 24 |
|
21 | 25 | import org.springframework.beans.factory.BeanFactory;
|
22 | 26 | import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
23 | 27 | import org.springframework.beans.factory.annotation.Autowired;
|
| 28 | +import org.springframework.beans.factory.config.BeanDefinition; |
| 29 | +import org.springframework.boot.autoconfigure.AutoConfigurationPackages; |
24 | 30 | import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
25 | 31 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
26 | 32 | import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
27 | 33 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
28 | 34 | import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
29 | 35 | import org.springframework.context.annotation.Bean;
|
| 36 | +import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider; |
30 | 37 | import org.springframework.context.annotation.Configuration;
|
| 38 | +import org.springframework.core.env.Environment; |
| 39 | +import org.springframework.core.io.ResourceLoader; |
| 40 | +import org.springframework.core.type.filter.AnnotationTypeFilter; |
31 | 41 | import org.springframework.dao.DataAccessException;
|
32 | 42 | import org.springframework.dao.support.PersistenceExceptionTranslator;
|
| 43 | +import org.springframework.data.annotation.Persistent; |
33 | 44 | import org.springframework.data.authentication.UserCredentials;
|
34 | 45 | import org.springframework.data.mongodb.MongoDbFactory;
|
35 | 46 | import org.springframework.data.mongodb.core.MongoTemplate;
|
|
39 | 50 | import org.springframework.data.mongodb.core.convert.DefaultDbRefResolver;
|
40 | 51 | import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
|
41 | 52 | import org.springframework.data.mongodb.core.convert.MongoConverter;
|
| 53 | +import org.springframework.data.mongodb.core.mapping.Document; |
42 | 54 | import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
|
43 | 55 | import org.springframework.data.mongodb.gridfs.GridFsTemplate;
|
44 | 56 | import org.springframework.util.Assert;
|
| 57 | +import org.springframework.util.ClassUtils; |
45 | 58 | import org.springframework.util.StringUtils;
|
46 | 59 |
|
47 | 60 | import com.mongodb.DB;
|
@@ -71,6 +84,12 @@ public class MongoDataAutoConfiguration {
|
71 | 84 | @Autowired
|
72 | 85 | private MongoProperties properties;
|
73 | 86 |
|
| 87 | + @Autowired |
| 88 | + private Environment environment; |
| 89 | + |
| 90 | + @Autowired |
| 91 | + private ResourceLoader resourceLoader; |
| 92 | + |
74 | 93 | @Bean
|
75 | 94 | @ConditionalOnMissingBean
|
76 | 95 | public MongoDbFactory mongoDbFactory(Mongo mongo) throws Exception {
|
@@ -111,8 +130,42 @@ public MappingMongoConverter mappingMongoConverter(MongoDbFactory factory,
|
111 | 130 |
|
112 | 131 | @Bean
|
113 | 132 | @ConditionalOnMissingBean
|
114 |
| - public MongoMappingContext mongoMappingContext() { |
115 |
| - return new MongoMappingContext(); |
| 133 | + public MongoMappingContext mongoMappingContext(BeanFactory beanFactory) |
| 134 | + throws ClassNotFoundException { |
| 135 | + MongoMappingContext context = new MongoMappingContext(); |
| 136 | + context.setInitialEntitySet(getInitialEntitySet(beanFactory)); |
| 137 | + return context; |
| 138 | + } |
| 139 | + |
| 140 | + private Set<Class<?>> getInitialEntitySet(BeanFactory beanFactory) |
| 141 | + throws ClassNotFoundException { |
| 142 | + Set<Class<?>> entitySet = new HashSet<Class<?>>(); |
| 143 | + ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider( |
| 144 | + false); |
| 145 | + scanner.setEnvironment(this.environment); |
| 146 | + scanner.setResourceLoader(this.resourceLoader); |
| 147 | + scanner.addIncludeFilter(new AnnotationTypeFilter(Document.class)); |
| 148 | + scanner.addIncludeFilter(new AnnotationTypeFilter(Persistent.class)); |
| 149 | + for (String basePackage : getMappingBasePackages(beanFactory)) { |
| 150 | + if (StringUtils.hasText(basePackage)) { |
| 151 | + for (BeanDefinition candidate : scanner |
| 152 | + .findCandidateComponents(basePackage)) { |
| 153 | + entitySet.add(ClassUtils.forName(candidate.getBeanClassName(), |
| 154 | + MongoDataAutoConfiguration.class.getClassLoader())); |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | + return entitySet; |
| 159 | + } |
| 160 | + |
| 161 | + private static Collection<String> getMappingBasePackages(BeanFactory beanFactory) { |
| 162 | + try { |
| 163 | + return AutoConfigurationPackages.get(beanFactory); |
| 164 | + } |
| 165 | + catch (IllegalStateException ex) { |
| 166 | + // no auto-configuration package registered yet |
| 167 | + return Collections.emptyList(); |
| 168 | + } |
116 | 169 | }
|
117 | 170 |
|
118 | 171 | @Bean
|
|
0 commit comments