Sak*_*ket 8 java spring spring-data spring-data-cassandra
Spring Data Cassandra是否支持同一应用程序上下文中的多个键空间存储库?我正在使用以下JavaConfig类设置cassandra spring数据配置
@Configuration
@EnableCassandraRepositories(basePackages = "com.blah.repository")
public class CassandraConfig extends AbstractCassandraConfiguration {
@Override
public String getKeyspaceName() {
return "keyspace1";
}
Run Code Online (Sandbox Code Playgroud)
在将存储库类移动到另一个包之后,我尝试创建第二个配置类.
@Configuration
@EnableCassandraRepositories(basePackages = "com.blah.secondrepository")
public class SecondCassandraConfig extends AbstractCassandraConfiguration {
@Override
public String getKeyspaceName() {
return "keyspace2";
}
Run Code Online (Sandbox Code Playgroud)
但是,在这种情况下,第一个集合,如果存储库失败,因为在键空间中找不到实体的已配置列族.我认为它可能正在寻找第二个键空间中的列族.
spring-data-cassandra是否支持多个密钥空间存储库?我找到多个键空间的引用的唯一地方就在这里.但它没有解释是否可以使用存储库完成此操作?
工作APP示例: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
Run Code Online (Sandbox Code Playgroud)
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;
}
}
Run Code Online (Sandbox Code Playgroud)
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;
}
}
Run Code Online (Sandbox Code Playgroud)
4)同样适用于test2,只需使用不同的软件包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> {
// ....
}
Run Code Online (Sandbox Code Playgroud)
尝试为每个键空间显式命名您的bean,并在注释的属性CassandraTemplate中使用这些名称(有关更改,请参阅 with 行)。@EnableCassandraRepositoriescassandraTemplateRef/* CHANGED */
在您的第一个配置中:
@Configuration
@EnableCassandraRepositories(basePackages = "com.blah.repository",
/* CHANGED */ cassandraTemplateRef = "template1")
public class CassandraConfig extends AbstractCassandraConfiguration {
@Override
public String getKeyspaceName() {
return "keyspace1";
}
/* CHANGED */
@Override
@Bean(name = "template1")
public CassandraAdminOperations cassandraTemplate() throws Exception {
return new CassandraAdminTemplate(session().getObject(), cassandraConverter());
}
Run Code Online (Sandbox Code Playgroud)
...在你的第二个配置中:
@Configuration
@EnableCassandraRepositories(basePackages = "com.blah.secondrepository",
/* CHANGED */ cassandraTemplateRef = "template2")
public class SecondCassandraConfig extends AbstractCassandraConfiguration {
@Override
public String getKeyspaceName() {
return "keyspace2";
}
/* CHANGED */
@Override
@Bean(name = "template2")
public CassandraAdminOperations cassandraTemplate() throws Exception {
return new CassandraAdminTemplate(session().getObject(), cassandraConverter());
}
Run Code Online (Sandbox Code Playgroud)
我认为这可能会起作用。如果没有请回帖。
| 归档时间: |
|
| 查看次数: |
5176 次 |
| 最近记录: |