Removed mapping-context-ref from repositories namespace.

We now take the mapping context from the wired MongoTemplate.
This commit is contained in:
Oliver Gierke
2011-05-20 20:46:35 +02:00
parent e89d09cc86
commit d0da787f70
7 changed files with 22 additions and 94 deletions

View File

@@ -15,11 +15,9 @@
*/
package org.springframework.data.document.mongodb.config;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.data.document.mongodb.config.SimpleMongoRepositoryConfiguration.MongoRepositoryConfiguration;
import org.springframework.data.mapping.model.MappingContext;
import org.springframework.data.repository.config.AbstractRepositoryConfigDefinitionParser;
import org.w3c.dom.Element;
@@ -32,15 +30,10 @@ import org.w3c.dom.Element;
public class MongoRepositoryConfigParser extends
AbstractRepositoryConfigDefinitionParser<SimpleMongoRepositoryConfiguration, MongoRepositoryConfiguration> {
private static final String MAPPING_CONTEXT_DEFAULT = BeanNames.MAPPING_CONTEXT;
/*
* (non-Javadoc)
*
* @see org.springframework.data.repository.config.
* AbstractRepositoryConfigDefinitionParser
* #getGlobalRepositoryConfigInformation(org.w3c.dom.Element)
*/
* (non-Javadoc)
* @see org.springframework.data.repository.config.AbstractRepositoryConfigDefinitionParser#getGlobalRepositoryConfigInformation(org.w3c.dom.Element)
*/
@Override
protected SimpleMongoRepositoryConfiguration getGlobalRepositoryConfigInformation(Element element) {
@@ -56,35 +49,5 @@ public class MongoRepositoryConfigParser extends
BeanDefinitionRegistry registry, Object beanSource) {
builder.addPropertyReference("template", context.getMongoTemplateRef());
String mappingContextRef = getMappingContextReference(context, registry);
if (mappingContextRef != null) {
builder.addPropertyReference("mappingContext", mappingContextRef);
}
}
/**
* Returns the bean name of a {@link MappingContext} to be wired. Will inspect the namespace attribute first and if no
* config is found in that place it will try to lookup the default one. Will return {@literal null} if neither one is
* available.
*
* @param config
* @param registry
* @return
*/
private String getMappingContextReference(MongoRepositoryConfiguration config, BeanDefinitionRegistry registry) {
String contextRef = config.getMappingContextRef();
if (contextRef != null) {
return contextRef;
}
try {
registry.getBeanDefinition(MAPPING_CONTEXT_DEFAULT);
return MAPPING_CONTEXT_DEFAULT;
} catch (NoSuchBeanDefinitionException e) {
return null;
}
}
}

View File

@@ -36,8 +36,6 @@ public class SimpleMongoRepositoryConfiguration
private static final String MONGO_TEMPLATE_REF = "mongo-template-ref";
private static final String DEFAULT_MONGO_TEMPLATE_REF = "mongoTemplate";
private static final String MAPPING_CONTEXT_REF = "mongo-mapping-context-ref";
/**
* Creates a new {@link SimpleMongoRepositoryConfiguration} for the given {@link Element}.
*
@@ -59,12 +57,6 @@ public class SimpleMongoRepositoryConfiguration
return StringUtils.hasText(templateRef) ? templateRef : DEFAULT_MONGO_TEMPLATE_REF;
}
public String getMappingContextRef() {
String attribute = getSource().getAttribute(MAPPING_CONTEXT_REF);
return StringUtils.hasText(attribute) ? attribute : null;
}
/*
* (non-Javadoc)
*
@@ -98,8 +90,6 @@ public class SimpleMongoRepositoryConfiguration
SingleRepositoryConfigInformation<SimpleMongoRepositoryConfiguration> {
String getMongoTemplateRef();
String getMappingContextRef();
}
/**
@@ -132,13 +122,6 @@ public class SimpleMongoRepositoryConfiguration
return getAttribute(MONGO_TEMPLATE_REF);
}
/* (non-Javadoc)
* @see org.springframework.data.document.mongodb.config.SimpleMongoRepositoryConfiguration.MongoRepositoryConfiguration#getMappingContextRef()
*/
public String getMappingContextRef() {
return getAttribute(MAPPING_CONTEXT_REF);
}
}
/**
@@ -171,12 +154,5 @@ public class SimpleMongoRepositoryConfiguration
return getParent().getMongoTemplateRef();
}
/* (non-Javadoc)
* @see org.springframework.data.document.mongodb.config.SimpleMongoRepositoryConfiguration.MongoRepositoryConfiguration#getMappingContextRef()
*/
public String getMappingContextRef() {
return getParent().getMappingContextRef();
}
}
}

View File

@@ -57,7 +57,6 @@ public class MongoRepositoryFactoryBean<T extends Repository<S, ID>, S, ID exten
RepositoryFactoryBeanSupport<T, S, ID> {
private MongoTemplate template;
private MappingContext<MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext;
/**
* Configures the {@link MongoTemplate} to be used.
@@ -70,16 +69,6 @@ public class MongoRepositoryFactoryBean<T extends Repository<S, ID>, S, ID exten
this.template = template;
}
/**
* Sets the {@link MappingContext} used with the underlying {@link MongoTemplate}.
*
* @param mappingContext
* the mappingContext to set
*/
public void setMappingContext(MappingContext<MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext) {
this.mappingContext = mappingContext;
}
/*
* (non-Javadoc)
*
@@ -90,7 +79,7 @@ public class MongoRepositoryFactoryBean<T extends Repository<S, ID>, S, ID exten
@Override
protected RepositoryFactorySupport createRepositoryFactory() {
MongoRepositoryFactory factory = new MongoRepositoryFactory(template, mappingContext);
MongoRepositoryFactory factory = new MongoRepositoryFactory(template);
factory.addQueryCreationListener(new IndexEnsuringQueryCreationListener(template));
return factory;
}
@@ -107,7 +96,6 @@ public class MongoRepositoryFactoryBean<T extends Repository<S, ID>, S, ID exten
super.afterPropertiesSet();
Assert.notNull(template, "MongoTemplate must not be null!");
Assert.notNull(mappingContext, "MappingContext must not be null!");
}
/**
@@ -127,13 +115,12 @@ public class MongoRepositoryFactoryBean<T extends Repository<S, ID>, S, ID exten
* must not be {@literal null}
* @param mappingContext
*/
public MongoRepositoryFactory(MongoTemplate template,
MappingContext<MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext) {
public MongoRepositoryFactory(MongoTemplate template) {
Assert.notNull(template);
Assert.notNull(mappingContext);
this.template = template;
this.entityInformationCreator = new EntityInformationCreator(mappingContext);
this.entityInformationCreator = new EntityInformationCreator(template.getConverter()
.getMappingContext());
}
/*
@@ -252,9 +239,9 @@ public class MongoRepositoryFactoryBean<T extends Repository<S, ID>, S, ID exten
*/
static class EntityInformationCreator {
private final MappingContext<MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext;
private final MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext;
public EntityInformationCreator(MappingContext<MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext) {
public EntityInformationCreator(MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext) {
Assert.notNull(mappingContext);
this.mappingContext = mappingContext;
}

View File

@@ -116,13 +116,6 @@ The password to use when connecting to a MongoDB server.
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="mongo-mapping-context-ref" type="mappingContextRef" default="mappingContext">
<xsd:annotation>
<xsd:documentation>
The reference to a MappingContext. Will pick up a bean named 'mappingContext' by default if available.
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
</xsd:attributeGroup>
<xsd:element name="repositories">

View File

@@ -20,11 +20,13 @@ import static org.mockito.Mockito.*;
import java.io.Serializable;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.data.document.mongodb.MongoTemplate;
import org.springframework.data.document.mongodb.convert.MongoConverter;
import org.springframework.data.document.mongodb.mapping.MongoPersistentEntity;
import org.springframework.data.document.mongodb.mapping.MongoPersistentProperty;
import org.springframework.data.document.mongodb.repository.MongoRepositoryFactoryBean.MongoRepositoryFactory;
@@ -40,6 +42,9 @@ public class MongoRepositoryFactoryUnitTests {
@Mock
MongoTemplate template;
@Mock
MongoConverter converter;
@Mock
MappingContext<MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext;
@@ -47,10 +52,16 @@ public class MongoRepositoryFactoryUnitTests {
@Mock
@SuppressWarnings("rawtypes")
MongoPersistentEntity entity;
@Before
public void setUp() {
when(template.getConverter()).thenReturn(converter);
when(converter.getMappingContext()).thenReturn((MappingContext) mappingContext);
}
@Test(expected = IllegalArgumentException.class)
public void rejectsInvalidIdType() throws Exception {
MongoRepositoryFactory factory = new MongoRepositoryFactory(template, null);
MongoRepositoryFactory factory = new MongoRepositoryFactory(template);
factory.getRepository(SampleRepository.class);
}
@@ -61,7 +72,7 @@ public class MongoRepositoryFactoryUnitTests {
when(mappingContext.getPersistentEntity(Person.class)).thenReturn(entity);
when(entity.getType()).thenReturn(Person.class);
MongoRepositoryFactory factory = new MongoRepositoryFactory(template, mappingContext);
MongoRepositoryFactory factory = new MongoRepositoryFactory(template);
MongoEntityInformation<Person, Serializable> entityInformation = factory.getEntityInformation(Person.class);
assertTrue(entityInformation instanceof MappingMongoEntityInformation);
}

View File

@@ -25,6 +25,5 @@ public class MongoNamespaceIntegrationTests extends AbstractPersonRepositoryInte
getClass()));
BeanDefinition definition = factory.getBeanDefinition("personRepository");
assertThat(definition, is(notNullValue()));
assertThat(definition.getPropertyValues().getPropertyValue("mappingContext"), is(notNullValue()));
}
}

View File

@@ -15,7 +15,6 @@
<bean class="org.springframework.data.document.mongodb.repository.MongoRepositoryFactoryBean">
<property name="template" ref="mongoTemplate"/>
<property name="mappingContext" ref="mappingContext"/>
<property name="repositoryInterface" value="org.springframework.data.document.mongodb.repository.PersonRepository"/>
</bean>