DATAJDBC-147 - Support registration von converters via ConversionCustomizers.

This commit is contained in:
Jens Schauder
2017-11-01 14:37:40 +01:00
committed by Greg Turnquist
parent 207278c891
commit 3c9765c9b8
17 changed files with 127 additions and 47 deletions

View File

@@ -21,13 +21,9 @@ import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.dao.NonTransientDataAccessException;
import org.springframework.data.convert.Jsr310Converters;
import org.springframework.data.jdbc.mapping.model.BasicJdbcPersistentEntityInformation;
import org.springframework.data.jdbc.mapping.model.JdbcMappingContext;
import org.springframework.data.jdbc.mapping.model.JdbcPersistentEntity;
@@ -58,7 +54,6 @@ public class DefaultDataAccessStrategy implements DataAccessStrategy {
private final SqlGeneratorSource sqlGeneratorSource;
private final NamedParameterJdbcOperations operations;
private final JdbcMappingContext context;
private final ConversionService conversions = getDefaultConversionService();
private final DataAccessStrategy accessStrategy;
public DefaultDataAccessStrategy(SqlGeneratorSource sqlGeneratorSource, NamedParameterJdbcOperations operations,
@@ -222,14 +217,6 @@ public class DefaultDataAccessStrategy implements DataAccessStrategy {
return operations.queryForObject(existsSql, parameter, Boolean.class);
}
private static GenericConversionService getDefaultConversionService() {
DefaultConversionService conversionService = new DefaultConversionService();
Jsr310Converters.getConvertersToRegister().forEach(conversionService::addConverter);
return conversionService;
}
private <S> MapSqlParameterSource getPropertyMap(final S instance, JdbcPersistentEntity<S> persistentEntity) {
MapSqlParameterSource parameters = new MapSqlParameterSource();
@@ -295,7 +282,7 @@ public class DefaultDataAccessStrategy implements DataAccessStrategy {
}
private <T> EntityRowMapper<T> getEntityRowMapper(Class<T> domainType) {
return new EntityRowMapper<>(getRequiredPersistentEntity(domainType), conversions, context, accessStrategy);
return new EntityRowMapper<>(getRequiredPersistentEntity(domainType), context.getConversions(), context, accessStrategy);
}
private RowMapper getMapEntityRowMapper(JdbcPersistentProperty property) {
@@ -323,7 +310,7 @@ public class DefaultDataAccessStrategy implements DataAccessStrategy {
Object id = persistentEntity == null ? null : persistentEntity.getIdentifierAccessor(from).getIdentifier();
return conversions.convert(id == null ? from : id, to);
return context.getConversions().convert(id == null ? from : id, to);
}
private SqlGenerator sql(Class<?> domainType) {

View File

@@ -0,0 +1,26 @@
/*
* Copyright 2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.jdbc.mapping.model;
import org.springframework.core.convert.support.GenericConversionService;
/**
* @author Jens Schauder
*/
public interface ConversionCustomizer {
void customize(GenericConversionService conversions);
}

View File

@@ -27,6 +27,10 @@ import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.data.convert.Jsr310Converters;
import org.springframework.data.mapping.PropertyPath;
import org.springframework.data.mapping.context.AbstractMappingContext;
import org.springframework.data.mapping.context.MappingContext;
@@ -50,13 +54,21 @@ public class JdbcMappingContext extends AbstractMappingContext<JdbcPersistentEnt
));
private final @Getter NamingStrategy namingStrategy;
private GenericConversionService conversions = getDefaultConversionService();
public JdbcMappingContext(NamingStrategy namingStrategy) {
public JdbcMappingContext(NamingStrategy namingStrategy, ConversionCustomizer customizer) {
this.namingStrategy = namingStrategy;
customizer.customize(conversions);
setSimpleTypeHolder(new SimpleTypeHolder(CUSTOM_SIMPLE_TYPES, true));
}
public JdbcMappingContext() {
this(new DefaultNamingStrategy(), __ -> {});
}
public List<PropertyPath> referencedEntities(Class<?> rootType, PropertyPath path) {
List<PropertyPath> paths = new ArrayList<>();
@@ -103,4 +115,16 @@ public class JdbcMappingContext extends AbstractMappingContext<JdbcPersistentEnt
return new BasicJdbcPersistentEntityInformation<>((JdbcPersistentEntity<T>) getRequiredPersistentEntity(type));
}
public ConversionService getConversions() {
return conversions;
}
private static GenericConversionService getDefaultConversionService() {
DefaultConversionService conversionService = new DefaultConversionService();
Jsr310Converters.getConvertersToRegister().forEach(conversionService::addConverter);
return conversionService;
}
}

View File

@@ -32,6 +32,7 @@ import org.springframework.data.jdbc.core.DataAccessStrategy;
import org.springframework.data.jdbc.core.DefaultDataAccessStrategy;
import org.springframework.data.jdbc.core.DelegatingDataAccessStrategy;
import org.springframework.data.jdbc.core.SqlGeneratorSource;
import org.springframework.data.jdbc.mapping.model.ConversionCustomizer;
import org.springframework.data.jdbc.mapping.model.DefaultNamingStrategy;
import org.springframework.data.jdbc.mapping.model.JdbcMappingContext;
import org.springframework.data.jdbc.mapping.model.NamingStrategy;
@@ -65,22 +66,23 @@ public class JdbcRepositoryFactoryBean<T extends Repository<S, ID>, S, ID extend
private static final String DATA_SOURCE_BEAN_NAME = "dataSource";
private static final String NAMING_STRATEGY_BEAN_NAME = "namingStrategy";
private static final String SQL_SESSION_FACTORY_BEAN_NAME = "sqlSessionFactory";
private static final String CONVERSION_CUSTOMIZER_BEAN_NAME = "conversionCustomizer";
private final ApplicationEventPublisher applicationEventPublisher;
private final ApplicationContext context;
private final ApplicationContext applicationContext;
JdbcRepositoryFactoryBean(Class<? extends T> repositoryInterface, ApplicationEventPublisher applicationEventPublisher,
ApplicationContext context) {
ApplicationContext applicationContext) {
super(repositoryInterface);
this.applicationEventPublisher = applicationEventPublisher;
this.context = context;
this.applicationContext = applicationContext;
}
@Override
protected RepositoryFactorySupport doCreateRepositoryFactory() {
final JdbcMappingContext context = new JdbcMappingContext(findOrCreateNamingStrategy());
final JdbcMappingContext context = new JdbcMappingContext(findOrCreateNamingStrategy(), findOrCreateConversionCustomizer());
return new JdbcRepositoryFactory(applicationEventPublisher, context, createDataAccessStrategy(context));
}
@@ -156,6 +158,10 @@ public class JdbcRepositoryFactoryBean<T extends Repository<S, ID>, S, ID extend
return getNamingStrategy().orElse(new DefaultNamingStrategy());
}
private ConversionCustomizer findOrCreateConversionCustomizer() {
return getConversionCustomizer().orElse(conversionService->{});
}
private Optional<NamedParameterJdbcOperations> getNamedParameterJdbcOperations() {
return getBean(NamedParameterJdbcOperations.class, NAMED_PARAMETER_JDBC_OPERATIONS_BEAN_NAME);
}
@@ -172,9 +178,13 @@ public class JdbcRepositoryFactoryBean<T extends Repository<S, ID>, S, ID extend
return getBean(NamingStrategy.class, NAMING_STRATEGY_BEAN_NAME);
}
private Optional<ConversionCustomizer> getConversionCustomizer() {
return getBean(ConversionCustomizer.class, CONVERSION_CUSTOMIZER_BEAN_NAME);
}
private <R> Optional<R> getBean(Class<R> type, String name) {
Map<String, R> beansOfType = context.getBeansOfType(type);
Map<String, R> beansOfType = applicationContext.getBeansOfType(type);
if (beansOfType.size() == 1) {
return beansOfType.values().stream().findFirst();