Skip to content

Commit a9dc0fa

Browse files
Thomas Darimontodrotbohm
authored andcommitted
DATAMONGO-725 - Improve configurability and documentation of TypeMapper on MappingMongoConverter.
Added new attribute type-mapper-ref to the mapping-converter element in spring-mongo-1.3.xsd in order to support the configuration of custom-type-mappers. Removed the unsupported attributes "mongo-ref" and "mongo-template-ref" from the mapping-converter element in spring-mongo-1.3.xsd because they are not considered anymore. Updated MappingMongoConverterParser to be aware of the new attribute. Added examples for configuring a custom MongoTypeMapper the usage of @typealias to the reference documentation. Original pull request: spring-projects#61.
1 parent 0605c7b commit a9dc0fa

File tree

10 files changed

+181
-14
lines changed

10 files changed

+181
-14
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/MappingMongoConverterParser.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
* @author Jon Brisbin
7171
* @author Oliver Gierke
7272
* @author Maciej Walkowiak
73+
* @author Thomas Darimont
7374
*/
7475
public class MappingMongoConverterParser implements BeanDefinitionParser {
7576

@@ -105,6 +106,11 @@ public BeanDefinition parse(Element element, ParserContext parserContext) {
105106
converterBuilder.addConstructorArgReference(dbFactoryRef);
106107
converterBuilder.addConstructorArgReference(ctxRef);
107108

109+
String typeMapperRef = element.getAttribute("type-mapper-ref");
110+
if (StringUtils.hasText(typeMapperRef)) {
111+
converterBuilder.addPropertyReference("typeMapper", typeMapperRef);
112+
}
113+
108114
if (conversionsDefinition != null) {
109115
converterBuilder.addPropertyValue("customConversions", conversionsDefinition);
110116
}

spring-data-mongodb/src/main/resources/org/springframework/data/mongodb/config/spring-mongo-1.3.xsd

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,10 @@ The base package in which to scan for entities annotated with @Document
181181
</xsd:appinfo>
182182
</xsd:annotation>
183183
</xsd:attribute>
184-
<xsd:attribute name="mongo-ref" type="mongoRef" use="optional">
184+
<xsd:attribute name="type-mapper-ref" type="typeMapperRef" use="optional">
185185
<xsd:annotation>
186186
<xsd:documentation>
187-
The reference to a Mongo. Will default to 'mongo'.
187+
The reference to a MongoTypeMapper to be used by this MappingMongoConverter.
188188
</xsd:documentation>
189189
</xsd:annotation>
190190
</xsd:attribute>
@@ -195,13 +195,6 @@ The base package in which to scan for entities annotated with @Document
195195
</xsd:documentation>
196196
</xsd:annotation>
197197
</xsd:attribute>
198-
<xsd:attribute name="mongo-template-ref" type="mongoTemplateRef" use="optional">
199-
<xsd:annotation>
200-
<xsd:documentation source="org.springframework.data.mongodb.core.core.MongoTemplate">
201-
The reference to a MongoTemplate. Will default to 'mongoTemplate'.
202-
</xsd:documentation>
203-
</xsd:annotation>
204-
</xsd:attribute>
205198
<xsd:attribute name="disable-validation" use="optional">
206199
<xsd:annotation>
207200
<xsd:documentation source="org.springframework.data.mongodb.core.mapping.event.ValidatingMongoEventListener">
@@ -257,6 +250,17 @@ The name of the Mongo object that determines what server to monitor. (by default
257250
</xsd:complexType>
258251
</xsd:element>
259252

253+
<xsd:simpleType name="typeMapperRef">
254+
<xsd:annotation>
255+
<xsd:appinfo>
256+
<tool:annotation kind="ref">
257+
<tool:assignable-to type="org.springframework.data.mongodb.core.convert.MongoTypeMapper"/>
258+
</tool:annotation>
259+
</xsd:appinfo>
260+
</xsd:annotation>
261+
<xsd:union memberTypes="xsd:string"/>
262+
</xsd:simpleType>
263+
260264
<xsd:simpleType name="mappingContextRef">
261265
<xsd:annotation>
262266
<xsd:appinfo>

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/config/AbstractMongoConfigurationUnitTests.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012 the original author or authors.
2+
* Copyright 2012-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,8 +23,11 @@
2323
import org.junit.rules.ExpectedException;
2424
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
2525
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
26+
import org.springframework.context.annotation.Bean;
2627
import org.springframework.context.annotation.Configuration;
2728
import org.springframework.data.mongodb.MongoDbFactory;
29+
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
30+
import org.springframework.data.mongodb.core.convert.MongoTypeMapper;
2831
import org.springframework.data.mongodb.core.mapping.BasicMongoPersistentEntity;
2932
import org.springframework.data.mongodb.core.mapping.Document;
3033
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
@@ -37,6 +40,7 @@
3740
* Unit tests for {@link AbstractMongoConfiguration}.
3841
*
3942
* @author Oliver Gierke
43+
* @author Thomas Darimont
4044
*/
4145
public class AbstractMongoConfigurationUnitTests {
4246

@@ -113,6 +117,20 @@ public void lifecycleCallbacksAreInvokedInAppropriateOrder() {
113117
assertThat(spElContext.getBeanResolver(), is(notNullValue()));
114118
}
115119

120+
/**
121+
* @see DATAMONGO-725
122+
*/
123+
@Test
124+
public void shouldBeAbleToConfigureCustomTypeMapperViaJavaConfig() {
125+
126+
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SampleMongoConfiguration.class);
127+
MongoTypeMapper typeMapper = context.getBean(CustomMongoTypeMapper.class);
128+
MappingMongoConverter mmc = context.getBean(MappingMongoConverter.class);
129+
130+
assertThat(mmc, is(notNullValue()));
131+
assertThat(mmc.getTypeMapper(), is(typeMapper));
132+
}
133+
116134
private static void assertScanningDisabled(final String value) throws ClassNotFoundException {
117135

118136
AbstractMongoConfiguration configuration = new SampleMongoConfiguration() {
@@ -138,6 +156,19 @@ protected String getDatabaseName() {
138156
public Mongo mongo() throws Exception {
139157
return new Mongo();
140158
}
159+
160+
@Bean
161+
@Override
162+
public MappingMongoConverter mappingMongoConverter() throws Exception {
163+
MappingMongoConverter mmc = super.mappingMongoConverter();
164+
mmc.setTypeMapper(typeMapper());
165+
return mmc;
166+
}
167+
168+
@Bean
169+
public MongoTypeMapper typeMapper() {
170+
return new CustomMongoTypeMapper();
171+
}
141172
}
142173

143174
@Document
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright 2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mongodb.config;
17+
18+
import org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper;
19+
20+
/**
21+
* @author Thomas Darimont
22+
*/
23+
class CustomMongoTypeMapper extends DefaultMongoTypeMapper {}

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/config/MappingMongoConverterParserIntegrationTests.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import org.springframework.core.convert.converter.GenericConverter;
3232
import org.springframework.core.io.ClassPathResource;
3333
import org.springframework.data.mongodb.core.convert.CustomConversions;
34+
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
35+
import org.springframework.data.mongodb.core.convert.MongoTypeMapper;
3436
import org.springframework.data.mongodb.core.mapping.Account;
3537
import org.springframework.data.mongodb.core.mapping.CamelCaseAbbreviatingFieldNamingStrategy;
3638
import org.springframework.data.mongodb.repository.Person;
@@ -42,6 +44,7 @@
4244
* Integration tests for {@link MappingMongoConverterParser}.
4345
*
4446
* @author Oliver Gierke
47+
* @author Thomas Darimont
4548
*/
4649
public class MappingMongoConverterParserIntegrationTests {
4750

@@ -61,6 +64,15 @@ public void allowsDbFactoryRefAttribute() {
6164
factory.getBean("converter");
6265
}
6366

67+
@Test
68+
public void hasCustomTypeMapper() {
69+
70+
MappingMongoConverter converter = factory.getBean("converter", MappingMongoConverter.class);
71+
MongoTypeMapper customMongoTypeMapper = factory.getBean(CustomMongoTypeMapper.class);
72+
73+
assertThat(converter.getTypeMapper(), is(customMongoTypeMapper));
74+
}
75+
6476
@Test
6577
public void scansForConverterAndSetsUpCustomConversionsAccordingly() {
6678

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/config/MappingMongoConverterParserValidationIntegrationTests.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012 the original author or authors.
2+
* Copyright 2012-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,6 +33,7 @@
3333
*
3434
* @see DATAMONGO-36
3535
* @author Maciej Walkowiak
36+
* @author Thomas Darimont
3637
*/
3738
public class MappingMongoConverterParserValidationIntegrationTests {
3839

@@ -65,4 +66,11 @@ public void validatingEventListenersIsNotCreatedWhenDisabled() {
6566
reader.loadBeanDefinitions(new ClassPathResource("namespace/converter-validation-disabled.xml"));
6667
factory.getBean(BeanNames.VALIDATING_EVENT_LISTENER);
6768
}
69+
70+
@Test
71+
public void validatingEventListenerCreatedWithCustomTypeMapperConfig() {
72+
73+
reader.loadBeanDefinitions(new ClassPathResource("namespace/converter-custom-typeMapper.xml"));
74+
assertThat(factory.getBean(BeanNames.VALIDATING_EVENT_LISTENER), is(not(nullValue())));
75+
}
6876
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
5+
xsi:schemaLocation="http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo.xsd
6+
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
7+
8+
<mongo:mapping-converter type-mapper-ref="customMongoTypeMapper"/>
9+
10+
<bean name="customMongoTypeMapper" class="org.springframework.data.mongodb.config.CustomMongoTypeMapper"/>
11+
</beans>

spring-data-mongodb/src/test/resources/namespace/converter.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
xsi:schemaLocation="http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo.xsd
66
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
77

8-
<mongo:mapping-converter id="converter" db-factory-ref="factory">
8+
<mongo:mapping-converter id="converter" db-factory-ref="factory" type-mapper-ref="customMongoTypeMapper">
99
<mongo:custom-converters base-package="org.springframework.data.mongodb.config" />
1010
</mongo:mapping-converter>
1111

1212
<mongo:db-factory id="factory" />
1313

1414
<mongo:mapping-converter id="abbreviatingConverter" abbreviate-field-names="true" />
15+
16+
<bean name="customMongoTypeMapper" class="org.springframework.data.mongodb.config.CustomMongoTypeMapper"/>
1517

1618
</beans>

spring-data-mongodb/src/test/resources/template-mapping.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@
3535
<constructor-arg name="mongoConverter" ref="mappingConverter1"/>
3636
</bean>
3737

38-
<mongo:mapping-converter id="mappingConverter2" base-package="org.springframework.data.mongodb.core.mapping"
39-
mongo-template-ref="mongoTemplate2">
38+
<mongo:mapping-converter id="mappingConverter2" base-package="org.springframework.data.mongodb.core.mapping">
4039
<mongo:custom-converters>
4140
<mongo:converter>
4241
<bean class="org.springframework.data.mongodb.core.PersonReadConverter"/>

src/docbkx/reference/mongodb.xml

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,6 +1057,77 @@ mongoTemplate.save(sample);
10571057
instance of that interface can be configured at the
10581058
<classname>DefaultMongoTypeMapper</classname> which can be configured
10591059
in turn on <classname>MappingMongoConverter</classname>.</para>
1060+
1061+
<example>
1062+
<title>Defining a TypeAlias for an Entity</title>
1063+
1064+
<programlisting language="java">@TypeAlias("pers")
1065+
class Person {
1066+
1067+
}</programlisting>
1068+
1069+
<para>Note that the resulting document will contain
1070+
<code>"pers"</code> as the value in the <code>_class</code>
1071+
Field.</para>
1072+
</example>
1073+
</simplesect>
1074+
1075+
<simplesect>
1076+
<title>Configuring custom type mapping</title>
1077+
1078+
<para>The following example demonstrates how to configure a custom
1079+
<classname>MongoTypeMapper</classname> in
1080+
<classname>MappingMongoConverter</classname>.</para>
1081+
1082+
<example>
1083+
<title>Configuring a custom MongoTypeMapper via Spring Java
1084+
Config</title>
1085+
1086+
<programlisting language="java">class CustomMongoTypeMapper extends DefaultMongoTypeMapper {
1087+
//implement custom type mapping here
1088+
}</programlisting>
1089+
1090+
<programlisting language="java">@Configuration
1091+
class SampleMongoConfiguration extends AbstractMongoConfiguration {
1092+
1093+
@Override
1094+
protected String getDatabaseName() {
1095+
return "database";
1096+
}
1097+
1098+
@Override
1099+
public Mongo mongo() throws Exception {
1100+
return new Mongo();
1101+
}
1102+
1103+
@Bean
1104+
@Override
1105+
public MappingMongoConverter mappingMongoConverter() throws Exception {
1106+
MappingMongoConverter mmc = super.mappingMongoConverter();
1107+
mmc.setTypeMapper(customTypeMapper());
1108+
return mmc;
1109+
}
1110+
1111+
@Bean
1112+
public MongoTypeMapper customTypeMapper() {
1113+
return new CustomMongoTypeMapper();
1114+
}
1115+
}</programlisting>
1116+
1117+
<para>Note that we are extending the
1118+
<classname>AbstractMongoConfiguration</classname> class and override
1119+
the bean definition of the
1120+
<classname>MappingMongoConverter</classname> where we configure our
1121+
custom <classname>MongoTypeMapper</classname>. </para>
1122+
</example>
1123+
1124+
<example>
1125+
<title>Configuring a custom MongoTypeMapper via XML</title>
1126+
1127+
<programlisting language="xml">&lt;mongo:mapping-converter type-mapper-ref="customMongoTypeMapper"/&gt;
1128+
1129+
&lt;bean name="customMongoTypeMapper" class="com.bubu.mongo.CustomMongoTypeMapper"/&gt;</programlisting>
1130+
</example>
10601131
</simplesect>
10611132
</section>
10621133

0 commit comments

Comments
 (0)