DATADOC-75 - Changed addConverters(…) to setConverters(…).
Removed empty constructor to enforce a MappingContext being passed into MappingMongoConverter. Removed the setter for MappingContext. initializeConverters now checks for existence of a converter for ObjectId-to-whatever before adding one in both SimpleMongoConverter as well as MappingMongoConverter.
This commit is contained in:
@@ -72,8 +72,7 @@ public abstract class AbstractMongoConfiguration {
|
||||
|
||||
@Bean
|
||||
public MappingMongoConverter mappingMongoConverter() throws Exception {
|
||||
MappingMongoConverter converter = new MappingMongoConverter();
|
||||
converter.setMappingContext(mongoMappingContext());
|
||||
MappingMongoConverter converter = new MappingMongoConverter(mongoMappingContext());
|
||||
converter.setMongo(mongo());
|
||||
return converter;
|
||||
}
|
||||
|
||||
@@ -85,7 +85,7 @@ public class MongoMappingConverterParser extends AbstractBeanDefinitionParser {
|
||||
}
|
||||
|
||||
BeanDefinitionBuilder converterBuilder = BeanDefinitionBuilder.genericBeanDefinition(MappingMongoConverter.class);
|
||||
converterBuilder.addPropertyReference("mappingContext", ctxRef);
|
||||
converterBuilder.addConstructorArgReference(ctxRef);
|
||||
|
||||
// Need a reference to a Mongo instance
|
||||
String mongoRef = element.getAttribute("mongo-ref");
|
||||
|
||||
@@ -27,8 +27,6 @@ import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import com.mongodb.BasicDBList;
|
||||
import com.mongodb.BasicDBObject;
|
||||
import com.mongodb.DB;
|
||||
@@ -80,19 +78,21 @@ public class MappingMongoConverter implements MongoConverter, ApplicationContext
|
||||
|
||||
protected final GenericConversionService conversionService = ConversionServiceFactory.createDefaultConversionService();
|
||||
protected final Map<Class<?>, Class<?>> customTypeMapping = new HashMap<Class<?>, Class<?>>();
|
||||
protected final MappingContext mappingContext;
|
||||
protected SpelExpressionParser spelExpressionParser = new SpelExpressionParser();
|
||||
protected MappingContext mappingContext;
|
||||
protected ApplicationContext applicationContext;
|
||||
protected boolean useFieldAccessOnly = true;
|
||||
protected Mongo mongo;
|
||||
protected String defaultDatabase;
|
||||
|
||||
public MappingMongoConverter() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link MappingMongoConverter} with the given {@link MappingContext}.
|
||||
*
|
||||
* @param mappingContext
|
||||
*/
|
||||
public MappingMongoConverter(MappingContext mappingContext) {
|
||||
this.mappingContext = mappingContext;
|
||||
this.conversionService.removeConvertible(Object.class, String.class);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -101,7 +101,7 @@ public class MappingMongoConverter implements MongoConverter, ApplicationContext
|
||||
*
|
||||
* @param converters
|
||||
*/
|
||||
public void addConverters(List<Converter<?, ?>> converters) {
|
||||
public void setConverters(List<Converter<?, ?>> converters) {
|
||||
if (null != converters) {
|
||||
for (Converter<?, ?> c : converters) {
|
||||
registerConverter(c);
|
||||
@@ -127,10 +127,6 @@ public class MappingMongoConverter implements MongoConverter, ApplicationContext
|
||||
return mappingContext;
|
||||
}
|
||||
|
||||
public void setMappingContext(MappingContext mappingContext) {
|
||||
this.mappingContext = mappingContext;
|
||||
}
|
||||
|
||||
public Mongo getMongo() {
|
||||
return mongo;
|
||||
}
|
||||
@@ -378,18 +374,20 @@ public class MappingMongoConverter implements MongoConverter, ApplicationContext
|
||||
* Registers converters for {@link ObjectId} handling, removes plain {@link #toString()} converter and promotes the
|
||||
* configured {@link ConversionService} to {@link MappingBeanHelper}.
|
||||
*/
|
||||
protected void initializeConverters() {
|
||||
|
||||
this.conversionService.removeConvertible(Object.class, String.class);
|
||||
private void initializeConverters() {
|
||||
|
||||
if (!conversionService.canConvert(ObjectId.class, String.class)) {
|
||||
conversionService.addConverter(ObjectIdToStringConverter.INSTANCE);
|
||||
conversionService.addConverter(StringToObjectIdConverter.INSTANCE);
|
||||
}
|
||||
if (!conversionService.canConvert(ObjectId.class, BigInteger.class)) {
|
||||
conversionService.addConverter(ObjectIdToBigIntegerConverter.INSTANCE);
|
||||
conversionService.addConverter(BigIntegerToIdConverter.INSTANCE);
|
||||
}
|
||||
conversionService.addConverter(ObjectIdToStringConverter.INSTANCE);
|
||||
}
|
||||
if (!conversionService.canConvert(String.class, ObjectId.class)) {
|
||||
conversionService.addConverter(StringToObjectIdConverter.INSTANCE);
|
||||
}
|
||||
if (!conversionService.canConvert(ObjectId.class, BigInteger.class)) {
|
||||
conversionService.addConverter(ObjectIdToBigIntegerConverter.INSTANCE);
|
||||
}
|
||||
if (!conversionService.canConvert(BigInteger.class, ObjectId.class)) {
|
||||
conversionService.addConverter(BigIntegerToObjectIdConverter.INSTANCE);
|
||||
}
|
||||
|
||||
MappingBeanHelper.setConversionService(conversionService);
|
||||
}
|
||||
@@ -649,7 +647,7 @@ public class MappingMongoConverter implements MongoConverter, ApplicationContext
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public static enum BigIntegerToIdConverter implements Converter<BigInteger, ObjectId> {
|
||||
public static enum BigIntegerToObjectIdConverter implements Converter<BigInteger, ObjectId> {
|
||||
INSTANCE;
|
||||
|
||||
public ObjectId convert(BigInteger source) {
|
||||
|
||||
@@ -30,6 +30,7 @@ import org.apache.commons.logging.LogFactory;
|
||||
import org.bson.types.CodeWScope;
|
||||
import org.bson.types.ObjectId;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.core.CollectionFactory;
|
||||
import org.springframework.core.convert.ConversionFailedException;
|
||||
import org.springframework.core.convert.ConversionService;
|
||||
@@ -48,7 +49,7 @@ import org.springframework.util.comparator.CompoundComparator;
|
||||
* @author Thomas Risberg
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public class SimpleMongoConverter implements MongoConverter {
|
||||
public class SimpleMongoConverter implements MongoConverter, InitializingBean {
|
||||
|
||||
private static final Log LOG = LogFactory.getLog(SimpleMongoConverter.class);
|
||||
@SuppressWarnings("unchecked")
|
||||
@@ -113,19 +114,26 @@ public class SimpleMongoConverter implements MongoConverter {
|
||||
public SimpleMongoConverter() {
|
||||
this.conversionService = ConversionServiceFactory.createDefaultConversionService();
|
||||
this.conversionService.removeConvertible(Object.class, String.class);
|
||||
initializeConverters();
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes additional converters that handle {@link ObjectId} conversion. Will register converters for supported
|
||||
* id types if none are registered for those conversion already. {@link GenericConversionService} is configured.
|
||||
*/
|
||||
protected void initializeConverters() {
|
||||
private void initializeConverters() {
|
||||
|
||||
conversionService.addConverter(ObjectIdToStringConverter.INSTANCE);
|
||||
conversionService.addConverter(StringToObjectIdConverter.INSTANCE);
|
||||
conversionService.addConverter(ObjectIdToBigIntegerConverter.INSTANCE);
|
||||
conversionService.addConverter(BigIntegerToIdConverter.INSTANCE);
|
||||
if (!conversionService.canConvert(ObjectId.class, String.class)) {
|
||||
conversionService.addConverter(ObjectIdToStringConverter.INSTANCE);
|
||||
}
|
||||
if (!conversionService.canConvert(String.class, ObjectId.class)) {
|
||||
conversionService.addConverter(StringToObjectIdConverter.INSTANCE);
|
||||
}
|
||||
if (!conversionService.canConvert(ObjectId.class, BigInteger.class)) {
|
||||
conversionService.addConverter(ObjectIdToBigIntegerConverter.INSTANCE);
|
||||
}
|
||||
if (!conversionService.canConvert(BigInteger.class, ObjectId.class)) {
|
||||
conversionService.addConverter(BigIntegerToObjectIdConverter.INSTANCE);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -134,7 +142,7 @@ public class SimpleMongoConverter implements MongoConverter {
|
||||
*
|
||||
* @param converters
|
||||
*/
|
||||
public void addConverters(Set<?> converters) {
|
||||
public void setConverters(Set<?> converters) {
|
||||
for (Object converter : converters) {
|
||||
boolean added = false;
|
||||
if (converter instanceof Converter) {
|
||||
@@ -525,6 +533,13 @@ public class SimpleMongoConverter implements MongoConverter {
|
||||
public ObjectId convertObjectId(Object id) {
|
||||
return conversionService.convert(id, ObjectId.class);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
|
||||
*/
|
||||
public void afterPropertiesSet() {
|
||||
initializeConverters();
|
||||
}
|
||||
|
||||
/**
|
||||
* Simple singleton to convert {@link ObjectId}s to their {@link String} representation.
|
||||
@@ -570,7 +585,7 @@ public class SimpleMongoConverter implements MongoConverter {
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public static enum BigIntegerToIdConverter implements Converter<BigInteger, ObjectId> {
|
||||
public static enum BigIntegerToObjectIdConverter implements Converter<BigInteger, ObjectId> {
|
||||
INSTANCE;
|
||||
|
||||
public ObjectId convert(BigInteger source) {
|
||||
|
||||
@@ -61,13 +61,16 @@ public class MongoTemplateTests {
|
||||
|
||||
@Autowired
|
||||
public void setMongo(Mongo mongo) {
|
||||
MappingMongoConverter converter = new MappingMongoConverter();
|
||||
|
||||
MongoMappingContext mappingContext = new MongoMappingContext();
|
||||
mappingContext.addPersistentEntity(PersonWith_idPropertyOfTypeObjectId.class);
|
||||
mappingContext.addPersistentEntity(PersonWith_idPropertyOfTypeString.class);
|
||||
mappingContext.addPersistentEntity(PersonWithIdPropertyOfTypeObjectId.class);
|
||||
mappingContext.addPersistentEntity(PersonWithIdPropertyOfTypeString.class);
|
||||
converter.setMappingContext(mappingContext);
|
||||
|
||||
MappingMongoConverter converter = new MappingMongoConverter(mappingContext);
|
||||
converter.afterPropertiesSet();
|
||||
|
||||
this.mappingTemplate = new MongoTemplate(mongo, "database", "springdata", converter);
|
||||
}
|
||||
|
||||
@@ -190,7 +193,7 @@ public class MongoTemplateTests {
|
||||
PersonWith_idPropertyOfTypeString p4 = new PersonWith_idPropertyOfTypeString();
|
||||
p4.setFirstName("Sven_4");
|
||||
p4.setAge(22);
|
||||
p4.set_id("FOUR");
|
||||
p4.set_id("FOUR");
|
||||
// insert
|
||||
mongoTemplate.insert(p4);
|
||||
// also try save
|
||||
|
||||
@@ -53,6 +53,7 @@ public class SimpleMongoConverterTests {
|
||||
@Before
|
||||
public void setUp() {
|
||||
converter = new SimpleMongoConverter();
|
||||
converter.afterPropertiesSet();
|
||||
object = new BasicDBObject();
|
||||
}
|
||||
|
||||
@@ -326,7 +327,7 @@ public class SimpleMongoConverterTests {
|
||||
converters.add(new LocalDateToDateConverter());
|
||||
converters.add(new DateToLocalDateConverter());
|
||||
|
||||
converter.addConverters(converters);
|
||||
converter.setConverters(converters);
|
||||
|
||||
AnotherPerson person = new AnotherPerson();
|
||||
person.birthDate = new LocalDate();
|
||||
|
||||
@@ -77,7 +77,7 @@ public class MappingMongoConverterUnitTests {
|
||||
mappingContext.setCustomSimpleTypes(customSimpleTypes);
|
||||
|
||||
converter = new MappingMongoConverter(mappingContext);
|
||||
converter.addConverters(converters);
|
||||
converter.setConverters(converters);
|
||||
converter.afterPropertiesSet();
|
||||
|
||||
Person person = new Person();
|
||||
|
||||
Reference in New Issue
Block a user