Add support for CustomConversions with Cassandra

This commit add the support for CustomConversions in
spring-data-cassandra. To customize, bean just need to be declared and
it will be auto-configured.

See gh-8534
pull/9556/head
Eddú Meléndez 8 years ago committed by Stephane Nicoll
parent 7f7c60610c
commit 744a1ed497

@ -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<String> 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());
}
}

@ -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<Person, String> {
@Override
public String convert(Person o) {
return null;
}
}
private static class Person {
}
}

Loading…
Cancel
Save