Merge pull request #6882 from olivergierke/issue/6881

* pr/6882:
  Use default simple types with MongoMappingContext
pull/6900/head
Phillip Webb 8 years ago
commit fe7bbbe8ee

@ -17,6 +17,7 @@
package org.springframework.boot.autoconfigure.data.mongo;
import java.net.UnknownHostException;
import java.util.Collections;
import com.mongodb.DB;
import com.mongodb.Mongo;
@ -24,7 +25,6 @@ import com.mongodb.MongoClient;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
@ -103,24 +103,19 @@ public class MongoDataAutoConfiguration {
@Bean
@ConditionalOnMissingBean(MongoConverter.class)
public MappingMongoConverter mappingMongoConverter(MongoDbFactory factory,
MongoMappingContext context, BeanFactory beanFactory) {
MongoMappingContext context, BeanFactory beanFactory,
CustomConversions conversions) {
DbRefResolver dbRefResolver = new DefaultDbRefResolver(factory);
MappingMongoConverter mappingConverter = new MappingMongoConverter(dbRefResolver,
context);
try {
mappingConverter
.setCustomConversions(beanFactory.getBean(CustomConversions.class));
}
catch (NoSuchBeanDefinitionException ex) {
// Ignore
}
mappingConverter.setCustomConversions(conversions);
return mappingConverter;
}
@Bean
@ConditionalOnMissingBean
public MongoMappingContext mongoMappingContext(BeanFactory beanFactory)
throws ClassNotFoundException {
public MongoMappingContext mongoMappingContext(BeanFactory beanFactory,
CustomConversions conversions) throws ClassNotFoundException {
MongoMappingContext context = new MongoMappingContext();
context.setInitialEntitySet(new EntityScanner(this.applicationContext)
.scan(Document.class, Persistent.class));
@ -129,6 +124,7 @@ public class MongoDataAutoConfiguration {
context.setFieldNamingStrategy(
(FieldNamingStrategy) BeanUtils.instantiate(strategyClass));
}
context.setSimpleTypeHolder(conversions.getSimpleTypeHolder());
return context;
}
@ -141,6 +137,12 @@ public class MongoDataAutoConfiguration {
mongoTemplate.getConverter());
}
@Bean
@ConditionalOnMissingBean
public CustomConversions customConversions() {
return new CustomConversions(Collections.emptyList());
}
/**
* {@link MongoDbFactory} decorator to respect
* {@link MongoProperties#getGridFsDatabase()} if set.

@ -16,6 +16,7 @@
package org.springframework.boot.autoconfigure.data.mongo;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Set;
@ -44,6 +45,7 @@ import org.springframework.data.mapping.model.PropertyNameFieldNamingStrategy;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.convert.CustomConversions;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
import org.springframework.data.mongodb.gridfs.GridFsTemplate;
import org.springframework.test.util.ReflectionTestUtils;
@ -156,6 +158,17 @@ public class MongoDataAutoConfigurationTests {
assertThat(initialEntitySet).containsOnly(City.class, Country.class);
}
@Test
public void registersDefaultSimpleTypesWithMappingContext() {
this.context = new AnnotationConfigApplicationContext();
this.context.register(MongoAutoConfiguration.class,
MongoDataAutoConfiguration.class);
this.context.refresh();
MongoMappingContext context = this.context.getBean(MongoMappingContext.class);
MongoPersistentEntity<?> entity = context.getPersistentEntity(Sample.class);
assertThat(entity.getPersistentProperty("date").isEntity()).isFalse();
}
public void testFieldNamingStrategy(String strategy,
Class<? extends FieldNamingStrategy> expectedType) {
this.context = new AnnotationConfigApplicationContext();
@ -205,4 +218,9 @@ public class MongoDataAutoConfigurationTests {
}
static class Sample {
LocalDateTime date;
}
}

Loading…
Cancel
Save