Skip to content

Commit 73a6015

Browse files
committed
DATAMONGO-1211 - Adapt to changes in Spring Data Commons.
Tweaked method signatures in MongoRepositoryFactory after some signature changes in Spring Data Commons. Use newly introduced getTragetRepositoryViaReflection(…) to obtain the repository instance via the super class. Added repositoryBaseClass() attribute to @EnableMongoRepositories. Related tickets: DATACMNS-542.
1 parent 67cf0e6 commit 73a6015

File tree

2 files changed

+40
-38
lines changed

2 files changed

+40
-38
lines changed

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

+9
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.springframework.context.annotation.Import;
2828
import org.springframework.data.mongodb.core.MongoTemplate;
2929
import org.springframework.data.mongodb.repository.support.MongoRepositoryFactoryBean;
30+
import org.springframework.data.repository.config.DefaultRepositoryBaseClass;
3031
import org.springframework.data.repository.query.QueryLookupStrategy;
3132
import org.springframework.data.repository.query.QueryLookupStrategy.Key;
3233

@@ -107,6 +108,14 @@
107108
*/
108109
Class<?> repositoryFactoryBeanClass() default MongoRepositoryFactoryBean.class;
109110

111+
/**
112+
* Configure the repository base class to be used to create repository proxies for this particular configuration.
113+
*
114+
* @return
115+
* @since 1.8
116+
*/
117+
Class<?> repositoryBaseClass() default DefaultRepositoryBaseClass.class;
118+
110119
/**
111120
* Configures the name of the {@link MongoTemplate} bean to be used with the repositories detected.
112121
*

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/MongoRepositoryFactory.java

+31-38
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2012 the original author or authors.
2+
* Copyright 2010-2015 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.
@@ -32,6 +32,7 @@
3232
import org.springframework.data.mongodb.repository.query.StringBasedMongoQuery;
3333
import org.springframework.data.querydsl.QueryDslPredicateExecutor;
3434
import org.springframework.data.repository.core.NamedQueries;
35+
import org.springframework.data.repository.core.RepositoryInformation;
3536
import org.springframework.data.repository.core.RepositoryMetadata;
3637
import org.springframework.data.repository.core.support.RepositoryFactorySupport;
3738
import org.springframework.data.repository.query.QueryLookupStrategy;
@@ -52,11 +53,12 @@ public class MongoRepositoryFactory extends RepositoryFactorySupport {
5253
/**
5354
* Creates a new {@link MongoRepositoryFactory} with the given {@link MongoOperations}.
5455
*
55-
* @param mongoOperations must not be {@literal null}
56+
* @param mongoOperations must not be {@literal null}.
5657
*/
5758
public MongoRepositoryFactory(MongoOperations mongoOperations) {
5859

5960
Assert.notNull(mongoOperations);
61+
6062
this.mongoOperations = mongoOperations;
6163
this.mappingContext = mongoOperations.getConverter().getMappingContext();
6264
}
@@ -67,31 +69,22 @@ public MongoRepositoryFactory(MongoOperations mongoOperations) {
6769
*/
6870
@Override
6971
protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) {
70-
return isQueryDslRepository(metadata.getRepositoryInterface()) ? QueryDslMongoRepository.class
71-
: SimpleMongoRepository.class;
72+
73+
boolean isQueryDslRepository = QUERY_DSL_PRESENT
74+
&& QueryDslPredicateExecutor.class.isAssignableFrom(metadata.getRepositoryInterface());
75+
76+
return isQueryDslRepository ? QueryDslMongoRepository.class : SimpleMongoRepository.class;
7277
}
7378

7479
/*
7580
* (non-Javadoc)
76-
* @see org.springframework.data.repository.core.support.RepositoryFactorySupport#getTargetRepository(org.springframework.data.repository.core.RepositoryMetadata)
81+
* @see org.springframework.data.repository.core.support.RepositoryFactorySupport#getTargetRepository(org.springframework.data.repository.core.RepositoryInformation)
7782
*/
7883
@Override
79-
@SuppressWarnings({ "rawtypes", "unchecked" })
80-
protected Object getTargetRepository(RepositoryMetadata metadata) {
84+
protected Object getTargetRepository(RepositoryInformation information) {
8185

82-
Class<?> repositoryInterface = metadata.getRepositoryInterface();
83-
MongoEntityInformation<?, Serializable> entityInformation = getEntityInformation(metadata.getDomainType());
84-
85-
if (isQueryDslRepository(repositoryInterface)) {
86-
return new QueryDslMongoRepository(entityInformation, mongoOperations);
87-
} else {
88-
return new SimpleMongoRepository(entityInformation, mongoOperations);
89-
}
90-
}
91-
92-
private static boolean isQueryDslRepository(Class<?> repositoryInterface) {
93-
94-
return QUERY_DSL_PRESENT && QueryDslPredicateExecutor.class.isAssignableFrom(repositoryInterface);
86+
MongoEntityInformation<?, Serializable> entityInformation = getEntityInformation(information.getDomainType());
87+
return getTargetRepositoryViaReflection(information, entityInformation, mongoOperations);
9588
}
9689

9790
/*
@@ -103,6 +96,24 @@ protected QueryLookupStrategy getQueryLookupStrategy(Key key) {
10396
return new MongoQueryLookupStrategy();
10497
}
10598

99+
/*
100+
* (non-Javadoc)
101+
* @see org.springframework.data.repository.core.support.RepositoryFactorySupport#getEntityInformation(java.lang.Class)
102+
*/
103+
@Override
104+
@SuppressWarnings("unchecked")
105+
public <T, ID extends Serializable> MongoEntityInformation<T, ID> getEntityInformation(Class<T> domainClass) {
106+
107+
MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(domainClass);
108+
109+
if (entity == null) {
110+
throw new MappingException(String.format("Could not lookup mapping metadata for domain class %s!",
111+
domainClass.getName()));
112+
}
113+
114+
return new MappingMongoEntityInformation<T, ID>((MongoPersistentEntity<T>) entity);
115+
}
116+
106117
/**
107118
* {@link QueryLookupStrategy} to create {@link PartTreeMongoQuery} instances.
108119
*
@@ -129,22 +140,4 @@ public RepositoryQuery resolveQuery(Method method, RepositoryMetadata metadata,
129140
}
130141
}
131142
}
132-
133-
/*
134-
* (non-Javadoc)
135-
* @see org.springframework.data.repository.core.support.RepositoryFactorySupport#getEntityInformation(java.lang.Class)
136-
*/
137-
@Override
138-
@SuppressWarnings("unchecked")
139-
public <T, ID extends Serializable> MongoEntityInformation<T, ID> getEntityInformation(Class<T> domainClass) {
140-
141-
MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(domainClass);
142-
143-
if (entity == null) {
144-
throw new MappingException(String.format("Could not lookup mapping metadata for domain class %s!",
145-
domainClass.getName()));
146-
}
147-
148-
return new MappingMongoEntityInformation<T, ID>((MongoPersistentEntity<T>) entity);
149-
}
150143
}

0 commit comments

Comments
 (0)