《Spring Data官方文檔》5.3. Connecting to Cassandra with Spring至5.5. Introduction to CassandraTemplate
5.3. 連接到Spring Cassandra
5.3.1. 外部化連接屬性
你需要連接到Cassandra來創建配置文件信息。接觸點是鍵空間的所必需的最小字段,但是為了清楚起見,最好能添加端口。
我們稱這些為cassandra.properties
cassandra.contactpoints=10.1.55.80,10.1.55.81
cassandra.port=9042
cassandra.keyspace=showcase
下麵兩個例子中我們將使用spring把這些屬性加載到Spring上下文.
5.3.2. XML 配置
一個Cassandra基礎配置中的XML配置元素如下所示。 這些元素都使用默認bean名稱來保持配置代碼幹淨和可讀。
這個例子說明配置Spring連接Cassandra很方便,而且還有一些其他選項可以用。Spring Data Cassandra配置中還提供了可供DataStax Java驅動程序使用的一切選項。 這包括但不限於身份驗證,負載平衡策略,重試策略和池選項。所有Spring Data Cassandra方法名稱和XML元素都采用驅動程序上的配置選項同樣(或差不都)的命名,因此應該直接的映射任意已有的驅動程序配置。
<?xml version='1.0'?>
<beans xmlns="https://www.springframework.org/schema/beans"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns:cassandra="https://www.springframework.org/schema/data/cassandra"
xmlns:context="https://www.springframework.org/schema/context"
xsi:schemaLocation="https://www.springframework.org/schema/cql https://www.springframework.org/schema/cql/spring-cql-1.0.xsd
https://www.springframework.org/schema/data/cassandra https://www.springframework.org/schema/data/cassandra/spring-cassandra-1.0.xsd
https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
https://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context-3.2.xsd">
<!-- Loads the properties into the Spring Context and uses them to fill
in placeholders in the bean definitions -->
<context:property-placeholder location="classpath:cassandra.properties" />
<!-- REQUIRED: The Cassandra Cluster -->
<cassandra:cluster contact-points="${cassandra.contactpoints}"
port="${cassandra.port}" />
<!-- REQUIRED: The Cassandra Session, built from the Cluster, and attaching
to a keyspace -->
<cassandra:session keyspace-name="${cassandra.keyspace}" />
<!-- REQUIRED: The Default Cassandra Mapping Context used by CassandraConverter -->
<cassandra:mapping />
<!-- REQUIRED: The Default Cassandra Converter used by CassandraTemplate -->
<cassandra:converter />
<!-- REQUIRED: The Cassandra Template is the building block of all Spring
Data Cassandra -->
<cassandra:template />
<!-- OPTIONAL: If you are using Spring Data Cassandra Repositories, add
your base packages to scan here -->
<cassandra:repositories base-package="org.spring.cassandra.example.repo" />
</beans>
5.3.3. Java 配置
下麵的類展示了AnnotationConfigApplicationContext(aka JavaConfig)中的最基本的Cassandra配置。
package org.spring.cassandra.example.config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.data.cassandra.config.CassandraClusterFactoryBean;
import org.springframework.data.cassandra.config.CassandraSessionFactoryBean;
import org.springframework.data.cassandra.config.SchemaAction;
import org.springframework.data.cassandra.convert.CassandraConverter;
import org.springframework.data.cassandra.convert.MappingCassandraConverter;
import org.springframework.data.cassandra.core.CassandraOperations;
import org.springframework.data.cassandra.core.CassandraTemplate;
import org.springframework.data.cassandra.mapping.BasicCassandraMappingContext;
import org.springframework.data.cassandra.mapping.CassandraMappingContext;
import org.springframework.data.cassandra.repository.config.EnableCassandraRepositories;
@Configuration
@PropertySource(value = { "classpath:cassandra.properties" })
@EnableCassandraRepositories(basePackages = { "org.spring.cassandra.example.repo" })
public class CassandraConfig {
private static final Logger LOG = LoggerFactory.getLogger(CassandraConfig.class);
@Autowired
private Environment env;
@Bean
public CassandraClusterFactoryBean cluster() {
CassandraClusterFactoryBean cluster = new CassandraClusterFactoryBean();
cluster.setContactPoints(env.getProperty("cassandra.contactpoints"));
cluster.setPort(Integer.parseInt(env.getProperty("cassandra.port")));
return cluster;
}
@Bean
public CassandraMappingContext mappingContext() {
return new BasicCassandraMappingContext();
}
@Bean
public CassandraConverter converter() {
return new MappingCassandraConverter(mappingContext());
}
@Bean
public CassandraSessionFactoryBean session() throws Exception {
CassandraSessionFactoryBean session = new CassandraSessionFactoryBean();
session.setCluster(cluster().getObject());
session.setKeyspaceName(env.getProperty("cassandra.keyspace"));
session.setConverter(converter());
session.setSchemaAction(SchemaAction.NONE);
return session;
}
@Bean
public CassandraOperations cassandraTemplate() throws Exception {
return new CassandraTemplate(session().getObject());
}
}
5.5. CassandraTemplate簡介
5.5.1. 實例化CassandraTemplate
雖然我們在上麵展示一個你可以直接實例化它的例子,但是’CassandraTemplate’應該始終被配置為一個Spring Bean。為了這個存在的Spring模塊的目的,在此我們假定我們使用Spring容器。
‘CassandraTemplate’是’CassandraOperations’的實現類。 你應該指定你的’CassandraTemplate’實現它的接口定義,’CassandraOperations’。
有兩個簡單的方式來獲取’CassandraTemplate’,取決於你如何加載你的Spring應用程序上下文。
自動裝配
@Autowired
private CassandraOperations cassandraOperations;
像所有Spring Autowiring,這裏假設在’ApplicationContext’中隻有一個類型為’CassandraOperations’的bean。 如果您有多個“CassandraTemplate”bean(如果您在同一個項目中使用多個鍵空間,則會是這種情況),可以添加 [email protected]/* */`注解到你想要自動裝配的bean。
@Autowired
@Qualifier("myTemplateBeanId")
private CassandraOperations cassandraOperations;
使用ApplicationContext來查找Bean
你也可以從’ApplicationContext’中查找’CassandraTemplate’bean。
CassandraOperations cassandraOperations = applicationContext.getBean("cassandraTemplate", CassandraOperations.class);
最後更新:2017-05-19 11:31:56