# 11.2. Hazelcast

如果 Hazelcast 位于类路径上并且找到合适的配置，Spring Boot 会自动配置一个`HazelcastInstance`，您可以将其注入到应用程序中。

Spring Boot 首先尝试通过检查以下配置选项来创建客户端：

* `com.hazelcast.client.config.ClientConfig`的存在。
* 由`spring.hazelcast.config`属性定义的配置文件。
* `hazelcast.client.config`系统属性的存在。
* 位于工作目录或类路径的根目录中的`hazelcast-client.xml`。
* `hazelcast-client.yaml`（或`hazelcast-client.yml`）位于工作目录或类路径的根目录中。

如果无法创建客户端，Spring Boot 会尝试配置嵌入式服务器。如果您定义了一个`com.hazelcast.config.Config`bean，Spring Boot 就会使用它。如果您的配置定义了实例名称，Spring Boot 会尝试查找现有实例而不是创建新实例。

您还可以通过配置指定要使用的 Hazelcast 配置文件，如以下示例所示：

```
spring.hazelcast.config=classpath:config/my-hazelcast.xml
```

否则，Spring Boot 会尝试从默认位置查找 Hazelcast 配置：`hazelcast.xml`在工作目录中或类路径的根目录中，或者在相同位置中的 YAML 对应项中。我们还检查系统属性`hazelcast.config`是否已设置。有关更多详细信息，请参阅[Hazelcast 文档。](https://docs.hazelcast.org/docs/latest/manual/html-single/)

> 默认情况下，支持`Hazelcast`组件上的 `@SpringAware`。可以通过声明 `@Order` 大于零的`HazelcastConfigCustomizer`bean 来覆盖`ManagementContext`。
>
> Spring Boot 还[对 Hazelcast 提供显式缓存支持](https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#io.caching.provider.hazelcast)。如果启用缓存，则`HazelcastInstance`会自动包装在`CacheManager`实现中。


---

# 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://doc.shiker.tech/spring-boot-can-kao-wen-dang/11.-io/11.2.-hazelcast.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.
