直接上程式碼:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
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.SchemaAction;
import org.springframework.data.cassandra.config.java.AbstractCassandraConfiguration;
import org.springframework.data.cassandra.repository.config.EnableCassandraRepositories;
@Configuration
@PropertySource(value = { "classpath:cassandra.properties" })
@EnableCassandraRepositories
public class CassandraConfig extends AbstractCassandraConfiguration {
@Autowired
private Environment env;
@Value("${cassandra.keyspace}")
protected String keySpace;
@Value("${cassandra.basepackages}")
private String basePackages;
@Override
protected String getKeyspaceName() {
return keySpace;
}
@Override
public String[] getEntityBasePackages() {
return new String[] {basePackages};
}
@Override
public SchemaAction getSchemaAction() {
return SchemaAction.CREATE_IF_NOT_EXISTS;
}
public CassandraClusterFactoryBean cluster(){
CassandraClusterFactoryBean cluster=new CassandraClusterFactoryBean();
cluster.setContactPoints(env.getProperty("cassandra.contactpoints"));
cluster.setUsername(env.getProperty("cassandra.username"));
cluster.setPassword(env.getProperty("cassandra.password"));
cluster.setPort(new Integer(env.getProperty("cassandra.port")));
return cluster;
}
}
複製程式碼