Skip to content

Commit 1b1395a

Browse files
committed
DATAJDBC-430 - Polishing.
Renames *Bean annotation attributes to *Ref to match other similar attributes. Removes additional arguments from constructors of public classes in order to avoid breaking the API. Gathers application context configuration for StringBasedJdbcQueryMappingConfigurationIntegrationTests in a single java file. Formatting. Adds the author tag. Changes default value of references to the empty String. Original pull request: #249.
1 parent c356a8c commit 1b1395a

12 files changed

+178
-161
lines changed

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
* @author Jens Schauder
4747
* @author Kazuki Shimizu
4848
* @author Moises Cisneros
49+
* @author Hebert Coelho
4950
*/
5051
public class JdbcQueryMethod extends QueryMethod {
5152

@@ -175,13 +176,13 @@ Class<? extends RowMapper> getRowMapperClass() {
175176

176177

177178
/**
178-
* Returns the bean to be used as {@link org.springframework.jdbc.core.RowMapper}
179+
* Returns the name of the bean to be used as {@link org.springframework.jdbc.core.RowMapper}
179180
*
180181
* @return May be {@code null}.
181182
*/
182183
@Nullable
183-
String getRowMapperBean() {
184-
return getMergedAnnotationAttribute("rowMapperBean");
184+
String getRowMapperRef() {
185+
return getMergedAnnotationAttribute("rowMapperRef");
185186
}
186187

187188
/**
@@ -195,13 +196,13 @@ Class<? extends ResultSetExtractor> getResultSetExtractorClass() {
195196
}
196197

197198
/**
198-
* Returns the bean to be used as {@link org.springframework.jdbc.core.ResultSetExtractor}
199+
* Returns the bean name to be used as {@link org.springframework.jdbc.core.ResultSetExtractor}
199200
*
200201
* @return May be {@code null}.
201202
*/
202203
@Nullable
203-
String getResultSetExtractorBean() {
204-
return getMergedAnnotationAttribute("resultSetExtractorBean");
204+
String getResultSetExtractorRef() {
205+
return getMergedAnnotationAttribute("resultSetExtractorRef");
205206
}
206207

207208
/**

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
*
3333
* @author Jens Schauder
3434
* @author Moises Cisneros
35+
* @author Hebert Coelho
3536
*/
3637
@Retention(RetentionPolicy.RUNTIME)
3738
@Target(ElementType.METHOD)
@@ -57,10 +58,12 @@
5758
Class<? extends RowMapper> rowMapperClass() default RowMapper.class;
5859

5960
/**
60-
* Optional bean of type {@link RowMapper} to use to convert the result of the query to domain class instances. Cannot be used
61+
* Optional name of a bean of type {@link RowMapper} to use to convert the result of the query to domain class instances. Cannot be used
6162
* along with {@link #resultSetExtractorClass()} only one of the two can be set.
63+
*
64+
* @since 2.1
6265
*/
63-
String rowMapperBean() default "RowMapper";
66+
String rowMapperRef() default "";
6467

6568
/**
6669
* Optional {@link ResultSetExtractor} to use to convert the result of the query to domain class instances. Cannot be
@@ -69,8 +72,10 @@
6972
Class<? extends ResultSetExtractor> resultSetExtractorClass() default ResultSetExtractor.class;
7073

7174
/**
72-
* Optional bean of type {@link ResultSetExtractor} to use to convert the result of the query to domain class instances. Cannot be
75+
* Optional name of a bean of type {@link ResultSetExtractor} to use to convert the result of the query to domain class instances. Cannot be
7376
* used along with {@link #rowMapperClass()} only one of the two can be set.
77+
*
78+
* @since 2.1
7479
*/
75-
String resultSetExtractorBean() default "ResultSetExtractor";
80+
String resultSetExtractorRef() default "";
7681
}

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

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@
2626
import org.springframework.data.jdbc.support.JdbcUtil;
2727
import org.springframework.data.relational.core.mapping.RelationalMappingContext;
2828
import org.springframework.data.repository.query.Parameter;
29+
import org.springframework.data.util.Lazy;
2930
import org.springframework.jdbc.core.ResultSetExtractor;
3031
import org.springframework.jdbc.core.RowMapper;
3132
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
3233
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
3334
import org.springframework.lang.Nullable;
35+
import org.springframework.util.Assert;
3436
import org.springframework.util.ClassUtils;
3537
import org.springframework.util.StringUtils;
3638

@@ -43,16 +45,17 @@
4345
* @author Oliver Gierke
4446
* @author Maciej Walkowiak
4547
* @author Mark Paluch
48+
* @author Hebert Coelho
4649
* @since 2.0
4750
*/
4851
public class StringBasedJdbcQuery extends AbstractJdbcQuery {
4952

5053
private static final String PARAMETER_NEEDS_TO_BE_NAMED = "For queries with named parameters you need to provide names for method parameters. Use @Param for query method parameters, or when on Java 8+ use the javac flag -parameters.";
5154

5255
private final JdbcQueryMethod queryMethod;
53-
private final JdbcQueryExecution<?> executor;
56+
private final Lazy<JdbcQueryExecution<?>> executor;
5457
private final JdbcConverter converter;
55-
private BeanFactory beanfactory;
58+
private BeanFactory beanFactory;
5659

5760
/**
5861
* Creates a new {@link StringBasedJdbcQuery} for the given {@link JdbcQueryMethod}, {@link RelationalMappingContext}
@@ -63,20 +66,20 @@ public class StringBasedJdbcQuery extends AbstractJdbcQuery {
6366
* @param defaultRowMapper can be {@literal null} (only in case of a modifying query).
6467
*/
6568
public StringBasedJdbcQuery(JdbcQueryMethod queryMethod, NamedParameterJdbcOperations operations,
66-
@Nullable RowMapper<?> defaultRowMapper, JdbcConverter converter, BeanFactory beanfactory) {
69+
@Nullable RowMapper<?> defaultRowMapper, JdbcConverter converter) {
6770

6871
super(queryMethod, operations, defaultRowMapper);
6972

7073
this.queryMethod = queryMethod;
7174
this.converter = converter;
72-
this.beanfactory = beanfactory;
7375

74-
RowMapper<Object> rowMapper = determineRowMapper(defaultRowMapper);
75-
executor = getQueryExecution( //
76+
executor = Lazy.of(() -> {
77+
RowMapper<Object> rowMapper = determineRowMapper(defaultRowMapper);
78+
return getQueryExecution( //
7679
queryMethod, //
7780
determineResultSetExtractor(rowMapper != defaultRowMapper ? rowMapper : null), //
7881
rowMapper //
79-
);
82+
);});
8083
}
8184

8285
/*
@@ -85,7 +88,7 @@ public StringBasedJdbcQuery(JdbcQueryMethod queryMethod, NamedParameterJdbcOpera
8588
*/
8689
@Override
8790
public Object execute(Object[] objects) {
88-
return executor.execute(determineQuery(), this.bindParameters(objects));
91+
return executor.get().execute(determineQuery(), this.bindParameters(objects));
8992
}
9093

9194
/*
@@ -97,7 +100,7 @@ public JdbcQueryMethod getQueryMethod() {
97100
return queryMethod;
98101
}
99102

100-
MapSqlParameterSource bindParameters(Object[] objects) {
103+
private MapSqlParameterSource bindParameters(Object[] objects) {
101104

102105
MapSqlParameterSource parameters = new MapSqlParameterSource();
103106

@@ -140,10 +143,14 @@ private String determineQuery() {
140143
@Nullable
141144
@SuppressWarnings({ "rawtypes", "unchecked" })
142145
ResultSetExtractor<Object> determineResultSetExtractor(@Nullable RowMapper<Object> rowMapper) {
143-
String resultSetExtractorBean = queryMethod.getResultSetExtractorBean();
144146

145-
if (resultSetExtractorBean != null && !"ResultSetExtractor".equals(resultSetExtractorBean)) {
146-
return (ResultSetExtractor<Object>) beanfactory.getBean(resultSetExtractorBean);
147+
String resultSetExtractorRef = queryMethod.getResultSetExtractorRef();
148+
149+
if (!StringUtils.isEmpty(resultSetExtractorRef)) {
150+
151+
Assert.notNull(beanFactory, "When a ResultSetExtractorRef is specified the BeanFactory must not be null");
152+
153+
return (ResultSetExtractor<Object>) beanFactory.getBean(resultSetExtractorRef);
147154
}
148155

149156
Class<? extends ResultSetExtractor> resultSetExtractorClass = queryMethod.getResultSetExtractorClass();
@@ -163,12 +170,16 @@ ResultSetExtractor<Object> determineResultSetExtractor(@Nullable RowMapper<Objec
163170
}
164171

165172
@SuppressWarnings("unchecked")
173+
@Nullable
166174
RowMapper<Object> determineRowMapper(@Nullable RowMapper<?> defaultMapper) {
167175

168-
String rowMapperBean = queryMethod.getRowMapperBean();
176+
String rowMapperRef = queryMethod.getRowMapperRef();
169177

170-
if (rowMapperBean != null && !"RowMapper".equals(rowMapperBean)) {
171-
return (RowMapper<Object>) beanfactory.getBean(rowMapperBean);
178+
if (!StringUtils.isEmpty(rowMapperRef)) {
179+
180+
Assert.notNull(beanFactory, "When a RowMapperRef is specified the BeanFactory must not be null");
181+
182+
return (RowMapper<Object>) beanFactory.getBean(rowMapperRef);
172183
}
173184

174185
Class<?> rowMapperClass = queryMethod.getRowMapperClass();
@@ -183,4 +194,8 @@ RowMapper<Object> determineRowMapper(@Nullable RowMapper<?> defaultMapper) {
183194
private static boolean isUnconfigured(@Nullable Class<?> configuredClass, Class<?> defaultClass) {
184195
return configuredClass == null || configuredClass == defaultClass;
185196
}
197+
198+
public void setBeanFactory(BeanFactory beanFactory) {
199+
this.beanFactory = beanFactory;
200+
}
186201
}

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
* @author Mark Paluch
5555
* @author Maciej Walkowiak
5656
* @author Moises Cisneros
57+
* @author Hebert Coelho
5758
*/
5859
class JdbcQueryLookupStrategy implements QueryLookupStrategy {
5960

@@ -64,9 +65,9 @@ class JdbcQueryLookupStrategy implements QueryLookupStrategy {
6465
private final Dialect dialect;
6566
private final QueryMappingConfiguration queryMappingConfiguration;
6667
private final NamedParameterJdbcOperations operations;
67-
private BeanFactory beanfactory;
68+
private final BeanFactory beanfactory;
6869

69-
public JdbcQueryLookupStrategy(ApplicationEventPublisher publisher, @Nullable EntityCallbacks callbacks,
70+
JdbcQueryLookupStrategy(ApplicationEventPublisher publisher, @Nullable EntityCallbacks callbacks,
7071
RelationalMappingContext context, JdbcConverter converter, Dialect dialect,
7172
QueryMappingConfiguration queryMappingConfiguration, NamedParameterJdbcOperations operations,
7273
BeanFactory beanfactory) {
@@ -103,12 +104,14 @@ public RepositoryQuery resolveQuery(Method method, RepositoryMetadata repository
103104
if (namedQueries.hasQuery(queryMethod.getNamedQueryName()) || queryMethod.hasAnnotatedQuery()) {
104105

105106
RowMapper<?> mapper = queryMethod.isModifyingQuery() ? null : createMapper(queryMethod);
106-
return new StringBasedJdbcQuery(queryMethod, operations, mapper, converter, beanfactory);
107+
StringBasedJdbcQuery query = new StringBasedJdbcQuery(queryMethod, operations, mapper, converter);
108+
query.setBeanFactory(beanfactory);
109+
return query;
107110
} else {
108111
return new PartTreeJdbcQuery(context, queryMethod, dialect, converter, operations, createMapper(queryMethod));
109112
}
110113
} catch (Exception e) {
111-
throw QueryCreationException.create(queryMethod, e.getMessage());
114+
throw QueryCreationException.create(queryMethod, e);
112115
}
113116
}
114117

@@ -120,10 +123,10 @@ private RowMapper<Object> createMapper(JdbcQueryMethod queryMethod) {
120123
RelationalPersistentEntity<?> persistentEntity = context.getPersistentEntity(returnedObjectType);
121124

122125
if (persistentEntity == null) {
123-
return (RowMapper) SingleColumnRowMapper.newInstance(returnedObjectType, converter.getConversionService());
126+
return (RowMapper<Object>) SingleColumnRowMapper.newInstance(returnedObjectType, converter.getConversionService());
124127
}
125128

126-
return (RowMapper) determineDefaultMapper(queryMethod);
129+
return (RowMapper<Object>) determineDefaultMapper(queryMethod);
127130
}
128131

129132
private RowMapper<?> determineDefaultMapper(JdbcQueryMethod queryMethod) {

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
* @author Greg Turnquist
4646
* @author Christoph Strobl
4747
* @author Mark Paluch
48+
* @author Hebert Coelho
4849
*/
4950
public class JdbcRepositoryFactory extends RepositoryFactorySupport {
5051

@@ -54,7 +55,7 @@ public class JdbcRepositoryFactory extends RepositoryFactorySupport {
5455
private final DataAccessStrategy accessStrategy;
5556
private final NamedParameterJdbcOperations operations;
5657
private final Dialect dialect;
57-
private BeanFactory beanfactory;
58+
@Nullable private BeanFactory beanFactory;
5859

5960
private QueryMappingConfiguration queryMappingConfiguration = QueryMappingConfiguration.EMPTY;
6061
private EntityCallbacks entityCallbacks;
@@ -72,7 +73,7 @@ public class JdbcRepositoryFactory extends RepositoryFactorySupport {
7273
*/
7374
public JdbcRepositoryFactory(DataAccessStrategy dataAccessStrategy, RelationalMappingContext context,
7475
JdbcConverter converter, Dialect dialect, ApplicationEventPublisher publisher,
75-
NamedParameterJdbcOperations operations, BeanFactory beanfactory) {
76+
NamedParameterJdbcOperations operations) {
7677

7778
Assert.notNull(dataAccessStrategy, "DataAccessStrategy must not be null!");
7879
Assert.notNull(context, "RelationalMappingContext must not be null!");
@@ -86,7 +87,6 @@ public JdbcRepositoryFactory(DataAccessStrategy dataAccessStrategy, RelationalMa
8687
this.dialect = dialect;
8788
this.accessStrategy = dataAccessStrategy;
8889
this.operations = operations;
89-
this.beanfactory = beanfactory;
9090
}
9191

9292
/**
@@ -122,7 +122,8 @@ protected Object getTargetRepository(RepositoryInformation repositoryInformation
122122
template.setEntityCallbacks(entityCallbacks);
123123
}
124124

125-
RelationalPersistentEntity<?> persistentEntity = context.getRequiredPersistentEntity(repositoryInformation.getDomainType());
125+
RelationalPersistentEntity<?> persistentEntity = context
126+
.getRequiredPersistentEntity(repositoryInformation.getDomainType());
126127

127128
return getTargetRepositoryViaReflection(repositoryInformation.getRepositoryBaseClass(), template, persistentEntity);
128129
}
@@ -145,7 +146,7 @@ protected Optional<QueryLookupStrategy> getQueryLookupStrategy(@Nullable QueryLo
145146
QueryMethodEvaluationContextProvider evaluationContextProvider) {
146147

147148
return Optional.of(new JdbcQueryLookupStrategy(publisher, entityCallbacks, context, converter, dialect,
148-
queryMappingConfiguration, operations, beanfactory));
149+
queryMappingConfiguration, operations, beanFactory));
149150
}
150151

151152
/**
@@ -155,4 +156,12 @@ protected Optional<QueryLookupStrategy> getQueryLookupStrategy(@Nullable QueryLo
155156
public void setEntityCallbacks(EntityCallbacks entityCallbacks) {
156157
this.entityCallbacks = entityCallbacks;
157158
}
159+
160+
/**
161+
* @param beanFactory the {@link BeanFactory} used for looking up {@link org.springframework.jdbc.core.RowMapper} and
162+
* {@link org.springframework.jdbc.core.ResultSetExtractor} beans.
163+
*/
164+
public void setBeanFactory(@Nullable BeanFactory beanFactory) {
165+
this.beanFactory = beanFactory;
166+
}
158167
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
* @author Christoph Strobl
4545
* @author Oliver Gierke
4646
* @author Mark Paluch
47+
* @author Hebert Coelho
4748
*/
4849
public class JdbcRepositoryFactoryBean<T extends Repository<S, ID>, S, ID extends Serializable>
4950
extends TransactionalRepositoryFactoryBeanSupport<T, S, ID> implements ApplicationEventPublisherAware {
@@ -86,9 +87,10 @@ public void setApplicationEventPublisher(ApplicationEventPublisher publisher) {
8687
protected RepositoryFactorySupport doCreateRepositoryFactory() {
8788

8889
JdbcRepositoryFactory jdbcRepositoryFactory = new JdbcRepositoryFactory(dataAccessStrategy, mappingContext,
89-
converter, dialect, publisher, operations, beanFactory);
90+
converter, dialect, publisher, operations);
9091
jdbcRepositoryFactory.setQueryMappingConfiguration(queryMappingConfiguration);
9192
jdbcRepositoryFactory.setEntityCallbacks(entityCallbacks);
93+
jdbcRepositoryFactory.setBeanFactory(beanFactory);
9294

9395
return jdbcRepositoryFactory;
9496
}

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import org.junit.Before;
3333
import org.junit.Test;
3434
import org.mockito.stubbing.Answer;
35-
import org.springframework.beans.factory.BeanFactory;
3635
import org.springframework.context.ApplicationEventPublisher;
3736
import org.springframework.data.annotation.Id;
3837
import org.springframework.data.domain.PageRequest;
@@ -83,7 +82,6 @@ public class SimpleJdbcRepositoryEventsUnitTests {
8382

8483
DummyEntityRepository repository;
8584
DefaultDataAccessStrategy dataAccessStrategy;
86-
BeanFactory beanFactory = mock(BeanFactory.class);
8785

8886
@Before
8987
public void before() {
@@ -101,7 +99,7 @@ public void before() {
10199
doReturn(true).when(dataAccessStrategy).update(any(), any());
102100

103101
JdbcRepositoryFactory factory = new JdbcRepositoryFactory(dataAccessStrategy, context, converter,
104-
H2Dialect.INSTANCE, publisher, operations, beanFactory);
102+
H2Dialect.INSTANCE, publisher, operations);
105103

106104
this.repository = factory.getRepository(DummyEntityRepository.class);
107105
}

0 commit comments

Comments
 (0)