# 30.7.1 连接Cassandra

你可以注入一个自动配置的`CassandraTemplate`或Cassandra `Session`实例，就像注入其他Spring Bean那样。`spring.data.cassandra.*`属性可用来自定义该连接，通常你需要提供`keyspace-name`和`contact-points`属性。如下所示：

```
spring.data.cassandra.keyspace-name=mykeyspace
spring.data.cassandra.contact-points=cassandrahost1,cassandrahost2
```

下面的代码展示了怎样注入Cassandra bean：

```java
@Component
public class MyBean {

    private CassandraTemplate template;

    @Autowired
    public MyBean(CassandraTemplate template) {
        this.template = template;
    }

    // ...

}
```

如果添加自己的`CassandraTemplate`类型的`@Bean`，它将替换默认实例。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://jack80342.gitbook.io/spring-boot/iv.-spring-boot-features/30.-working-with-nosql-technologies/30.7-cassandra/30.7.1-connecting-to-cassandra.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
