diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/cassandra/CassandraDataAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/cassandra/CassandraDataAutoConfiguration.java index 0a85a34cc7..db8ff1626f 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/cassandra/CassandraDataAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/cassandra/CassandraDataAutoConfiguration.java @@ -16,6 +16,7 @@ package org.springframework.boot.autoconfigure.data.cassandra; +import java.util.Collections; import java.util.List; import com.datastax.driver.core.Cluster; @@ -41,9 +42,11 @@ import org.springframework.data.cassandra.config.SchemaAction; import org.springframework.data.cassandra.core.CassandraAdminOperations; import org.springframework.data.cassandra.core.CassandraTemplate; import org.springframework.data.cassandra.core.convert.CassandraConverter; +import org.springframework.data.cassandra.core.convert.CassandraCustomConversions; import org.springframework.data.cassandra.core.convert.MappingCassandraConverter; import org.springframework.data.cassandra.core.mapping.CassandraMappingContext; import org.springframework.data.cassandra.core.mapping.SimpleUserTypeResolver; +import org.springframework.data.convert.CustomConversions; import org.springframework.util.StringUtils; /** @@ -79,7 +82,7 @@ public class CassandraDataAutoConfiguration { @Bean @ConditionalOnMissingBean - public CassandraMappingContext cassandraMapping() throws ClassNotFoundException { + public CassandraMappingContext cassandraMapping(CustomConversions conversions) throws ClassNotFoundException { CassandraMappingContext context = new CassandraMappingContext(); List packages = EntityScanPackages.get(this.beanFactory) .getPackageNames(); @@ -93,13 +96,17 @@ public class CassandraDataAutoConfiguration { context.setUserTypeResolver(new SimpleUserTypeResolver(this.cluster, this.properties.getKeyspaceName())); } + context.setCustomConversions(conversions); return context; } @Bean @ConditionalOnMissingBean - public CassandraConverter cassandraConverter(CassandraMappingContext mapping) { - return new MappingCassandraConverter(mapping); + public CassandraConverter cassandraConverter(CassandraMappingContext mapping, + CustomConversions conversions) { + MappingCassandraConverter converter = new MappingCassandraConverter(mapping); + converter.setCustomConversions(conversions); + return converter; } @Bean @@ -123,4 +130,10 @@ public class CassandraDataAutoConfiguration { return new CassandraTemplate(session, converter); } + @Bean + @ConditionalOnMissingBean + public CustomConversions cassandraCustomConversions() { + return new CassandraCustomConversions(Collections.emptyList()); + } + } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/cassandra/CassandraDataAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/cassandra/CassandraDataAutoConfigurationTests.java index 3a1670fd3f..8d950fdf98 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/cassandra/CassandraDataAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/cassandra/CassandraDataAutoConfigurationTests.java @@ -16,6 +16,7 @@ package org.springframework.boot.autoconfigure.data.cassandra; +import java.util.Arrays; import java.util.Set; import com.datastax.driver.core.Session; @@ -26,13 +27,16 @@ import org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfigurati import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.autoconfigure.data.cassandra.city.City; import org.springframework.boot.autoconfigure.domain.EntityScan; +import org.springframework.boot.test.util.EnvironmentTestUtils; import org.springframework.boot.test.util.TestPropertyValues; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.FilterType; +import org.springframework.core.convert.converter.Converter; import org.springframework.data.cassandra.core.CassandraTemplate; +import org.springframework.data.cassandra.core.convert.CassandraCustomConversions; import org.springframework.data.cassandra.core.mapping.CassandraMappingContext; import org.springframework.data.cassandra.core.mapping.SimpleUserTypeResolver; import org.springframework.test.util.ReflectionTestUtils; @@ -101,6 +105,22 @@ public class CassandraDataAutoConfigurationTests { .isInstanceOf(SimpleUserTypeResolver.class); } + @Test + public void customConversions() { + this.context = new AnnotationConfigApplicationContext(); + EnvironmentTestUtils.addEnvironment(this.context, + "spring.data.cassandra.keyspaceName:boot_test"); + this.context.register(CustomConversionConfig.class, + TestConfiguration.class, + PropertyPlaceholderAutoConfiguration.class, + CassandraAutoConfiguration.class, CassandraDataAutoConfiguration.class); + this.context.refresh(); + CassandraTemplate template = this.context.getBean(CassandraTemplate.class); + assertThat(template.getConverter().getConversionService().canConvert(Person.class, + String.class)).isTrue(); + + } + @Configuration @ComponentScan(excludeFilters = @ComponentScan.Filter(classes = { Session.class }, type = FilterType.ASSIGNABLE_TYPE)) @@ -124,4 +144,26 @@ public class CassandraDataAutoConfigurationTests { } + @Configuration + static class CustomConversionConfig { + + @Bean + public CassandraCustomConversions cassandraCustomConversions() { + return new CassandraCustomConversions(Arrays.asList(new MyConverter())); + } + + } + + private static class MyConverter implements Converter { + + @Override + public String convert(Person o) { + return null; + } + } + + private static class Person { + + } + }