Skip to content

Commit 7c96059

Browse files
committed
BeanPropertyRowMapper uses ConversionService for date-time support
Issue: SPR-13888
1 parent f045129 commit 7c96059

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

spring-jdbc/src/main/java/org/springframework/jdbc/core/BeanPropertyRowMapper.java

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -34,6 +34,8 @@
3434
import org.springframework.beans.NotWritablePropertyException;
3535
import org.springframework.beans.PropertyAccessorFactory;
3636
import org.springframework.beans.TypeMismatchException;
37+
import org.springframework.core.convert.ConversionService;
38+
import org.springframework.core.convert.support.DefaultConversionService;
3739
import org.springframework.dao.DataRetrievalFailureException;
3840
import org.springframework.dao.InvalidDataAccessApiUsageException;
3941
import org.springframework.jdbc.support.JdbcUtils;
@@ -85,6 +87,9 @@ public class BeanPropertyRowMapper<T> implements RowMapper<T> {
8587
/** Whether we're defaulting primitives when mapping a null value */
8688
private boolean primitivesDefaultedForNullValue = false;
8789

90+
/** ConversionService for binding JDBC values to bean properties */
91+
private ConversionService conversionService = new DefaultConversionService();
92+
8893
/** Map of the fields we provide mapping for */
8994
private Map<String, PropertyDescriptor> mappedFields;
9095

@@ -179,6 +184,27 @@ public boolean isPrimitivesDefaultedForNullValue() {
179184
return this.primitivesDefaultedForNullValue;
180185
}
181186

187+
/**
188+
* Set a {@link ConversionService} for binding JDBC values to bean properties,
189+
* or {@code null} for none.
190+
* <p>Default is a {@link DefaultConversionService}, as of Spring 4.3. This
191+
* provides support for {@code java.time} conversion and other special types.
192+
* @since 4.3
193+
* @see #initBeanWrapper(BeanWrapper)
194+
*/
195+
public void setConversionService(ConversionService conversionService) {
196+
this.conversionService = conversionService;
197+
}
198+
199+
/**
200+
* Return a {@link ConversionService} for binding JDBC values to bean properties,
201+
* or {@code null} if none.
202+
* @since 4.3
203+
*/
204+
public ConversionService getConversionService() {
205+
return this.conversionService;
206+
}
207+
182208

183209
/**
184210
* Initialize the mapping metadata for the given class.
@@ -313,10 +339,17 @@ public T mapRow(ResultSet rs, int rowNumber) throws SQLException {
313339
/**
314340
* Initialize the given BeanWrapper to be used for row mapping.
315341
* To be called for each row.
316-
* <p>The default implementation is empty. Can be overridden in subclasses.
342+
* <p>The default implementation applies the configured {@link ConversionService},
343+
* if any. Can be overridden in subclasses.
317344
* @param bw the BeanWrapper to initialize
345+
* @see #getConversionService()
346+
* @see BeanWrapper#setConversionService
318347
*/
319348
protected void initBeanWrapper(BeanWrapper bw) {
349+
ConversionService cs = getConversionService();
350+
if (cs != null) {
351+
bw.setConversionService(cs);
352+
}
320353
}
321354

322355
/**

0 commit comments

Comments
 (0)