Skip to content

Commit

Permalink
Polishing.
Browse files Browse the repository at this point in the history
Refactor JdbcLookupStrategy to not generally require BeanFactory. Reintroduce deprecated setBeanFactory(…) method.

See #1872
Original pull request: #1874
  • Loading branch information
mp911de committed Sep 16, 2024
1 parent d2bb64f commit 033ac1f
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

import org.springframework.core.convert.converter.Converter;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.data.jdbc.core.convert.JdbcArrayColumns;
import org.springframework.data.repository.query.RepositoryQuery;
import org.springframework.data.repository.query.ResultProcessor;
import org.springframework.data.repository.query.ReturnedType;
Expand Down Expand Up @@ -171,8 +170,8 @@ public interface RowMapperFactory {
* @param reference must not be {@code null}.
* @since 3.4
*/
default RowMapper<Object> rowMapperByReference(String reference) {
throw new UnsupportedOperationException("rowMapperByReference is not supported");
default RowMapper<Object> getRowMapper(String reference) {
throw new UnsupportedOperationException("getRowMapper is not supported");
}

/**
Expand All @@ -181,8 +180,8 @@ default RowMapper<Object> rowMapperByReference(String reference) {
* @param reference must not be {@code null}.
* @since 3.4
*/
default ResultSetExtractor<Object> resultSetExtractorByReference(String reference) {
throw new UnsupportedOperationException("resultSetExtractorByReference is not supported");
default ResultSetExtractor<Object> getResultSetExtractor(String reference) {
throw new UnsupportedOperationException("getResultSetExtractor is not supported");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,9 @@ private static boolean isUnconfigured(@Nullable Class<?> configuredClass, Class<
return configuredClass == null || configuredClass == defaultClass;
}

@Deprecated(since = "3.4")
public void setBeanFactory(BeanFactory beanFactory) {}

class CachedRowMapperFactory {

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

if (!ObjectUtils.isEmpty(rowMapperRef)) {
return rowMapperFactory.rowMapperByReference(rowMapperRef);
return rowMapperFactory.getRowMapper(rowMapperRef);
}

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

if (!ObjectUtils.isEmpty(resultSetExtractorRef)) {
return rowMapperFactory.resultSetExtractorByReference(resultSetExtractorRef);
return rowMapperFactory.getResultSetExtractor(resultSetExtractorRef);
}

if (isUnconfigured(resultSetExtractorClass, ResultSetExtractor.class)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,29 +73,27 @@ abstract class JdbcQueryLookupStrategy extends RelationalQueryLookupStrategy {
private final JdbcConverter converter;
private final QueryMappingConfiguration queryMappingConfiguration;
private final NamedParameterJdbcOperations operations;
@Nullable private final BeanFactory beanfactory;
protected final QueryMethodEvaluationContextProvider evaluationContextProvider;

JdbcQueryLookupStrategy(ApplicationEventPublisher publisher, @Nullable EntityCallbacks callbacks,
RelationalMappingContext context, JdbcConverter converter, Dialect dialect,
QueryMappingConfiguration queryMappingConfiguration, NamedParameterJdbcOperations operations,
@Nullable BeanFactory beanfactory, QueryMethodEvaluationContextProvider evaluationContextProvider) {
QueryMethodEvaluationContextProvider evaluationContextProvider) {

super(context, dialect);

Assert.notNull(publisher, "ApplicationEventPublisher must not be null");
Assert.notNull(converter, "JdbcConverter must not be null");
Assert.notNull(queryMappingConfiguration, "QueryMappingConfiguration must not be null");
Assert.notNull(operations, "NamedParameterJdbcOperations must not be null");
Assert.notNull(evaluationContextProvider, "QueryMethodEvaluationContextProvier must not be null");
Assert.notNull(evaluationContextProvider, "QueryMethodEvaluationContextProvider must not be null");

this.context = context;
this.publisher = publisher;
this.callbacks = callbacks;
this.converter = converter;
this.queryMappingConfiguration = queryMappingConfiguration;
this.operations = operations;
this.beanfactory = beanfactory;
this.evaluationContextProvider = evaluationContextProvider;
}

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

super(publisher, callbacks, context, converter, dialect, queryMappingConfiguration, operations, beanfactory,
super(publisher, callbacks, context, converter, dialect, queryMappingConfiguration, operations,
evaluationContextProvider);
}

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

private final AbstractJdbcQuery.RowMapperFactory rowMapperFactory;

DeclaredQueryLookupStrategy(ApplicationEventPublisher publisher, @Nullable EntityCallbacks callbacks,
RelationalMappingContext context, JdbcConverter converter, Dialect dialect,
QueryMappingConfiguration queryMappingConfiguration, NamedParameterJdbcOperations operations,
@Nullable BeanFactory beanfactory, QueryMethodEvaluationContextProvider evaluationContextProvider) {
super(publisher, callbacks, context, converter, dialect, queryMappingConfiguration, operations, beanfactory,
super(publisher, callbacks, context, converter, dialect, queryMappingConfiguration, operations,
evaluationContextProvider);

this.rowMapperFactory = new BeanFactoryRowMapperFactory(beanfactory);
}

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

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

return new StringBasedJdbcQuery(queryString, queryMethod, getOperations(),
new BeanFactoryRowMapperFactory(getBeanFactory()), getConverter(), evaluationContextProvider);
return new StringBasedJdbcQuery(queryString, queryMethod, getOperations(), rowMapperFactory, getConverter(),
evaluationContextProvider);
}

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

@SuppressWarnings("unchecked")
private class BeanFactoryRowMapperFactory implements AbstractJdbcQuery.RowMapperFactory {

private final BeanFactory beanFactory;
private final @Nullable BeanFactory beanFactory;

BeanFactoryRowMapperFactory(BeanFactory beanFactory) {
BeanFactoryRowMapperFactory(@Nullable BeanFactory beanFactory) {
this.beanFactory = beanFactory;
}

@Override
public RowMapper<Object> create(Class<?> result) {
return createMapper(result);
}

@Override
public RowMapper<Object> rowMapperByReference(String reference) {
public RowMapper<Object> getRowMapper(String reference) {

if (beanFactory == null) {
throw new IllegalStateException(
"Cannot resolve RowMapper bean reference '" + reference + "'; BeanFactory is not configured.");
}

return beanFactory.getBean(reference, RowMapper.class);
}

@Override
public ResultSetExtractor<Object> resultSetExtractorByReference(String reference) {
public ResultSetExtractor<Object> getResultSetExtractor(String reference) {

if (beanFactory == null) {
throw new IllegalStateException(
"Cannot resolve ResultSetExtractor bean reference '" + reference + "'; BeanFactory is not configured.");
}

return beanFactory.getBean(reference, ResultSetExtractor.class);
}
}

}

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

super(publisher, callbacks, context, converter, dialect, queryMappingConfiguration, operations, beanfactory,
super(publisher, callbacks, context, converter, dialect, queryMappingConfiguration, operations,
evaluationContextProvider);

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

CreateQueryLookupStrategy createQueryLookupStrategy = new CreateQueryLookupStrategy(publisher, callbacks, context,
converter, dialect, queryMappingConfiguration, operations, beanFactory, evaluationContextProvider);
converter, dialect, queryMappingConfiguration, operations, evaluationContextProvider);

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

Key cleanedKey = key != null ? key : Key.CREATE_IF_NOT_FOUND;
Key keyToUse = key != null ? key : Key.CREATE_IF_NOT_FOUND;

LOG.debug(String.format("Using the queryLookupStrategy %s", cleanedKey));
LOG.debug(String.format("Using the queryLookupStrategy %s", keyToUse));

switch (cleanedKey) {
switch (keyToUse) {
case CREATE:
return createQueryLookupStrategy;
case USE_DECLARED_QUERY:
return declaredQueryLookupStrategy;
case CREATE_IF_NOT_FOUND:
return new CreateIfNotFoundQueryLookupStrategy(publisher, callbacks, context, converter, dialect,
queryMappingConfiguration, operations, beanFactory, createQueryLookupStrategy, declaredQueryLookupStrategy,
queryMappingConfiguration, operations, createQueryLookupStrategy, declaredQueryLookupStrategy,
evaluationContextProvider);
default:
throw new IllegalArgumentException(String.format("Unsupported query lookup strategy %s", key));
Expand All @@ -308,11 +325,6 @@ NamedParameterJdbcOperations getOperations() {
return operations;
}

@Nullable
BeanFactory getBeanFactory() {
return beanfactory;
}

@SuppressWarnings("unchecked")
RowMapper<Object> createMapper(Class<?> returnedObjectType) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -645,21 +645,21 @@ public RowMapper<Object> create(Class<?> result) {
}

@Override
public RowMapper<Object> rowMapperByReference(String reference) {
public RowMapper<Object> getRowMapper(String reference) {

if (preparedReference.equals(reference)) {
return (RowMapper<Object>) value;
}
return AbstractJdbcQuery.RowMapperFactory.super.rowMapperByReference(reference);
return AbstractJdbcQuery.RowMapperFactory.super.getRowMapper(reference);
}

@Override
public ResultSetExtractor<Object> resultSetExtractorByReference(String reference) {
public ResultSetExtractor<Object> getResultSetExtractor(String reference) {

if (preparedReference.equals(reference)) {
return (ResultSetExtractor<Object>) value;
}
return AbstractJdbcQuery.RowMapperFactory.super.resultSetExtractorByReference(reference);
return AbstractJdbcQuery.RowMapperFactory.super.getResultSetExtractor(reference);
}
}
}

0 comments on commit 033ac1f

Please sign in to comment.