CosmosTemplate is the central class of Spring Data Azure Cosmos DB (the azure-spring-data-cosmos library). It wraps the Azure Cosmos DB Java SDK and gives you direct, programmatic access to containers: inserting, upserting, reading, deleting, counting and running custom SQL queries. Repositories such as CosmosRepository are built on top of it, so you reach for CosmosTemplate when a repository method cannot express what you need.
The class lives in com.azure.spring.data.cosmos.core, not in a org.springframework.data package. For non-blocking code there is a twin, ReactiveCosmosTemplate.
Spring Data Cosmos has two layers. The repository layer (CosmosRepository<T, ID>) offers derived queries such as findByLastName. Underneath, each repository call is delegated to a CosmosTemplate, which talks to the Cosmos DB SQL (NoSQL) API through the Azure SDK.
| Need | Use |
|---|---|
| CRUD, simple derived queries, paging | CosmosRepository |
| Hand-written SQL with parameters, projections | CosmosTemplate.runQuery |
| Choose the container name at runtime | CosmosTemplate methods that take a container name |
| Upsert or partition-key-specific delete | CosmosTemplate |
| Reactive, non-blocking access | ReactiveCosmosTemplate |
You can use both in the same application. Spring Boot auto-configures the template when the Cosmos starter and connection properties are present.
Add the Spring Cloud Azure starter and configure the account in application.properties:
Import the Spring Cloud Azure BOM so versions are aligned with your Spring Boot release. Prefer Microsoft Entra ID (managed identity) over account keys in production where your deployment allows it. The starter then exposes a CosmosTemplate bean you can inject.
Entities declare their container and partition key with annotations. Choosing the partition key well matters more for cost and performance than anything else in Cosmos DB.
The id must be a String. Together, the partition key value and the id identify one item.
Inject the template and call it directly. The service below covers the operations you use most.
insert fails if the item already exists, while upsert creates or replaces it. runQuery takes a SqlQuerySpec, so values are sent as parameters and not concatenated into the query string, which avoids query injection. The last two type arguments are the entity class that defines the container and the type of the returned items.
A query that does not filter on the partition key becomes a cross-partition query, which costs more request units (RU) and has higher latency. Include the partition key in the WHERE clause whenever you can. For large result sets use the paging overloads (for example findAll(Pageable, Class)) with a CosmosPageRequest, which carries the continuation token instead of an offset.
Some claims about this class are wrong and worth correcting:
Use the template when you need custom SQL, a container chosen at runtime, upserts or fine control over partition keys. For routine CRUD and derived queries, a repository is shorter and easier to test.
Yes. It is a singleton Spring bean that delegates to a shared CosmosAsyncClient, which is designed to be reused for the lifetime of the application.
ReactiveCosmosTemplate returns Mono and Flux types and suits WebFlux applications. Its operations mirror the blocking template.
Most often the query is cross-partition or lacks a supporting index policy. Check the request charge in the response diagnostics and add the partition key to the filter.
CosmosTemplate is the low-level entry point of Spring Data Azure Cosmos DB. Repositories use it internally, and you use it directly for parameterised SQL queries, upserts, partition-aware deletes and runtime container selection. It works with the NoSQL API only and does not add multi-item transactions, so design around the partition key.