@@ -73,29 +73,27 @@ abstract class JdbcQueryLookupStrategy extends RelationalQueryLookupStrategy {
73
73
private final JdbcConverter converter ;
74
74
private final QueryMappingConfiguration queryMappingConfiguration ;
75
75
private final NamedParameterJdbcOperations operations ;
76
- @ Nullable private final BeanFactory beanfactory ;
77
76
protected final QueryMethodEvaluationContextProvider evaluationContextProvider ;
78
77
79
78
JdbcQueryLookupStrategy (ApplicationEventPublisher publisher , @ Nullable EntityCallbacks callbacks ,
80
79
RelationalMappingContext context , JdbcConverter converter , Dialect dialect ,
81
80
QueryMappingConfiguration queryMappingConfiguration , NamedParameterJdbcOperations operations ,
82
- @ Nullable BeanFactory beanfactory , QueryMethodEvaluationContextProvider evaluationContextProvider ) {
81
+ QueryMethodEvaluationContextProvider evaluationContextProvider ) {
83
82
84
83
super (context , dialect );
85
84
86
85
Assert .notNull (publisher , "ApplicationEventPublisher must not be null" );
87
86
Assert .notNull (converter , "JdbcConverter must not be null" );
88
87
Assert .notNull (queryMappingConfiguration , "QueryMappingConfiguration must not be null" );
89
88
Assert .notNull (operations , "NamedParameterJdbcOperations must not be null" );
90
- Assert .notNull (evaluationContextProvider , "QueryMethodEvaluationContextProvier must not be null" );
89
+ Assert .notNull (evaluationContextProvider , "QueryMethodEvaluationContextProvider must not be null" );
91
90
92
91
this .context = context ;
93
92
this .publisher = publisher ;
94
93
this .callbacks = callbacks ;
95
94
this .converter = converter ;
96
95
this .queryMappingConfiguration = queryMappingConfiguration ;
97
96
this .operations = operations ;
98
- this .beanfactory = beanfactory ;
99
97
this .evaluationContextProvider = evaluationContextProvider ;
100
98
}
101
99
@@ -114,9 +112,9 @@ static class CreateQueryLookupStrategy extends JdbcQueryLookupStrategy {
114
112
CreateQueryLookupStrategy (ApplicationEventPublisher publisher , @ Nullable EntityCallbacks callbacks ,
115
113
RelationalMappingContext context , JdbcConverter converter , Dialect dialect ,
116
114
QueryMappingConfiguration queryMappingConfiguration , NamedParameterJdbcOperations operations ,
117
- @ Nullable BeanFactory beanfactory , QueryMethodEvaluationContextProvider evaluationContextProvider ) {
115
+ QueryMethodEvaluationContextProvider evaluationContextProvider ) {
118
116
119
- super (publisher , callbacks , context , converter , dialect , queryMappingConfiguration , operations , beanfactory ,
117
+ super (publisher , callbacks , context , converter , dialect , queryMappingConfiguration , operations ,
120
118
evaluationContextProvider );
121
119
}
122
120
@@ -140,12 +138,16 @@ public RepositoryQuery resolveQuery(Method method, RepositoryMetadata repository
140
138
*/
141
139
static class DeclaredQueryLookupStrategy extends JdbcQueryLookupStrategy {
142
140
141
+ private final AbstractJdbcQuery .RowMapperFactory rowMapperFactory ;
142
+
143
143
DeclaredQueryLookupStrategy (ApplicationEventPublisher publisher , @ Nullable EntityCallbacks callbacks ,
144
144
RelationalMappingContext context , JdbcConverter converter , Dialect dialect ,
145
145
QueryMappingConfiguration queryMappingConfiguration , NamedParameterJdbcOperations operations ,
146
146
@ Nullable BeanFactory beanfactory , QueryMethodEvaluationContextProvider evaluationContextProvider ) {
147
- super (publisher , callbacks , context , converter , dialect , queryMappingConfiguration , operations , beanfactory ,
147
+ super (publisher , callbacks , context , converter , dialect , queryMappingConfiguration , operations ,
148
148
evaluationContextProvider );
149
+
150
+ this .rowMapperFactory = new BeanFactoryRowMapperFactory (beanfactory );
149
151
}
150
152
151
153
@ Override
@@ -163,36 +165,51 @@ public RepositoryQuery resolveQuery(Method method, RepositoryMetadata repository
163
165
164
166
String queryString = evaluateTableExpressions (repositoryMetadata , queryMethod .getRequiredQuery ());
165
167
166
- return new StringBasedJdbcQuery (queryString , queryMethod , getOperations (),
167
- new BeanFactoryRowMapperFactory ( getBeanFactory ()), getConverter (), evaluationContextProvider );
168
+ return new StringBasedJdbcQuery (queryString , queryMethod , getOperations (), rowMapperFactory , getConverter (),
169
+ evaluationContextProvider );
168
170
}
169
171
170
172
throw new IllegalStateException (
171
173
String .format ("Did neither find a NamedQuery nor an annotated query for method %s" , method ));
172
174
}
173
175
176
+ @ SuppressWarnings ("unchecked" )
174
177
private class BeanFactoryRowMapperFactory implements AbstractJdbcQuery .RowMapperFactory {
175
178
176
- private final BeanFactory beanFactory ;
179
+ private final @ Nullable BeanFactory beanFactory ;
177
180
178
- BeanFactoryRowMapperFactory (BeanFactory beanFactory ) {
181
+ BeanFactoryRowMapperFactory (@ Nullable BeanFactory beanFactory ) {
179
182
this .beanFactory = beanFactory ;
180
183
}
184
+
181
185
@ Override
182
186
public RowMapper <Object > create (Class <?> result ) {
183
187
return createMapper (result );
184
188
}
185
189
186
190
@ Override
187
- public RowMapper <Object > rowMapperByReference (String reference ) {
191
+ public RowMapper <Object > getRowMapper (String reference ) {
192
+
193
+ if (beanFactory == null ) {
194
+ throw new IllegalStateException (
195
+ "Cannot resolve RowMapper bean reference '" + reference + "'; BeanFactory is not configured." );
196
+ }
197
+
188
198
return beanFactory .getBean (reference , RowMapper .class );
189
199
}
190
200
191
201
@ Override
192
- public ResultSetExtractor <Object > resultSetExtractorByReference (String reference ) {
202
+ public ResultSetExtractor <Object > getResultSetExtractor (String reference ) {
203
+
204
+ if (beanFactory == null ) {
205
+ throw new IllegalStateException (
206
+ "Cannot resolve ResultSetExtractor bean reference '" + reference + "'; BeanFactory is not configured." );
207
+ }
208
+
193
209
return beanFactory .getBean (reference , ResultSetExtractor .class );
194
210
}
195
211
}
212
+
196
213
}
197
214
198
215
/**
@@ -217,10 +234,10 @@ static class CreateIfNotFoundQueryLookupStrategy extends JdbcQueryLookupStrategy
217
234
CreateIfNotFoundQueryLookupStrategy (ApplicationEventPublisher publisher , @ Nullable EntityCallbacks callbacks ,
218
235
RelationalMappingContext context , JdbcConverter converter , Dialect dialect ,
219
236
QueryMappingConfiguration queryMappingConfiguration , NamedParameterJdbcOperations operations ,
220
- @ Nullable BeanFactory beanfactory , CreateQueryLookupStrategy createStrategy ,
237
+ CreateQueryLookupStrategy createStrategy ,
221
238
DeclaredQueryLookupStrategy lookupStrategy , QueryMethodEvaluationContextProvider evaluationContextProvider ) {
222
239
223
- super (publisher , callbacks , context , converter , dialect , queryMappingConfiguration , operations , beanfactory ,
240
+ super (publisher , callbacks , context , converter , dialect , queryMappingConfiguration , operations ,
224
241
evaluationContextProvider );
225
242
226
243
Assert .notNull (createStrategy , "CreateQueryLookupStrategy must not be null" );
@@ -277,23 +294,23 @@ public static QueryLookupStrategy create(@Nullable Key key, ApplicationEventPubl
277
294
Assert .notNull (operations , "NamedParameterJdbcOperations must not be null" );
278
295
279
296
CreateQueryLookupStrategy createQueryLookupStrategy = new CreateQueryLookupStrategy (publisher , callbacks , context ,
280
- converter , dialect , queryMappingConfiguration , operations , beanFactory , evaluationContextProvider );
297
+ converter , dialect , queryMappingConfiguration , operations , evaluationContextProvider );
281
298
282
299
DeclaredQueryLookupStrategy declaredQueryLookupStrategy = new DeclaredQueryLookupStrategy (publisher , callbacks ,
283
300
context , converter , dialect , queryMappingConfiguration , operations , beanFactory , evaluationContextProvider );
284
301
285
- Key cleanedKey = key != null ? key : Key .CREATE_IF_NOT_FOUND ;
302
+ Key keyToUse = key != null ? key : Key .CREATE_IF_NOT_FOUND ;
286
303
287
- LOG .debug (String .format ("Using the queryLookupStrategy %s" , cleanedKey ));
304
+ LOG .debug (String .format ("Using the queryLookupStrategy %s" , keyToUse ));
288
305
289
- switch (cleanedKey ) {
306
+ switch (keyToUse ) {
290
307
case CREATE :
291
308
return createQueryLookupStrategy ;
292
309
case USE_DECLARED_QUERY :
293
310
return declaredQueryLookupStrategy ;
294
311
case CREATE_IF_NOT_FOUND :
295
312
return new CreateIfNotFoundQueryLookupStrategy (publisher , callbacks , context , converter , dialect ,
296
- queryMappingConfiguration , operations , beanFactory , createQueryLookupStrategy , declaredQueryLookupStrategy ,
313
+ queryMappingConfiguration , operations , createQueryLookupStrategy , declaredQueryLookupStrategy ,
297
314
evaluationContextProvider );
298
315
default :
299
316
throw new IllegalArgumentException (String .format ("Unsupported query lookup strategy %s" , key ));
@@ -308,11 +325,6 @@ NamedParameterJdbcOperations getOperations() {
308
325
return operations ;
309
326
}
310
327
311
- @ Nullable
312
- BeanFactory getBeanFactory () {
313
- return beanfactory ;
314
- }
315
-
316
328
@ SuppressWarnings ("unchecked" )
317
329
RowMapper <Object > createMapper (Class <?> returnedObjectType ) {
318
330
0 commit comments