Skip to content

Commit d0da787

Browse files
committed
Removed mapping-context-ref from repositories namespace.
We now take the mapping context from the wired MongoTemplate.
1 parent e89d09c commit d0da787

File tree

7 files changed

+22
-94
lines changed

7 files changed

+22
-94
lines changed

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

Lines changed: 3 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,9 @@
1515
*/
1616
package org.springframework.data.document.mongodb.config;
1717

18-
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
1918
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
2019
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
2120
import org.springframework.data.document.mongodb.config.SimpleMongoRepositoryConfiguration.MongoRepositoryConfiguration;
22-
import org.springframework.data.mapping.model.MappingContext;
2321
import org.springframework.data.repository.config.AbstractRepositoryConfigDefinitionParser;
2422
import org.w3c.dom.Element;
2523

@@ -32,15 +30,10 @@
3230
public class MongoRepositoryConfigParser extends
3331
AbstractRepositoryConfigDefinitionParser<SimpleMongoRepositoryConfiguration, MongoRepositoryConfiguration> {
3432

35-
private static final String MAPPING_CONTEXT_DEFAULT = BeanNames.MAPPING_CONTEXT;
36-
3733
/*
38-
* (non-Javadoc)
39-
*
40-
* @see org.springframework.data.repository.config.
41-
* AbstractRepositoryConfigDefinitionParser
42-
* #getGlobalRepositoryConfigInformation(org.w3c.dom.Element)
43-
*/
34+
* (non-Javadoc)
35+
* @see org.springframework.data.repository.config.AbstractRepositoryConfigDefinitionParser#getGlobalRepositoryConfigInformation(org.w3c.dom.Element)
36+
*/
4437
@Override
4538
protected SimpleMongoRepositoryConfiguration getGlobalRepositoryConfigInformation(Element element) {
4639

@@ -56,35 +49,5 @@ protected void postProcessBeanDefinition(MongoRepositoryConfiguration context, B
5649
BeanDefinitionRegistry registry, Object beanSource) {
5750

5851
builder.addPropertyReference("template", context.getMongoTemplateRef());
59-
60-
String mappingContextRef = getMappingContextReference(context, registry);
61-
if (mappingContextRef != null) {
62-
builder.addPropertyReference("mappingContext", mappingContextRef);
63-
}
64-
}
65-
66-
/**
67-
* Returns the bean name of a {@link MappingContext} to be wired. Will inspect the namespace attribute first and if no
68-
* config is found in that place it will try to lookup the default one. Will return {@literal null} if neither one is
69-
* available.
70-
*
71-
* @param config
72-
* @param registry
73-
* @return
74-
*/
75-
private String getMappingContextReference(MongoRepositoryConfiguration config, BeanDefinitionRegistry registry) {
76-
77-
String contextRef = config.getMappingContextRef();
78-
79-
if (contextRef != null) {
80-
return contextRef;
81-
}
82-
83-
try {
84-
registry.getBeanDefinition(MAPPING_CONTEXT_DEFAULT);
85-
return MAPPING_CONTEXT_DEFAULT;
86-
} catch (NoSuchBeanDefinitionException e) {
87-
return null;
88-
}
8952
}
9053
}

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

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ public class SimpleMongoRepositoryConfiguration
3636
private static final String MONGO_TEMPLATE_REF = "mongo-template-ref";
3737
private static final String DEFAULT_MONGO_TEMPLATE_REF = "mongoTemplate";
3838

39-
private static final String MAPPING_CONTEXT_REF = "mongo-mapping-context-ref";
40-
4139
/**
4240
* Creates a new {@link SimpleMongoRepositoryConfiguration} for the given {@link Element}.
4341
*
@@ -59,12 +57,6 @@ public String getMongoTemplateRef() {
5957
return StringUtils.hasText(templateRef) ? templateRef : DEFAULT_MONGO_TEMPLATE_REF;
6058
}
6159

62-
public String getMappingContextRef() {
63-
64-
String attribute = getSource().getAttribute(MAPPING_CONTEXT_REF);
65-
return StringUtils.hasText(attribute) ? attribute : null;
66-
}
67-
6860
/*
6961
* (non-Javadoc)
7062
*
@@ -98,8 +90,6 @@ public interface MongoRepositoryConfiguration extends
9890
SingleRepositoryConfigInformation<SimpleMongoRepositoryConfiguration> {
9991

10092
String getMongoTemplateRef();
101-
102-
String getMappingContextRef();
10393
}
10494

10595
/**
@@ -132,13 +122,6 @@ public String getMongoTemplateRef() {
132122

133123
return getAttribute(MONGO_TEMPLATE_REF);
134124
}
135-
136-
/* (non-Javadoc)
137-
* @see org.springframework.data.document.mongodb.config.SimpleMongoRepositoryConfiguration.MongoRepositoryConfiguration#getMappingContextRef()
138-
*/
139-
public String getMappingContextRef() {
140-
return getAttribute(MAPPING_CONTEXT_REF);
141-
}
142125
}
143126

144127
/**
@@ -171,12 +154,5 @@ public String getMongoTemplateRef() {
171154

172155
return getParent().getMongoTemplateRef();
173156
}
174-
175-
/* (non-Javadoc)
176-
* @see org.springframework.data.document.mongodb.config.SimpleMongoRepositoryConfiguration.MongoRepositoryConfiguration#getMappingContextRef()
177-
*/
178-
public String getMappingContextRef() {
179-
return getParent().getMappingContextRef();
180-
}
181157
}
182158
}

spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/MongoRepositoryFactoryBean.java

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ public class MongoRepositoryFactoryBean<T extends Repository<S, ID>, S, ID exten
5757
RepositoryFactoryBeanSupport<T, S, ID> {
5858

5959
private MongoTemplate template;
60-
private MappingContext<MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext;
6160

6261
/**
6362
* Configures the {@link MongoTemplate} to be used.
@@ -70,16 +69,6 @@ public void setTemplate(MongoTemplate template) {
7069
this.template = template;
7170
}
7271

73-
/**
74-
* Sets the {@link MappingContext} used with the underlying {@link MongoTemplate}.
75-
*
76-
* @param mappingContext
77-
* the mappingContext to set
78-
*/
79-
public void setMappingContext(MappingContext<MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext) {
80-
this.mappingContext = mappingContext;
81-
}
82-
8372
/*
8473
* (non-Javadoc)
8574
*
@@ -90,7 +79,7 @@ public void setMappingContext(MappingContext<MongoPersistentEntity<?>, MongoPers
9079
@Override
9180
protected RepositoryFactorySupport createRepositoryFactory() {
9281

93-
MongoRepositoryFactory factory = new MongoRepositoryFactory(template, mappingContext);
82+
MongoRepositoryFactory factory = new MongoRepositoryFactory(template);
9483
factory.addQueryCreationListener(new IndexEnsuringQueryCreationListener(template));
9584
return factory;
9685
}
@@ -107,7 +96,6 @@ public void afterPropertiesSet() {
10796

10897
super.afterPropertiesSet();
10998
Assert.notNull(template, "MongoTemplate must not be null!");
110-
Assert.notNull(mappingContext, "MappingContext must not be null!");
11199
}
112100

113101
/**
@@ -127,13 +115,12 @@ public static class MongoRepositoryFactory extends RepositoryFactorySupport {
127115
* must not be {@literal null}
128116
* @param mappingContext
129117
*/
130-
public MongoRepositoryFactory(MongoTemplate template,
131-
MappingContext<MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext) {
118+
public MongoRepositoryFactory(MongoTemplate template) {
132119

133120
Assert.notNull(template);
134-
Assert.notNull(mappingContext);
135121
this.template = template;
136-
this.entityInformationCreator = new EntityInformationCreator(mappingContext);
122+
this.entityInformationCreator = new EntityInformationCreator(template.getConverter()
123+
.getMappingContext());
137124
}
138125

139126
/*
@@ -252,9 +239,9 @@ public <T, ID extends Serializable> MongoEntityInformation<T, ID> getEntityInfor
252239
*/
253240
static class EntityInformationCreator {
254241

255-
private final MappingContext<MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext;
242+
private final MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext;
256243

257-
public EntityInformationCreator(MappingContext<MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext) {
244+
public EntityInformationCreator(MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext) {
258245
Assert.notNull(mappingContext);
259246
this.mappingContext = mappingContext;
260247
}

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,6 @@ The password to use when connecting to a MongoDB server.
116116
</xsd:documentation>
117117
</xsd:annotation>
118118
</xsd:attribute>
119-
<xsd:attribute name="mongo-mapping-context-ref" type="mappingContextRef" default="mappingContext">
120-
<xsd:annotation>
121-
<xsd:documentation>
122-
The reference to a MappingContext. Will pick up a bean named 'mappingContext' by default if available.
123-
</xsd:documentation>
124-
</xsd:annotation>
125-
</xsd:attribute>
126119
</xsd:attributeGroup>
127120

128121
<xsd:element name="repositories">

spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/repository/MongoRepositoryFactoryUnitTests.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@
2020

2121
import java.io.Serializable;
2222

23+
import org.junit.Before;
2324
import org.junit.Test;
2425
import org.junit.runner.RunWith;
2526
import org.mockito.Mock;
2627
import org.mockito.runners.MockitoJUnitRunner;
2728
import org.springframework.data.document.mongodb.MongoTemplate;
29+
import org.springframework.data.document.mongodb.convert.MongoConverter;
2830
import org.springframework.data.document.mongodb.mapping.MongoPersistentEntity;
2931
import org.springframework.data.document.mongodb.mapping.MongoPersistentProperty;
3032
import org.springframework.data.document.mongodb.repository.MongoRepositoryFactoryBean.MongoRepositoryFactory;
@@ -40,17 +42,26 @@ public class MongoRepositoryFactoryUnitTests {
4042

4143
@Mock
4244
MongoTemplate template;
45+
46+
@Mock
47+
MongoConverter converter;
4348

4449
@Mock
4550
MappingContext<MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext;
4651

4752
@Mock
4853
@SuppressWarnings("rawtypes")
4954
MongoPersistentEntity entity;
55+
56+
@Before
57+
public void setUp() {
58+
when(template.getConverter()).thenReturn(converter);
59+
when(converter.getMappingContext()).thenReturn((MappingContext) mappingContext);
60+
}
5061

5162
@Test(expected = IllegalArgumentException.class)
5263
public void rejectsInvalidIdType() throws Exception {
53-
MongoRepositoryFactory factory = new MongoRepositoryFactory(template, null);
64+
MongoRepositoryFactory factory = new MongoRepositoryFactory(template);
5465
factory.getRepository(SampleRepository.class);
5566
}
5667

@@ -61,7 +72,7 @@ public void usesMappingMongoEntityInformationIfMappingContextSet() {
6172
when(mappingContext.getPersistentEntity(Person.class)).thenReturn(entity);
6273
when(entity.getType()).thenReturn(Person.class);
6374

64-
MongoRepositoryFactory factory = new MongoRepositoryFactory(template, mappingContext);
75+
MongoRepositoryFactory factory = new MongoRepositoryFactory(template);
6576
MongoEntityInformation<Person, Serializable> entityInformation = factory.getEntityInformation(Person.class);
6677
assertTrue(entityInformation instanceof MappingMongoEntityInformation);
6778
}

spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/repository/config/MongoNamespaceIntegrationTests.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,5 @@ public void assertDefaultMappingContextIsWired() {
2525
getClass()));
2626
BeanDefinition definition = factory.getBeanDefinition("personRepository");
2727
assertThat(definition, is(notNullValue()));
28-
assertThat(definition.getPropertyValues().getPropertyValue("mappingContext"), is(notNullValue()));
2928
}
3029
}

spring-data-mongodb/src/test/resources/org/springframework/data/document/mongodb/repository/PersonRepositoryIntegrationTests-context.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
<bean class="org.springframework.data.document.mongodb.repository.MongoRepositoryFactoryBean">
1717
<property name="template" ref="mongoTemplate"/>
18-
<property name="mappingContext" ref="mappingContext"/>
1918
<property name="repositoryInterface" value="org.springframework.data.document.mongodb.repository.PersonRepository"/>
2019
</bean>
2120

0 commit comments

Comments
 (0)