updated java config

This commit is contained in:
Matthew Adams
2013-12-13 14:01:40 -06:00
parent b32c132423
commit 5d85127dff
4 changed files with 40 additions and 52 deletions

View File

@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.cassandra.config;
package org.springframework.data.cassandra.config.java;
import java.util.HashSet;
import java.util.Set;
@@ -51,13 +51,8 @@ public abstract class AbstractSpringDataCassandraConfiguration extends AbstractC
private ClassLoader beanClassLoader;
/**
* Return the base package to scan for mapped {@link Table}s. Will return the package name of the configuration class'
* (the concrete class, not this one here) by default. So if you have a {@code com.acme.AppConfig} extending
* {@link AbstractSpringDataCassandraConfiguration} the base package will be considered {@code com.acme} unless the
* method is overriden to implement alternate behaviour.
*
* @return the base package to scan for mapped {@link Table} classes or {@literal null} to not enable scanning for
* entities.
* The base package to scan for entities annotated with {@link Table} annotations. By default, returns the package
* name of {@literal this} (<code>this.getClass().getPackage().getName()</code>).
*/
protected String getMappingBasePackage() {
return getClass().getPackage().getName();
@@ -65,44 +60,42 @@ public abstract class AbstractSpringDataCassandraConfiguration extends AbstractC
/**
* Creates a {@link CassandraAdminTemplate}.
*
* @return
* @throws Exception
*/
@Bean
public CassandraAdminOperations adminTemplate() throws Exception {
public CassandraAdminOperations adminTemplate() {
return new CassandraAdminTemplate(session());
}
/**
* Return the {@link MappingContext} instance to map Entities to properties.
*
* @return
* @throws Exception
* @throws ClassNotFoundException
*/
@Bean
public MappingContext<? extends CassandraPersistentEntity<?>, CassandraPersistentProperty> mappingContext() {
return new CassandraMappingContext();
public MappingContext<? extends CassandraPersistentEntity<?>, CassandraPersistentProperty> cassandraMappingContext()
throws ClassNotFoundException {
CassandraMappingContext context = new CassandraMappingContext();
context.setInitialEntitySet(getInitialEntitySet());
return context;
}
/**
* Return the {@link CassandraConverter} instance to convert Rows to Objects, Objects to BuiltStatements
*
* @return
* @throws Exception
* @throws ClassNotFoundException
*/
@Bean
public CassandraConverter converter() {
MappingCassandraConverter converter = new MappingCassandraConverter(mappingContext());
public CassandraConverter converter() throws ClassNotFoundException {
MappingCassandraConverter converter = new MappingCassandraConverter(cassandraMappingContext());
converter.setBeanClassLoader(beanClassLoader);
return converter;
}
/**
* Scans the mapping base package for classes annotated with {@link Table}.
* Scans the mapping base package for entity classes annotated with {@link Table} or {@link Persistent}.
*
* @see #getMappingBasePackage()
* @return
* @return <code>Set&lt;Class&lt;?&gt;&gt;</code> representing the annotated entity classes found.
* @throws ClassNotFoundException
*/
protected Set<Class<?>> getInitialEntitySet() throws ClassNotFoundException {
@@ -116,19 +109,18 @@ public abstract class AbstractSpringDataCassandraConfiguration extends AbstractC
componentProvider.addIncludeFilter(new AnnotationTypeFilter(Table.class));
componentProvider.addIncludeFilter(new AnnotationTypeFilter(Persistent.class));
// TODO: figure out which ClassLoader to use here
ClassLoader classLoader = getClass().getClassLoader();
for (BeanDefinition candidate : componentProvider.findCandidateComponents(basePackage)) {
initialEntitySet.add(ClassUtils.forName(candidate.getBeanClassName(),
AbstractSpringDataCassandraConfiguration.class.getClassLoader()));
initialEntitySet.add(ClassUtils.forName(candidate.getBeanClassName(), classLoader));
}
}
return initialEntitySet;
}
/**
* Bean ClassLoader Aware for CassandraTemplate/CassandraAdminTemplate
*/
@Override
public void setBeanClassLoader(ClassLoader classLoader) {
this.beanClassLoader = classLoader;
}

View File

@@ -18,27 +18,24 @@ package org.springframework.data.cassandra.convert;
import org.springframework.beans.factory.InitializingBean;
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.EntityInstantiators;
/**
* Base class for {@link CassandraConverter} implementations. Sets up a {@link GenericConversionService} and populates
* basic converters.
* Base class for {@link CassandraConverter} implementations. Sets up a {@link ConversionService} and populates basic
* converters.
*
* @author Alex Shvid
* @author Matthew T. Adams
*/
public abstract class AbstractCassandraConverter implements CassandraConverter, InitializingBean {
protected final GenericConversionService conversionService;
protected final ConversionService conversionService;
protected EntityInstantiators instantiators = new EntityInstantiators();
/**
* Creates a new {@link AbstractCassandraConverter} using the given {@link GenericConversionService}.
*
* @param conversionService
* Creates a new {@link AbstractCassandraConverter} using the given {@link ConversionService}.
*/
public AbstractCassandraConverter(GenericConversionService conversionService) {
public AbstractCassandraConverter(ConversionService conversionService) {
this.conversionService = conversionService == null ? new DefaultConversionService() : conversionService;
}

View File

@@ -97,18 +97,12 @@ public class MappingCassandraConverter extends AbstractCassandraConverter implem
return readRowInternal(persistentEntity, row);
}
/*
* (non-Javadoc)
* @see org.springframework.data.convert.EntityConverter#getMappingContext()
*/
@Override
public MappingContext<? extends CassandraPersistentEntity<?>, CassandraPersistentProperty> getMappingContext() {
return mappingContext;
}
/*
* (non-Javadoc)
* @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext)
*/
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
this.spELContext = new SpELContext(this.spELContext, applicationContext);
@@ -131,6 +125,7 @@ public class MappingCassandraConverter extends AbstractCassandraConverter implem
entity.doWithProperties(new PropertyHandler<CassandraPersistentProperty>() {
@Override
public void doWithPersistentProperty(CassandraPersistentProperty prop) {
MappingCassandraConverter.this.handlePersistentPropertyRead(row, entity, prop, propertyProvider, wrapper);
@@ -161,13 +156,14 @@ public class MappingCassandraConverter extends AbstractCassandraConverter implem
wrapper.setProperty(prop, obj, useFieldAccessOnly);
}
public boolean getUseFieldAccessOnly() {
return useFieldAccessOnly;
}
public void setUseFieldAccessOnly(boolean useFieldAccessOnly) {
this.useFieldAccessOnly = useFieldAccessOnly;
}
/* (non-Javadoc)
* @see org.springframework.data.convert.EntityWriter#write(java.lang.Object, java.lang.Object)
*/
@Override
public <R> R read(Class<R> type, Object row) {
if (row instanceof Row) {
@@ -176,9 +172,6 @@ public class MappingCassandraConverter extends AbstractCassandraConverter implem
throw new MappingException("Unknown row object " + row.getClass().getName());
}
/* (non-Javadoc)
* @see org.springframework.data.convert.EntityWriter#write(java.lang.Object, java.lang.Object)
*/
@Override
public void write(Object obj, Object builtStatement) {
@@ -211,6 +204,7 @@ public class MappingCassandraConverter extends AbstractCassandraConverter implem
// Write the properties
entity.doWithProperties(new PropertyHandler<CassandraPersistentProperty>() {
@Override
public void doWithPersistentProperty(CassandraPersistentProperty prop) {
Object propertyObj = wrapper.getProperty(prop, prop.getType(), useFieldAccessOnly);
@@ -231,6 +225,7 @@ public class MappingCassandraConverter extends AbstractCassandraConverter implem
// Write the properties
entity.doWithProperties(new PropertyHandler<CassandraPersistentProperty>() {
@Override
public void doWithPersistentProperty(CassandraPersistentProperty prop) {
Object propertyObj = wrapper.getProperty(prop, prop.getType(), useFieldAccessOnly);
@@ -256,6 +251,7 @@ public class MappingCassandraConverter extends AbstractCassandraConverter implem
// Write the properties
entity.doWithProperties(new PropertyHandler<CassandraPersistentProperty>() {
@Override
public void doWithPersistentProperty(CassandraPersistentProperty prop) {
if (prop.isIdProperty()) {
@@ -272,6 +268,7 @@ public class MappingCassandraConverter extends AbstractCassandraConverter implem
}
@Override
public CreateTableSpecification getCreateTableSpecification(CassandraPersistentEntity<?> entity) {
final CreateTableSpecification spec = new CreateTableSpecification();
@@ -279,6 +276,7 @@ public class MappingCassandraConverter extends AbstractCassandraConverter implem
spec.name(entity.getTableName());
entity.doWithProperties(new PropertyHandler<CassandraPersistentProperty>() {
@Override
public void doWithPersistentProperty(CassandraPersistentProperty prop) {
if (prop.isCompositePrimaryKey()) {
@@ -286,6 +284,7 @@ public class MappingCassandraConverter extends AbstractCassandraConverter implem
CassandraPersistentEntity<?> pkEntity = mappingContext.getPersistentEntity(prop.getRawType());
pkEntity.doWithProperties(new PropertyHandler<CassandraPersistentProperty>() {
@Override
public void doWithPersistentProperty(CassandraPersistentProperty pkProp) {
if (pkProp.isPartitionKeyColumn()) {

View File

@@ -5,7 +5,7 @@ import org.springframework.cassandra.core.CassandraOperations;
import org.springframework.cassandra.core.CassandraTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.cassandra.config.AbstractSpringDataCassandraConfiguration;
import org.springframework.data.cassandra.config.java.AbstractSpringDataCassandraConfiguration;
import org.springframework.data.cassandra.convert.CassandraConverter;
import org.springframework.data.cassandra.convert.MappingCassandraConverter;
import org.springframework.data.cassandra.core.CassandraDataOperations;
@@ -66,7 +66,7 @@ public class TestConfig extends AbstractSpringDataCassandraConfiguration {
}
@Bean
public CassandraDataOperations cassandraDataTemplate() {
public CassandraDataOperations cassandraDataTemplate() throws ClassNotFoundException {
CassandraDataOperations template = new CassandraDataTemplate(sessionFactoryBean().getObject(), converter(),
keyspaceName);