Spring Data Cassandra是否支持同一应用程序上下文中的多个键空间存储库?我正在使用以下
JavaConfig类设置cassandra spring数据配置
@Configuration @EnableCassandraRepositories(basePackages = "com.blah.repository") public class CassandraConfig extends AbstractCassandraConfiguration { @Override public String getKeyspaceName() { return "keyspace1"; }
在将存储库类移动到另一个包之后,我尝试创建第二个配置类.
@Configuration @EnableCassandraRepositories(basePackages = "com.blah.secondrepository") public class SecondCassandraConfig extends AbstractCassandraConfiguration { @Override public String getKeyspaceName() { return "keyspace2"; }
但是,在这种情况下,第一个集合,如果存储库失败,因为在键空间中找不到实体的已配置列族.我认为它可能正在寻找第二个键空间中的列族.
spring-data-cassandra是否支持多个密钥空间存储库?我找到多个键空间的引用的唯一地方是here.但它没有解释是否可以使用存储库完成此操作?
解决方法
工作APP样本:
http://valchkou.com/spring-boot-cassandra.html#multikeyspace
http://valchkou.com/spring-boot-cassandra.html#multikeyspace
您需要的Idea会覆盖默认bean:sessionfactory和template
样品:
1)application.yml
spring: data: cassandra: test1: keyspace-name: test1_keyspace contact-points: localhost test2: keyspace-name: test2_keyspace contact-points: localhost
2)基本配置类
public abstract class CassandraBaseConfig extends AbstractCassandraConfiguration{ protected String contactPoints; protected String keyspaceName; public String getContactPoints() { return contactPoints; } public void setContactPoints(String contactPoints) { this.contactPoints = contactPoints; } public void setKeyspaceName(String keyspaceName) { this.keyspaceName = keyspaceName; } @Override protected String getKeyspaceName() { return keyspaceName; } }
3)配置test1的实现
package com.sample.repo.test1; @Configuration @ConfigurationProperties("spring.data.cassandra.test1") @EnableCassandraRepositories( basePackages = "com.sample.repo.test1",cassandraTemplateRef = "test1Template" ) public class Test1Config extends CassandraBaseConfig { @Override @Primary @Bean(name = "test1Template") public CassandraAdminOperations cassandraTemplate() throws Exception { return new CassandraAdminTemplate(session().getObject(),cassandraConverter()); } @Override @Bean(name = "test1Session") public CassandraSessionfactorybean session() throws Exception { CassandraSessionfactorybean session = new CassandraSessionfactorybean(); session.setCluster(cluster().getObject()); session.setConverter(cassandraConverter()); session.setKeyspaceName(getKeyspaceName()); session.setSchemaAction(getSchemaAction()); session.setStartupScripts(getStartupScripts()); session.setShutdownScripts(getShutdownScripts()); return session; } }
4)同样适用于test2,只需使用不同的包
package com.sample.repo.test2;
5)在专用包中为每个键空间放置repo
即
package com.sample.repo.test1; @Repository public interface RepositoryForTest1 extends CassandraRepository<MyEntity> { // .... } package com.sample.repo.test2; @Repository public interface RepositoryForTest2 extends CassandraRepository<MyEntity> { // .... }