diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/config/EnableMongoRepositories.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/config/EnableMongoRepositories.java index 21606d460..fcb458bd2 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/config/EnableMongoRepositories.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/config/EnableMongoRepositories.java @@ -27,6 +27,7 @@ import org.springframework.context.annotation.ComponentScan.Filter; import org.springframework.context.annotation.Import; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.repository.support.MongoRepositoryFactoryBean; +import org.springframework.data.repository.config.DefaultRepositoryBaseClass; import org.springframework.data.repository.query.QueryLookupStrategy; import org.springframework.data.repository.query.QueryLookupStrategy.Key; @@ -107,6 +108,14 @@ public @interface EnableMongoRepositories { */ Class repositoryFactoryBeanClass() default MongoRepositoryFactoryBean.class; + /** + * Configure the repository base class to be used to create repository proxies for this particular configuration. + * + * @return + * @since 1.8 + */ + Class repositoryBaseClass() default DefaultRepositoryBaseClass.class; + /** * Configures the name of the {@link MongoTemplate} bean to be used with the repositories detected. * diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/MongoRepositoryFactory.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/MongoRepositoryFactory.java index 85d048ba3..644fed6f5 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/MongoRepositoryFactory.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/MongoRepositoryFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2010-2012 the original author or authors. + * Copyright 2010-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -32,6 +32,7 @@ import org.springframework.data.mongodb.repository.query.PartTreeMongoQuery; import org.springframework.data.mongodb.repository.query.StringBasedMongoQuery; import org.springframework.data.querydsl.QueryDslPredicateExecutor; import org.springframework.data.repository.core.NamedQueries; +import org.springframework.data.repository.core.RepositoryInformation; import org.springframework.data.repository.core.RepositoryMetadata; import org.springframework.data.repository.core.support.RepositoryFactorySupport; import org.springframework.data.repository.query.QueryLookupStrategy; @@ -52,11 +53,12 @@ public class MongoRepositoryFactory extends RepositoryFactorySupport { /** * Creates a new {@link MongoRepositoryFactory} with the given {@link MongoOperations}. * - * @param mongoOperations must not be {@literal null} + * @param mongoOperations must not be {@literal null}. */ public MongoRepositoryFactory(MongoOperations mongoOperations) { Assert.notNull(mongoOperations); + this.mongoOperations = mongoOperations; this.mappingContext = mongoOperations.getConverter().getMappingContext(); } @@ -67,31 +69,22 @@ public class MongoRepositoryFactory extends RepositoryFactorySupport { */ @Override protected Class getRepositoryBaseClass(RepositoryMetadata metadata) { - return isQueryDslRepository(metadata.getRepositoryInterface()) ? QueryDslMongoRepository.class - : SimpleMongoRepository.class; + + boolean isQueryDslRepository = QUERY_DSL_PRESENT + && QueryDslPredicateExecutor.class.isAssignableFrom(metadata.getRepositoryInterface()); + + return isQueryDslRepository ? QueryDslMongoRepository.class : SimpleMongoRepository.class; } /* * (non-Javadoc) - * @see org.springframework.data.repository.core.support.RepositoryFactorySupport#getTargetRepository(org.springframework.data.repository.core.RepositoryMetadata) + * @see org.springframework.data.repository.core.support.RepositoryFactorySupport#getTargetRepository(org.springframework.data.repository.core.RepositoryInformation) */ @Override - @SuppressWarnings({ "rawtypes", "unchecked" }) - protected Object getTargetRepository(RepositoryMetadata metadata) { + protected Object getTargetRepository(RepositoryInformation information) { - Class repositoryInterface = metadata.getRepositoryInterface(); - MongoEntityInformation entityInformation = getEntityInformation(metadata.getDomainType()); - - if (isQueryDslRepository(repositoryInterface)) { - return new QueryDslMongoRepository(entityInformation, mongoOperations); - } else { - return new SimpleMongoRepository(entityInformation, mongoOperations); - } - } - - private static boolean isQueryDslRepository(Class repositoryInterface) { - - return QUERY_DSL_PRESENT && QueryDslPredicateExecutor.class.isAssignableFrom(repositoryInterface); + MongoEntityInformation entityInformation = getEntityInformation(information.getDomainType()); + return getTargetRepositoryViaReflection(information, entityInformation, mongoOperations); } /* @@ -103,6 +96,24 @@ public class MongoRepositoryFactory extends RepositoryFactorySupport { return new MongoQueryLookupStrategy(); } + /* + * (non-Javadoc) + * @see org.springframework.data.repository.core.support.RepositoryFactorySupport#getEntityInformation(java.lang.Class) + */ + @Override + @SuppressWarnings("unchecked") + public MongoEntityInformation getEntityInformation(Class domainClass) { + + MongoPersistentEntity entity = mappingContext.getPersistentEntity(domainClass); + + if (entity == null) { + throw new MappingException(String.format("Could not lookup mapping metadata for domain class %s!", + domainClass.getName())); + } + + return new MappingMongoEntityInformation((MongoPersistentEntity) entity); + } + /** * {@link QueryLookupStrategy} to create {@link PartTreeMongoQuery} instances. * @@ -129,22 +140,4 @@ public class MongoRepositoryFactory extends RepositoryFactorySupport { } } } - - /* - * (non-Javadoc) - * @see org.springframework.data.repository.core.support.RepositoryFactorySupport#getEntityInformation(java.lang.Class) - */ - @Override - @SuppressWarnings("unchecked") - public MongoEntityInformation getEntityInformation(Class domainClass) { - - MongoPersistentEntity entity = mappingContext.getPersistentEntity(domainClass); - - if (entity == null) { - throw new MappingException(String.format("Could not lookup mapping metadata for domain class %s!", - domainClass.getName())); - } - - return new MappingMongoEntityInformation((MongoPersistentEntity) entity); - } }