|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2015 the original author or authors. |
| 2 | + * Copyright 2002-2016 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
34 | 34 | import org.springframework.beans.NotWritablePropertyException;
|
35 | 35 | import org.springframework.beans.PropertyAccessorFactory;
|
36 | 36 | import org.springframework.beans.TypeMismatchException;
|
| 37 | +import org.springframework.core.convert.ConversionService; |
| 38 | +import org.springframework.core.convert.support.DefaultConversionService; |
37 | 39 | import org.springframework.dao.DataRetrievalFailureException;
|
38 | 40 | import org.springframework.dao.InvalidDataAccessApiUsageException;
|
39 | 41 | import org.springframework.jdbc.support.JdbcUtils;
|
@@ -85,6 +87,9 @@ public class BeanPropertyRowMapper<T> implements RowMapper<T> {
|
85 | 87 | /** Whether we're defaulting primitives when mapping a null value */
|
86 | 88 | private boolean primitivesDefaultedForNullValue = false;
|
87 | 89 |
|
| 90 | + /** ConversionService for binding JDBC values to bean properties */ |
| 91 | + private ConversionService conversionService = new DefaultConversionService(); |
| 92 | + |
88 | 93 | /** Map of the fields we provide mapping for */
|
89 | 94 | private Map<String, PropertyDescriptor> mappedFields;
|
90 | 95 |
|
@@ -179,6 +184,27 @@ public boolean isPrimitivesDefaultedForNullValue() {
|
179 | 184 | return this.primitivesDefaultedForNullValue;
|
180 | 185 | }
|
181 | 186 |
|
| 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 | + |
182 | 208 |
|
183 | 209 | /**
|
184 | 210 | * Initialize the mapping metadata for the given class.
|
@@ -313,10 +339,17 @@ public T mapRow(ResultSet rs, int rowNumber) throws SQLException {
|
313 | 339 | /**
|
314 | 340 | * Initialize the given BeanWrapper to be used for row mapping.
|
315 | 341 | * 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. |
317 | 344 | * @param bw the BeanWrapper to initialize
|
| 345 | + * @see #getConversionService() |
| 346 | + * @see BeanWrapper#setConversionService |
318 | 347 | */
|
319 | 348 | protected void initBeanWrapper(BeanWrapper bw) {
|
| 349 | + ConversionService cs = getConversionService(); |
| 350 | + if (cs != null) { |
| 351 | + bw.setConversionService(cs); |
| 352 | + } |
320 | 353 | }
|
321 | 354 |
|
322 | 355 | /**
|
|
0 commit comments