Skip to content

Commit 221a603

Browse files
committed
update comments
1 parent 39c255b commit 221a603

File tree

1 file changed

+15
-91
lines changed

1 file changed

+15
-91
lines changed

articles/cosmos-db/nosql/quickstart-java-spring-data.md

Lines changed: 15 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,9 @@ git clone https://github.com/Azure-Samples/azure-spring-boot-samples.git
9191

9292
This step is optional. If you're interested in learning how the database resources are created in the code, you can review the following snippets. Otherwise, you can skip ahead to [Run the app](#run-the-app).
9393

94-
# [Passwordless (Recommended)](#tab/passwordless)
94+
## [Passwordless (Recommended)](#tab/passwordless)
9595

96-
[!INCLUDE [cosmos-nosql-create-assign-roles](../../../includes/passwordless/cosmos-nosql/cosmos-nosql-create-assign-roles.md)]
96+
[!INCLUDE [cosmos-nosql-create-assign-roles](../../../includes/passwordless/cosmos-nosql/cosmos-nosql-create-assign-roles.md)]
9797

9898
### Application configuration file
9999

@@ -121,114 +121,38 @@ After creating the Azure Cosmos DB account, database and container, Spring Boot/
121121

122122
The sample code has already been added, you don't need to add any code.
123123

124-
# [Password](#tab/password)
124+
## [Password](#tab/password)
125125

126126
### Application configuration file
127127

128128
Here we showcase how Spring Boot and Spring Data enhance user experience - the process of establishing an Azure Cosmos DB client and connecting to Azure Cosmos DB resources is now config rather than code. At application startup Spring Boot handles all of this boilerplate using the settings in **application.yml**:
129129

130130
```yaml
131-
cosmos:
132-
uri: ${ACCOUNT_HOST}
133-
key: ${ACCOUNT_KEY}
134-
secondaryKey: ${SECONDARY_ACCOUNT_KEY}
135-
queryMetricsEnabled: true # Populate query metrics
136-
dynamic:
137-
collection:
138-
name: spel-property-collection
131+
spring:
132+
cloud:
133+
azure:
134+
cosmos:
135+
key: ${AZURE_COSMOS_KEY}
136+
endpoint: ${AZURE_COSMOS_ENDPOINT}
137+
database: ${COSMOS_DATABASE}
139138
```
140139

141140
Once you create an Azure Cosmos DB account, database, and container, just fill-in-the-blanks in the config file and Spring Boot/Spring Data will automatically do the following: (1) create an underlying Java SDK `CosmosClient` instance with the URI and key, and (2) connect to the database and container. You're all set - **no more resource management code!**
142141

143142
### Java source
144143

145-
The Spring Data value-add also comes from its simple, clean, standardized and platform-independent interface for operating on datastores. Building on the Spring Data GitHub sample linked above, below are CRUD and query samples for manipulating Azure Cosmos DB documents with Spring Data Azure Cosmos DB.
146-
147-
1. Create a new Java file named *MessageProperties.java* as an entity stored in Azure Cosmos DB. The following code ignores the `getters` and `setters` methods.
148-
149-
```java
150-
import org.springframework.boot.context.properties.ConfigurationProperties;
151-
152-
@ConfigurationProperties(prefix = "cosmos")
153-
public class CosmosProperties {
154-
155-
private String uri;
156-
157-
private String key;
158-
159-
private String secondaryKey;
160-
161-
private boolean queryMetricsEnabled;
162-
}
163-
```
164-
165-
1. Create a new Java file named *AzureCosmosDbConfiguration.java*. It is used to create a client to connect to Cosmos DB.
166-
167-
```java
168-
import com.azure.cosmos.CosmosClientBuilder;
169-
import com.azure.cosmos.DirectConnectionConfig;
170-
import com.azure.spring.data.cosmos.config.AbstractCosmosConfiguration;
171-
import com.azure.spring.data.cosmos.config.CosmosConfig;
172-
import com.azure.spring.data.cosmos.core.ResponseDiagnostics;
173-
import com.azure.spring.data.cosmos.core.ResponseDiagnosticsProcessor;
174-
import com.azure.spring.data.cosmos.repository.config.EnableReactiveCosmosRepositories;
175-
import org.slf4j.Logger;
176-
import org.slf4j.LoggerFactory;
177-
import org.springframework.beans.factory.annotation.Autowired;
178-
import org.springframework.boot.context.properties.EnableConfigurationProperties;
179-
import org.springframework.context.annotation.Bean;
180-
import org.springframework.context.annotation.Configuration;
181-
import org.springframework.context.annotation.PropertySource;
182-
import org.springframework.lang.Nullable;
183-
184-
@Configuration
185-
@EnableConfigurationProperties(CosmosProperties.class)
186-
@EnableReactiveCosmosRepositories
187-
@PropertySource("classpath:application.yml")
188-
public class AzureCosmosDbConfiguration extends AbstractCosmosConfiguration {
189-
private static final Logger logger = LoggerFactory.getLogger(AzureCosmosDbConfiguration.class);
190-
191-
@Autowired
192-
private CosmosProperties properties;
193-
194-
@Bean
195-
public CosmosClientBuilder cosmosClientBuilder() {
196-
DirectConnectionConfig directConnectionConfig = DirectConnectionConfig.getDefaultConfig();
197-
return new CosmosClientBuilder()
198-
.endpoint(properties.getUri())
199-
.key(properties.getKey())
200-
.directMode(directConnectionConfig);
201-
}
202-
203-
@Bean
204-
public CosmosConfig cosmosConfig() {
205-
return CosmosConfig.builder()
206-
.responseDiagnosticsProcessor(new ResponseDiagnosticsProcessorImplementation())
207-
.enableQueryMetrics(properties.isQueryMetricsEnabled())
208-
.build();
209-
}
210-
211-
@Override
212-
protected String getDatabaseName() {
213-
return "testdb";
214-
}
215-
216-
private static class ResponseDiagnosticsProcessorImplementation implements ResponseDiagnosticsProcessor {
217-
218-
@Override
219-
public void processResponseDiagnostics(@Nullable ResponseDiagnostics responseDiagnostics) {
220-
logger.info("Response Diagnostics {}", responseDiagnostics);
221-
}
222-
}
223-
}
224-
```
144+
The sample code has already been added, you don't need to add any code.
225145

226146
---
227147

148+
228149
## Run the app
229150

230151
Now go back to the Azure portal to get your connection string information and launch the app with your endpoint information. This enables your app to communicate with your hosted database.
231152

153+
>[!TIP]
154+
> Before starting the following sections, replace the variables in *application.yml* with real values. If you use Passwordless authentication, use Azure CLI, Visual Studio Code, PowerShell, or other methods to complete the authentication.
155+
232156
1. In the git terminal window, `cd` to the sample code folder.
233157

234158
```bash

0 commit comments

Comments
 (0)