> For the complete documentation index, see [llms.txt](https://jack80342.gitbook.io/spring-boot/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jack80342.gitbook.io/spring-boot/iv.-spring-boot-features/30.-working-with-nosql-technologies/30.6-elasticsearch/30.6.2-connecting-to-elasticsearch-by-using-spring-data.md).

# 30.6.2 使用Spring Data连接Elasticsearch

为了连接Elasticsearch，你必须提供一个或者更多的集群节点的地址。可以通过设置`spring.data.elasticsearch.cluster-nodes`（逗号分隔的‘host:port’列表），来指定地址。当配置完成后，`ElasticsearchTemplate`或者`TransportClient`就能够像其它Spring bean一样被注入。如下所示：

```
spring.data.elasticsearch.cluster-nodes=localhost:9300
```

```java
@Component
public class MyBean {

    private final ElasticsearchTemplate template;

    public MyBean(ElasticsearchTemplate template) {
        this.template = template;
    }

    // ...

}
```

如果你添加自己的`ElasticsearchTemplate`或者`TransportClient` `@Bean`，它将覆盖默认实例。
