wip: moved base c* config to spring-cassandra

This commit is contained in:
Matthew Adams
2013-12-09 13:16:16 -06:00
parent 194ca1e2c1
commit a177562b27
3 changed files with 78 additions and 56 deletions

View File

@@ -0,0 +1,69 @@
/*
* Copyright 2011-2012 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.cassandra.config;
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.util.StringUtils;
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Session;
/**
* Base class for Spring Cassandra configuration using JavaConfig.
*
* @author Alex Shvid
* @author Matthew T. Adams
*/
@Configuration
public abstract class AbstractCassandraConfiguration {
/**
* The name of the keyspace to connect to. If {@literal null} or empty, then the system keyspace will be used.
*/
protected abstract String getKeyspaceName();
/**
* The {@link Cluster} instance to connect to. Must not be null.
*/
@Bean
public abstract Cluster cluster();
/**
* Creates a {@link Session} using the {@link Cluster} instance configured in {@link #cluster()}.
*
* @see #cluster()
*/
@Bean
public Session session() {
String keyspace = getKeyspaceName();
if (StringUtils.hasText(keyspace)) {
return cluster().connect(keyspace);
} else {
return cluster().connect();
}
}
/**
* A {@link CassandraTemplate} created from the {@link Session} returned by {@link #session()}.
*/
@Bean
public CassandraOperations cassandraTemplate() {
return new CassandraTemplate(session());
}
}

View File

@@ -20,6 +20,7 @@ import java.util.Set;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.cassandra.config.AbstractCassandraConfiguration;
import org.springframework.cassandra.core.CassandraOperations;
import org.springframework.cassandra.core.CassandraTemplate;
import org.springframework.context.annotation.Bean;
@@ -47,51 +48,14 @@ import com.datastax.driver.core.Session;
* Base class for Spring Data Cassandra configuration using JavaConfig.
*
* @author Alex Shvid
* @author Matthew T. Adams
*/
@Configuration
public abstract class AbstractCassandraConfiguration implements BeanClassLoaderAware {
/**
* Used by CassandraTemplate and CassandraAdminTemplate
*/
public abstract class AbstractSpringDataCassandraConfiguration extends AbstractCassandraConfiguration implements
BeanClassLoaderAware {
private ClassLoader beanClassLoader;
/**
* Return the name of the keyspace to connect to.
*
* @return must not be {@literal null}.
*/
protected abstract String getKeyspaceName();
/**
* Return the {@link Cluster} instance to connect to.
*
* @return
* @throws Exception
*/
@Bean
public abstract Cluster cluster() throws Exception;
/**
* Creates a {@link Session} to be used by the {@link SpringDataKeyspace}. Will use the {@link Cluster} instance
* configured in {@link #cluster()}.
*
* @see #cluster()
* @see #Keyspace()
* @return
* @throws Exception
*/
@Bean
public Session session() throws Exception {
String keyspace = getKeyspaceName();
if (StringUtils.hasText(keyspace)) {
return cluster().connect(keyspace);
} else {
return cluster().connect();
}
}
/**
* Creates a {@link SpringDataKeyspace} to be used by the {@link CassandraTemplate}. Will use the {@link Session}
* instance configured in {@link #session()} and {@link CassandraConverter} configured in {@link #converter()}.
@@ -109,8 +73,8 @@ public abstract class AbstractCassandraConfiguration implements BeanClassLoaderA
/**
* 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 AbstractCassandraConfiguration} the base package will be considered {@code com.acme} unless the method is
* overriden to implement alternate behaviour.
* {@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.
@@ -119,17 +83,6 @@ public abstract class AbstractCassandraConfiguration implements BeanClassLoaderA
return getClass().getPackage().getName();
}
/**
* Creates a {@link CassandraTemplate}.
*
* @return
* @throws Exception
*/
@Bean
public CassandraOperations cassandraTemplate() throws Exception {
return new CassandraTemplate(session());
}
/**
* Creates a {@link CassandraAdminTemplate}.
*
@@ -185,7 +138,7 @@ public abstract class AbstractCassandraConfiguration implements BeanClassLoaderA
for (BeanDefinition candidate : componentProvider.findCandidateComponents(basePackage)) {
initialEntitySet.add(ClassUtils.forName(candidate.getBeanClassName(),
AbstractCassandraConfiguration.class.getClassLoader()));
AbstractSpringDataCassandraConfiguration.class.getClassLoader()));
}
}

View File

@@ -5,7 +5,7 @@ import org.springframework.cassandra.core.CassandraTemplate;
import org.springframework.cassandra.core.SessionFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.cassandra.config.AbstractCassandraConfiguration;
import org.springframework.data.cassandra.config.AbstractSpringDataCassandraConfiguration;
import org.springframework.data.cassandra.core.CassandraDataOperations;
import org.springframework.data.cassandra.core.CassandraDataTemplate;
import org.springframework.data.cassandra.core.CassandraKeyspaceFactoryBean;
@@ -20,7 +20,7 @@ import com.datastax.driver.core.Cluster.Builder;
*
*/
@Configuration
public class TestConfig extends AbstractCassandraConfiguration {
public class TestConfig extends AbstractSpringDataCassandraConfiguration {
public static final String keyspace = "test";