Skip to content

Commit 033ac1f

Browse files
committed
Polishing.
Refactor JdbcLookupStrategy to not generally require BeanFactory. Reintroduce deprecated setBeanFactory(…) method. See #1872 Original pull request: #1874
1 parent d2bb64f commit 033ac1f

File tree

4 files changed

+50
-36
lines changed

4 files changed

+50
-36
lines changed

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/query/AbstractJdbcQuery.java

+4-5
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323

2424
import org.springframework.core.convert.converter.Converter;
2525
import org.springframework.dao.EmptyResultDataAccessException;
26-
import org.springframework.data.jdbc.core.convert.JdbcArrayColumns;
2726
import org.springframework.data.repository.query.RepositoryQuery;
2827
import org.springframework.data.repository.query.ResultProcessor;
2928
import org.springframework.data.repository.query.ReturnedType;
@@ -171,8 +170,8 @@ public interface RowMapperFactory {
171170
* @param reference must not be {@code null}.
172171
* @since 3.4
173172
*/
174-
default RowMapper<Object> rowMapperByReference(String reference) {
175-
throw new UnsupportedOperationException("rowMapperByReference is not supported");
173+
default RowMapper<Object> getRowMapper(String reference) {
174+
throw new UnsupportedOperationException("getRowMapper is not supported");
176175
}
177176

178177
/**
@@ -181,8 +180,8 @@ default RowMapper<Object> rowMapperByReference(String reference) {
181180
* @param reference must not be {@code null}.
182181
* @since 3.4
183182
*/
184-
default ResultSetExtractor<Object> resultSetExtractorByReference(String reference) {
185-
throw new UnsupportedOperationException("resultSetExtractorByReference is not supported");
183+
default ResultSetExtractor<Object> getResultSetExtractor(String reference) {
184+
throw new UnsupportedOperationException("getResultSetExtractor is not supported");
186185
}
187186
}
188187

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/query/StringBasedJdbcQuery.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,9 @@ private static boolean isUnconfigured(@Nullable Class<?> configuredClass, Class<
352352
return configuredClass == null || configuredClass == defaultClass;
353353
}
354354

355+
@Deprecated(since = "3.4")
356+
public void setBeanFactory(BeanFactory beanFactory) {}
357+
355358
class CachedRowMapperFactory {
356359

357360
private final Lazy<RowMapper<Object>> cachedRowMapper;
@@ -375,7 +378,7 @@ public CachedRowMapperFactory(Supplier<RowMapper<Object>> defaultMapper) {
375378
this.cachedRowMapper = Lazy.of(() -> {
376379

377380
if (!ObjectUtils.isEmpty(rowMapperRef)) {
378-
return rowMapperFactory.rowMapperByReference(rowMapperRef);
381+
return rowMapperFactory.getRowMapper(rowMapperRef);
379382
}
380383

381384
if (isUnconfigured(rowMapperClass, RowMapper.class)) {
@@ -426,7 +429,7 @@ public CachedResultSetExtractorFactory(Supplier<RowMapper<?>> resultSetExtractor
426429
this.resultSetExtractorFactory = rowMapper -> {
427430

428431
if (!ObjectUtils.isEmpty(resultSetExtractorRef)) {
429-
return rowMapperFactory.resultSetExtractorByReference(resultSetExtractorRef);
432+
return rowMapperFactory.getResultSetExtractor(resultSetExtractorRef);
430433
}
431434

432435
if (isUnconfigured(resultSetExtractorClass, ResultSetExtractor.class)) {

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/support/JdbcQueryLookupStrategy.java

+37-25
Original file line numberDiff line numberDiff line change
@@ -73,29 +73,27 @@ abstract class JdbcQueryLookupStrategy extends RelationalQueryLookupStrategy {
7373
private final JdbcConverter converter;
7474
private final QueryMappingConfiguration queryMappingConfiguration;
7575
private final NamedParameterJdbcOperations operations;
76-
@Nullable private final BeanFactory beanfactory;
7776
protected final QueryMethodEvaluationContextProvider evaluationContextProvider;
7877

7978
JdbcQueryLookupStrategy(ApplicationEventPublisher publisher, @Nullable EntityCallbacks callbacks,
8079
RelationalMappingContext context, JdbcConverter converter, Dialect dialect,
8180
QueryMappingConfiguration queryMappingConfiguration, NamedParameterJdbcOperations operations,
82-
@Nullable BeanFactory beanfactory, QueryMethodEvaluationContextProvider evaluationContextProvider) {
81+
QueryMethodEvaluationContextProvider evaluationContextProvider) {
8382

8483
super(context, dialect);
8584

8685
Assert.notNull(publisher, "ApplicationEventPublisher must not be null");
8786
Assert.notNull(converter, "JdbcConverter must not be null");
8887
Assert.notNull(queryMappingConfiguration, "QueryMappingConfiguration must not be null");
8988
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");
9190

9291
this.context = context;
9392
this.publisher = publisher;
9493
this.callbacks = callbacks;
9594
this.converter = converter;
9695
this.queryMappingConfiguration = queryMappingConfiguration;
9796
this.operations = operations;
98-
this.beanfactory = beanfactory;
9997
this.evaluationContextProvider = evaluationContextProvider;
10098
}
10199

@@ -114,9 +112,9 @@ static class CreateQueryLookupStrategy extends JdbcQueryLookupStrategy {
114112
CreateQueryLookupStrategy(ApplicationEventPublisher publisher, @Nullable EntityCallbacks callbacks,
115113
RelationalMappingContext context, JdbcConverter converter, Dialect dialect,
116114
QueryMappingConfiguration queryMappingConfiguration, NamedParameterJdbcOperations operations,
117-
@Nullable BeanFactory beanfactory, QueryMethodEvaluationContextProvider evaluationContextProvider) {
115+
QueryMethodEvaluationContextProvider evaluationContextProvider) {
118116

119-
super(publisher, callbacks, context, converter, dialect, queryMappingConfiguration, operations, beanfactory,
117+
super(publisher, callbacks, context, converter, dialect, queryMappingConfiguration, operations,
120118
evaluationContextProvider);
121119
}
122120

@@ -140,12 +138,16 @@ public RepositoryQuery resolveQuery(Method method, RepositoryMetadata repository
140138
*/
141139
static class DeclaredQueryLookupStrategy extends JdbcQueryLookupStrategy {
142140

141+
private final AbstractJdbcQuery.RowMapperFactory rowMapperFactory;
142+
143143
DeclaredQueryLookupStrategy(ApplicationEventPublisher publisher, @Nullable EntityCallbacks callbacks,
144144
RelationalMappingContext context, JdbcConverter converter, Dialect dialect,
145145
QueryMappingConfiguration queryMappingConfiguration, NamedParameterJdbcOperations operations,
146146
@Nullable BeanFactory beanfactory, QueryMethodEvaluationContextProvider evaluationContextProvider) {
147-
super(publisher, callbacks, context, converter, dialect, queryMappingConfiguration, operations, beanfactory,
147+
super(publisher, callbacks, context, converter, dialect, queryMappingConfiguration, operations,
148148
evaluationContextProvider);
149+
150+
this.rowMapperFactory = new BeanFactoryRowMapperFactory(beanfactory);
149151
}
150152

151153
@Override
@@ -163,36 +165,51 @@ public RepositoryQuery resolveQuery(Method method, RepositoryMetadata repository
163165

164166
String queryString = evaluateTableExpressions(repositoryMetadata, queryMethod.getRequiredQuery());
165167

166-
return new StringBasedJdbcQuery(queryString, queryMethod, getOperations(),
167-
new BeanFactoryRowMapperFactory(getBeanFactory()), getConverter(), evaluationContextProvider);
168+
return new StringBasedJdbcQuery(queryString, queryMethod, getOperations(), rowMapperFactory, getConverter(),
169+
evaluationContextProvider);
168170
}
169171

170172
throw new IllegalStateException(
171173
String.format("Did neither find a NamedQuery nor an annotated query for method %s", method));
172174
}
173175

176+
@SuppressWarnings("unchecked")
174177
private class BeanFactoryRowMapperFactory implements AbstractJdbcQuery.RowMapperFactory {
175178

176-
private final BeanFactory beanFactory;
179+
private final @Nullable BeanFactory beanFactory;
177180

178-
BeanFactoryRowMapperFactory(BeanFactory beanFactory) {
181+
BeanFactoryRowMapperFactory(@Nullable BeanFactory beanFactory) {
179182
this.beanFactory = beanFactory;
180183
}
184+
181185
@Override
182186
public RowMapper<Object> create(Class<?> result) {
183187
return createMapper(result);
184188
}
185189

186190
@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+
188198
return beanFactory.getBean(reference, RowMapper.class);
189199
}
190200

191201
@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+
193209
return beanFactory.getBean(reference, ResultSetExtractor.class);
194210
}
195211
}
212+
196213
}
197214

198215
/**
@@ -217,10 +234,10 @@ static class CreateIfNotFoundQueryLookupStrategy extends JdbcQueryLookupStrategy
217234
CreateIfNotFoundQueryLookupStrategy(ApplicationEventPublisher publisher, @Nullable EntityCallbacks callbacks,
218235
RelationalMappingContext context, JdbcConverter converter, Dialect dialect,
219236
QueryMappingConfiguration queryMappingConfiguration, NamedParameterJdbcOperations operations,
220-
@Nullable BeanFactory beanfactory, CreateQueryLookupStrategy createStrategy,
237+
CreateQueryLookupStrategy createStrategy,
221238
DeclaredQueryLookupStrategy lookupStrategy, QueryMethodEvaluationContextProvider evaluationContextProvider) {
222239

223-
super(publisher, callbacks, context, converter, dialect, queryMappingConfiguration, operations, beanfactory,
240+
super(publisher, callbacks, context, converter, dialect, queryMappingConfiguration, operations,
224241
evaluationContextProvider);
225242

226243
Assert.notNull(createStrategy, "CreateQueryLookupStrategy must not be null");
@@ -277,23 +294,23 @@ public static QueryLookupStrategy create(@Nullable Key key, ApplicationEventPubl
277294
Assert.notNull(operations, "NamedParameterJdbcOperations must not be null");
278295

279296
CreateQueryLookupStrategy createQueryLookupStrategy = new CreateQueryLookupStrategy(publisher, callbacks, context,
280-
converter, dialect, queryMappingConfiguration, operations, beanFactory, evaluationContextProvider);
297+
converter, dialect, queryMappingConfiguration, operations, evaluationContextProvider);
281298

282299
DeclaredQueryLookupStrategy declaredQueryLookupStrategy = new DeclaredQueryLookupStrategy(publisher, callbacks,
283300
context, converter, dialect, queryMappingConfiguration, operations, beanFactory, evaluationContextProvider);
284301

285-
Key cleanedKey = key != null ? key : Key.CREATE_IF_NOT_FOUND;
302+
Key keyToUse = key != null ? key : Key.CREATE_IF_NOT_FOUND;
286303

287-
LOG.debug(String.format("Using the queryLookupStrategy %s", cleanedKey));
304+
LOG.debug(String.format("Using the queryLookupStrategy %s", keyToUse));
288305

289-
switch (cleanedKey) {
306+
switch (keyToUse) {
290307
case CREATE:
291308
return createQueryLookupStrategy;
292309
case USE_DECLARED_QUERY:
293310
return declaredQueryLookupStrategy;
294311
case CREATE_IF_NOT_FOUND:
295312
return new CreateIfNotFoundQueryLookupStrategy(publisher, callbacks, context, converter, dialect,
296-
queryMappingConfiguration, operations, beanFactory, createQueryLookupStrategy, declaredQueryLookupStrategy,
313+
queryMappingConfiguration, operations, createQueryLookupStrategy, declaredQueryLookupStrategy,
297314
evaluationContextProvider);
298315
default:
299316
throw new IllegalArgumentException(String.format("Unsupported query lookup strategy %s", key));
@@ -308,11 +325,6 @@ NamedParameterJdbcOperations getOperations() {
308325
return operations;
309326
}
310327

311-
@Nullable
312-
BeanFactory getBeanFactory() {
313-
return beanfactory;
314-
}
315-
316328
@SuppressWarnings("unchecked")
317329
RowMapper<Object> createMapper(Class<?> returnedObjectType) {
318330

spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/query/StringBasedJdbcQueryUnitTests.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -645,21 +645,21 @@ public RowMapper<Object> create(Class<?> result) {
645645
}
646646

647647
@Override
648-
public RowMapper<Object> rowMapperByReference(String reference) {
648+
public RowMapper<Object> getRowMapper(String reference) {
649649

650650
if (preparedReference.equals(reference)) {
651651
return (RowMapper<Object>) value;
652652
}
653-
return AbstractJdbcQuery.RowMapperFactory.super.rowMapperByReference(reference);
653+
return AbstractJdbcQuery.RowMapperFactory.super.getRowMapper(reference);
654654
}
655655

656656
@Override
657-
public ResultSetExtractor<Object> resultSetExtractorByReference(String reference) {
657+
public ResultSetExtractor<Object> getResultSetExtractor(String reference) {
658658

659659
if (preparedReference.equals(reference)) {
660660
return (ResultSetExtractor<Object>) value;
661661
}
662-
return AbstractJdbcQuery.RowMapperFactory.super.resultSetExtractorByReference(reference);
662+
return AbstractJdbcQuery.RowMapperFactory.super.getResultSetExtractor(reference);
663663
}
664664
}
665665
}

0 commit comments

Comments
 (0)