Skip to content

Commit 07a1295

Browse files
authored
Merge pull request #226649 from hui1110/add_passwordless_section_for_spring_data_cosmos
Update Spring Data Commos DB and App Configuration docs
2 parents 7e97706 + 17e3e9e commit 07a1295

File tree

2 files changed

+210
-176
lines changed

2 files changed

+210
-176
lines changed

articles/azure-app-configuration/quickstart-java-spring-app.md

Lines changed: 99 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,16 @@
22
title: Quickstart to learn how to use Azure App Configuration
33
description: In this quickstart, create a Java Spring app with Azure App Configuration to centralize storage and management of application settings separate from your code.
44
services: azure-app-configuration
5-
documentationcenter: ''
65
author: mrm9084
7-
editor: ''
86
ms.service: azure-app-configuration
97
ms.devlang: java
108
ms.topic: quickstart
11-
ms.date: 05/02/2022
9+
ms.date: 02/22/2023
1210
ms.custom: devx-track-java, mode-api
1311
ms.author: mametcal
1412
#Customer intent: As a Java Spring developer, I want to manage all my app settings in one place.
1513
---
14+
1615
# Quickstart: Create a Java Spring app with Azure App Configuration
1716

1817
In this quickstart, you incorporate Azure App Configuration into a Java Spring app to centralize storage and management of application settings separate from your code.
@@ -22,171 +21,153 @@ In this quickstart, you incorporate Azure App Configuration into a Java Spring a
2221
- Azure subscription - [create one for free](https://azure.microsoft.com/free/)
2322
- A supported [Java Development Kit (JDK)](/java/azure/jdk) with version 11.
2423
- [Apache Maven](https://maven.apache.org/download.cgi) version 3.0 or above.
24+
- A Spring Boot application. If you don't have one, create a Maven project with the [Spring Initializr](https://start.spring.io/). Be sure to select **Maven Project** and, under **Dependencies**, add the **Spring Web** dependency, and then select Java version 8 or higher.
2525

2626
## Create an App Configuration store
2727

2828
[!INCLUDE [azure-app-configuration-create](../../includes/azure-app-configuration-create.md)]
2929

30-
7. Select **Configuration Explorer** > **+ Create** > **Key-value** to add the following key-value pairs:
31-
32-
| Key | Value |
33-
|---|---|
34-
| /application/config.message | Hello |
35-
36-
Leave **Label** and **Content Type** empty for now.
37-
38-
8. Select **Apply**.
39-
40-
## Create a Spring Boot app
30+
9. Select **Configuration Explorer** > **+ Create** > **Key-value** to add the following key-value pairs:
4131

42-
To create a new Spring Boot project:
32+
| Key | Value |
33+
|---|---|
34+
| /application/config.message | Hello |
4335

44-
1. Browse to the [Spring Initializr](https://start.spring.io).
36+
Leave **Label** and **Content Type** empty for now.
4537

46-
1. Specify the following options:
47-
48-
- Generate a **Maven** project with **Java**.
49-
- Specify a **Spring Boot** version that's equal to or greater than 2.0.
50-
- Specify the **Group** and **Artifact** names for your application.
51-
- Add the **Spring Web** dependency.
52-
53-
1. After you specify the previous options, select **Generate Project**. When prompted, download the project to a path on your local computer.
38+
10. Select **Apply**.
5439

5540
## Connect to an App Configuration store
5641

57-
1. After you extract the files on your local system, your simple Spring Boot application is ready for editing. Locate the *pom.xml* file in the root directory of your app.
58-
59-
1. Open the *pom.xml* file in a text editor, and add the Spring Cloud Azure Config starter to the list of `<dependencies>`:
60-
61-
**Spring Boot 2.6**
42+
Now that you have an App Configuration store, you can use the Spring Cloud Azure Config starter to have your application communicate with the App Configuration store that you create.
6243

63-
```xml
64-
<dependency>
65-
<groupId>com.azure.spring</groupId>
66-
<artifactId>azure-spring-cloud-appconfiguration-config</artifactId>
67-
<version>2.6.0</version>
68-
</dependency>
69-
```
44+
To install the Spring Cloud Azure Config starter module, add the following dependency to your *pom.xml* file:
7045

71-
> [!NOTE]
72-
> If you need to support an older version of Spring Boot see our [old library](https://github.com/Azure/azure-sdk-for-java/blob/spring-cloud-starter-azure-appconfiguration-config_1.2.9/sdk/appconfiguration/spring-cloud-starter-azure-appconfiguration-config/README.md).
46+
```xml
47+
<dependency>
48+
<groupId>com.azure.spring</groupId>
49+
<artifactId>azure-spring-cloud-appconfiguration-config</artifactId>
50+
<version>2.11.0</version>
51+
</dependency>
52+
```
7353

74-
1. Create a new Java file named *MessageProperties.java* in the package directory of your app. Add the following lines:
54+
> [!NOTE]
55+
> If you need to support an older version of Spring Boot, see our [old library](https://github.com/Azure/azure-sdk-for-java/blob/spring-cloud-starter-azure-appconfiguration-config_1.2.9/sdk/appconfiguration/spring-cloud-starter-azure-appconfiguration-config/README.md).
7556
76-
```java
77-
package com.example.demo;
57+
### Code the application
7858

79-
import org.springframework.boot.context.properties.ConfigurationProperties;
59+
To use the Spring Cloud Azure Config starter to have your application communicate with the App Configuration store that you create, configure the application by using the following steps.
8060

81-
@ConfigurationProperties(prefix = "config")
82-
public class MessageProperties {
83-
private String message;
61+
1. Create a new Java file named *MessageProperties.java*, and add the following lines:
8462

85-
public String getMessage() {
86-
return message;
87-
}
63+
```java
64+
import org.springframework.boot.context.properties.ConfigurationProperties;
8865

89-
public void setMessage(String message) {
90-
this.message = message;
91-
}
92-
}
93-
```
66+
@ConfigurationProperties(prefix = "config")
67+
public class MessageProperties {
68+
private String message;
9469

95-
1. Create a new Java file named *HelloController.java* in the package directory of your app. Add the following lines:
70+
public String getMessage() {
71+
return message;
72+
}
9673

97-
```java
98-
package com.example.demo;
74+
public void setMessage(String message) {
75+
this.message = message;
76+
}
77+
}
78+
```
9979

100-
import org.springframework.web.bind.annotation.GetMapping;
101-
import org.springframework.web.bind.annotation.RestController;
80+
1. Create a new Java file named *HelloController.java*, and add the following lines:
10281

103-
@RestController
104-
public class HelloController {
105-
private final MessageProperties properties;
82+
```java
83+
import org.springframework.web.bind.annotation.GetMapping;
84+
import org.springframework.web.bind.annotation.RestController;
10685

107-
public HelloController(MessageProperties properties) {
108-
this.properties = properties;
109-
}
86+
@RestController
87+
public class HelloController {
88+
private final MessageProperties properties;
11089

111-
@GetMapping
112-
public String getMessage() {
113-
return "Message: " + properties.getMessage();
114-
}
115-
}
116-
```
90+
public HelloController(MessageProperties properties) {
91+
this.properties = properties;
92+
}
11793

118-
1. Open the main application Java file, and add `@EnableConfigurationProperties` to enable this feature.
94+
@GetMapping
95+
public String getMessage() {
96+
return "Message: " + properties.getMessage();
97+
}
98+
}
99+
```
119100

120-
```java
121-
import org.springframework.boot.context.properties.EnableConfigurationProperties;
101+
1. In the main application Java file, add `@EnableConfigurationProperties` to enable the *MessageProperties.java* configuration properties class to take effect and register it with the Spring container.
122102

123-
@SpringBootApplication
124-
@EnableConfigurationProperties(MessageProperties.class)
125-
public class DemoApplication {
126-
public static void main(String[] args) {
127-
SpringApplication.run(DemoApplication.class, args);
128-
}
129-
}
130-
```
103+
```java
104+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
131105

132-
1. Open the auto-generated unit test and update to disable Azure App Configuration, or it will try to load from the service when runnings unit tests.
106+
@SpringBootApplication
107+
@EnableConfigurationProperties(MessageProperties.class)
108+
public class DemoApplication {
109+
public static void main(String[] args) {
110+
SpringApplication.run(DemoApplication.class, args);
111+
}
112+
}
113+
```
133114

134-
```java
135-
package com.example.demo;
115+
1. Open the auto-generated unit test and update to disable Azure App Configuration, or it will try to load from the service when running unit tests.
136116

137-
import org.junit.jupiter.api.Test;
138-
import org.springframework.boot.test.context.SpringBootTest;
117+
```java
118+
import org.junit.jupiter.api.Test;
119+
import org.springframework.boot.test.context.SpringBootTest;
139120

140-
@SpringBootTest(properties = "spring.cloud.azure.appconfiguration.enabled=false")
141-
class DemoApplicationTests {
121+
@SpringBootTest(properties = "spring.cloud.azure.appconfiguration.enabled=false")
122+
class DemoApplicationTests {
142123

143-
@Test
144-
void contextLoads() {
145-
}
124+
@Test
125+
void contextLoads() {
126+
}
146127

147-
}
148-
```
128+
}
129+
```
149130

150-
1. Create a new file named `bootstrap.properties` under the resources directory of your app, and add the following line to the file.
131+
1. Create a new file named *bootstrap.properties* under the resources directory of your app, and add the following line to the file.
151132

152-
```CLI
153-
spring.cloud.azure.appconfiguration.stores[0].connection-string= ${APP_CONFIGURATION_CONNECTION_STRING}
154-
```
133+
```properties
134+
spring.cloud.azure.appconfiguration.stores[0].connection-string= ${APP_CONFIGURATION_CONNECTION_STRING}
135+
```
155136

156137
1. Set an environment variable named **APP_CONFIGURATION_CONNECTION_STRING**, and set it to the access key to your App Configuration store. At the command line, run the following command and restart the command prompt to allow the change to take effect:
157138

158-
```cmd
159-
setx APP_CONFIGURATION_CONNECTION_STRING "connection-string-of-your-app-configuration-store"
160-
```
139+
```cmd
140+
setx APP_CONFIGURATION_CONNECTION_STRING "connection-string-of-your-app-configuration-store"
141+
```
161142

162-
If you use Windows PowerShell, run the following command:
143+
If you use Windows PowerShell, run the following command:
163144

164-
```azurepowershell
165-
$Env:APP_CONFIGURATION_CONNECTION_STRING = "connection-string-of-your-app-configuration-store"
166-
```
145+
```azurepowershell
146+
$Env:APP_CONFIGURATION_CONNECTION_STRING = "connection-string-of-your-app-configuration-store"
147+
```
167148

168-
If you use macOS or Linux, run the following command:
149+
If you use macOS or Linux, run the following command:
169150

170-
```cmd
171-
export APP_CONFIGURATION_CONNECTION_STRING='connection-string-of-your-app-configuration-store'
172-
```
151+
```cmd
152+
export APP_CONFIGURATION_CONNECTION_STRING='connection-string-of-your-app-configuration-store'
153+
```
173154

174-
## Build and run the app locally
155+
### Build and run the app locally
175156

176157
1. Open command prompt to the root directory and run the following commands to build your Spring Boot application with Maven and run it.
177158

178-
```cmd
179-
mvn clean package
180-
mvn spring-boot:run
181-
```
159+
```cmd
160+
mvn clean package
161+
mvn spring-boot:run
162+
```
182163

183-
2. After your application is running, use *curl* to test your application, for example:
164+
1. After your application is running, use *curl* to test your application, for example:
184165

185-
```cmd
186-
curl -X GET http://localhost:8080/
187-
```
166+
```cmd
167+
curl -X GET http://localhost:8080/
168+
```
188169

189-
You see the message that you entered in the App Configuration store.
170+
You see the message that you entered in the App Configuration store.
190171

191172
## Clean up resources
192173

0 commit comments

Comments
 (0)