18
18
import static org .springframework .data .querydsl .QueryDslUtils .*;
19
19
20
20
import java .io .Serializable ;
21
+ import java .lang .reflect .Constructor ;
21
22
import java .lang .reflect .Method ;
22
23
24
+ import org .springframework .beans .BeanUtils ;
23
25
import org .springframework .data .keyvalue .core .KeyValueOperations ;
24
26
import org .springframework .data .keyvalue .repository .query .KeyValuePartTreeQuery ;
25
27
import org .springframework .data .keyvalue .repository .query .SpelQueryCreator ;
40
42
import org .springframework .data .repository .query .RepositoryQuery ;
41
43
import org .springframework .data .repository .query .parser .AbstractQueryCreator ;
42
44
import org .springframework .util .Assert ;
45
+ import org .springframework .util .ClassUtils ;
43
46
44
47
/**
45
48
* {@link RepositoryFactorySupport} specific of handing
@@ -55,6 +58,7 @@ public class KeyValueRepositoryFactory extends RepositoryFactorySupport {
55
58
private final KeyValueOperations keyValueOperations ;
56
59
private final MappingContext <?, ?> context ;
57
60
private final Class <? extends AbstractQueryCreator <?, ?>> queryCreator ;
61
+ private final Class <? extends RepositoryQuery > repositoryQueryType ;
58
62
59
63
/**
60
64
* Creates a new {@link KeyValueRepositoryFactory} for the given {@link KeyValueOperations}.
@@ -75,12 +79,29 @@ public KeyValueRepositoryFactory(KeyValueOperations keyValueOperations) {
75
79
public KeyValueRepositoryFactory (KeyValueOperations keyValueOperations ,
76
80
Class <? extends AbstractQueryCreator <?, ?>> queryCreator ) {
77
81
82
+ this (keyValueOperations , queryCreator , KeyValuePartTreeQuery .class );
83
+ }
84
+
85
+ /**
86
+ * Creates a new {@link KeyValueRepositoryFactory} for the given {@link KeyValueOperations} and
87
+ * {@link AbstractQueryCreator}-type.
88
+ *
89
+ * @param keyValueOperations must not be {@literal null}.
90
+ * @param queryCreator must not be {@literal null}.
91
+ * @param repositoryQueryType must not be {@literal null}.
92
+ * @since 1.1
93
+ */
94
+ public KeyValueRepositoryFactory (KeyValueOperations keyValueOperations ,
95
+ Class <? extends AbstractQueryCreator <?, ?>> queryCreator , Class <? extends RepositoryQuery > repositoryQueryType ) {
96
+
78
97
Assert .notNull (keyValueOperations , "KeyValueOperations must not be null!" );
79
98
Assert .notNull (queryCreator , "Query creator type must not be null!" );
99
+ Assert .notNull (repositoryQueryType , "RepositoryQueryType type must not be null!" );
80
100
81
101
this .queryCreator = queryCreator ;
82
102
this .keyValueOperations = keyValueOperations ;
83
103
this .context = keyValueOperations .getMappingContext ();
104
+ this .repositoryQueryType = repositoryQueryType ;
84
105
}
85
106
86
107
/*
@@ -134,7 +155,8 @@ private static boolean isQueryDslRepository(Class<?> repositoryInterface) {
134
155
*/
135
156
@ Override
136
157
protected QueryLookupStrategy getQueryLookupStrategy (Key key , EvaluationContextProvider evaluationContextProvider ) {
137
- return new KeyValueQueryLookupStrategy (key , evaluationContextProvider , this .keyValueOperations , this .queryCreator );
158
+ return new KeyValueQueryLookupStrategy (key , evaluationContextProvider , this .keyValueOperations , this .queryCreator ,
159
+ this .repositoryQueryType );
138
160
}
139
161
140
162
/**
@@ -147,6 +169,7 @@ private static class KeyValueQueryLookupStrategy implements QueryLookupStrategy
147
169
private KeyValueOperations keyValueOperations ;
148
170
149
171
private Class <? extends AbstractQueryCreator <?, ?>> queryCreator ;
172
+ private Class <? extends RepositoryQuery > repositoryQueryType ;
150
173
151
174
/**
152
175
* Creates a new {@link KeyValueQueryLookupStrategy} for the given {@link Key}, {@link EvaluationContextProvider},
@@ -161,26 +184,47 @@ private static class KeyValueQueryLookupStrategy implements QueryLookupStrategy
161
184
*/
162
185
public KeyValueQueryLookupStrategy (Key key , EvaluationContextProvider evaluationContextProvider ,
163
186
KeyValueOperations keyValueOperations , Class <? extends AbstractQueryCreator <?, ?>> queryCreator ) {
187
+ this (key , evaluationContextProvider , keyValueOperations , queryCreator , KeyValuePartTreeQuery .class );
188
+ }
189
+
190
+ /**
191
+ * @param key
192
+ * @param evaluationContextProvider
193
+ * @param keyValueOperations
194
+ * @param queryCreator
195
+ * @since 1.1
196
+ */
197
+ public KeyValueQueryLookupStrategy (Key key , EvaluationContextProvider evaluationContextProvider ,
198
+ KeyValueOperations keyValueOperations , Class <? extends AbstractQueryCreator <?, ?>> queryCreator ,
199
+ Class <? extends RepositoryQuery > repositoryQueryType ) {
164
200
165
201
Assert .notNull (evaluationContextProvider , "EvaluationContextProvider must not be null!" );
166
202
Assert .notNull (keyValueOperations , "KeyValueOperations must not be null!" );
167
203
Assert .notNull (queryCreator , "Query creator type must not be null!" );
204
+ Assert .notNull (repositoryQueryType , "RepositoryQueryType type must not be null!" );
168
205
169
206
this .evaluationContextProvider = evaluationContextProvider ;
170
207
this .keyValueOperations = keyValueOperations ;
171
208
this .queryCreator = queryCreator ;
209
+ this .repositoryQueryType = repositoryQueryType ;
172
210
}
173
211
174
212
/*
175
213
* (non-Javadoc)
176
214
* @see org.springframework.data.repository.query.QueryLookupStrategy#resolveQuery(java.lang.reflect.Method, org.springframework.data.repository.core.RepositoryMetadata, org.springframework.data.projection.ProjectionFactory, org.springframework.data.repository.core.NamedQueries)
177
215
*/
178
216
@ Override
217
+ @ SuppressWarnings ("unchecked" )
179
218
public RepositoryQuery resolveQuery (Method method , RepositoryMetadata metadata , ProjectionFactory factory ,
180
219
NamedQueries namedQueries ) {
181
220
182
221
QueryMethod queryMethod = new QueryMethod (method , metadata , factory );
183
- return new KeyValuePartTreeQuery (queryMethod , evaluationContextProvider , this .keyValueOperations ,
222
+
223
+ Constructor <? extends KeyValuePartTreeQuery > constructor = (Constructor <? extends KeyValuePartTreeQuery >) ClassUtils
224
+ .getConstructorIfAvailable (this .repositoryQueryType , QueryMethod .class , EvaluationContextProvider .class ,
225
+ KeyValueOperations .class , Class .class );
226
+
227
+ return BeanUtils .instantiateClass (constructor , queryMethod , evaluationContextProvider , this .keyValueOperations ,
184
228
this .queryCreator );
185
229
}
186
230
}
0 commit comments