Skip to content

Files

Latest commit

 

History

History
200 lines (139 loc) · 7.11 KB

quickstart-java-spring-app.md

File metadata and controls

200 lines (139 loc) · 7.11 KB
title description services documentationcenter author editor ms.service ms.devlang ms.topic ms.date ms.custom ms.author
Quickstart to learn how to use Azure App Configuration
In this quickstart, create a Java Spring app with Azure App Configuration to centralize storage and management of application settings separate from your code.
azure-app-configuration
mrm9084
azure-app-configuration
java
quickstart
05/02/2022
devx-track-java, mode-api
mametcal

Quickstart: Create a Java Spring app with Azure App Configuration

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.

Prerequisites

Create an App Configuration store

[!INCLUDE azure-app-configuration-create]

  1. Select Configuration Explorer > + Create > Key-value to add the following key-value pairs:

    Key Value
    /application/config.message Hello

    Leave Label and Content Type empty for now.

  2. Select Apply.

Create a Spring Boot app

Use the Spring Initializr to create a new Spring Boot project.

  1. Browse to https://start.spring.io/.

  2. Specify the following options:

    • Generate a Maven project with Java.
    • Specify a Spring Boot version that's equal to or greater than 2.0.
    • Specify the Group and Artifact names for your application.
    • Add the Spring Web dependency.
  3. After you specify the previous options, select Generate Project. When prompted, download the project to a path on your local computer.

Connect to an App Configuration store

  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.

  2. Open the pom.xml file in a text editor, and add the Spring Cloud Azure Config starter to the list of <dependencies>:

    Spring Boot 2.6

    <dependency>
        <groupId>com.azure.spring</groupId>
        <artifactId>azure-spring-cloud-appconfiguration-config</artifactId>
        <version>2.6.0</version>
    </dependency>

    [!NOTE] If you need to support an older version of Spring Boot see our old library.

  3. Create a new Java file named MessageProperties.java in the package directory of your app. Add the following lines:

    package com.example.demo;
    
    import org.springframework.boot.context.properties.ConfigurationProperties;
    
    @ConfigurationProperties(prefix = "config")
    public class MessageProperties {
        private String message;
    
        public String getMessage() {
            return message;
        }
    
        public void setMessage(String message) {
            this.message = message;
        }
    }
  4. Create a new Java file named HelloController.java in the package directory of your app. Add the following lines:

    package com.example.demo;
    
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class HelloController {
        private final MessageProperties properties;
    
        public HelloController(MessageProperties properties) {
            this.properties = properties;
        }
    
        @GetMapping
        public String getMessage() {
            return "Message: " + properties.getMessage();
        }
    }
  5. Open the main application Java file, and add @EnableConfigurationProperties to enable this feature.

    import org.springframework.boot.context.properties.EnableConfigurationProperties;
    
    @SpringBootApplication
    @EnableConfigurationProperties(MessageProperties.class)
    public class DemoApplication {
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
        }
    }
  6. 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.

    package com.example.demo;
    
    import org.junit.jupiter.api.Test;
    import org.springframework.boot.test.context.SpringBootTest;
    
    @SpringBootTest(properties = "spring.cloud.azure.appconfiguration.enabled=false")
    class DemoApplicationTests {
    
        @Test
        void contextLoads() {
        }
    
    }
  7. Create a new file named bootstrap.properties under the resources directory of your app, and add the following line to the file.

    spring.cloud.azure.appconfiguration.stores[0].connection-string= ${APP_CONFIGURATION_CONNECTION_STRING}
    
  8. 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:

    setx APP_CONFIGURATION_CONNECTION_STRING "connection-string-of-your-app-configuration-store"

    If you use Windows PowerShell, run the following command:

    $Env:APP_CONFIGURATION_CONNECTION_STRING = "connection-string-of-your-app-configuration-store"
    

    If you use macOS or Linux, run the following command:

    export APP_CONFIGURATION_CONNECTION_STRING='connection-string-of-your-app-configuration-store'

Build and run the app locally

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

    mvn clean package
    mvn spring-boot:run
  2. After your application is running, use curl to test your application, for example:

    curl -X GET http://localhost:8080/

    You see the message that you entered in the App Configuration store.

Clean up resources

[!INCLUDE azure-app-configuration-cleanup]

Next steps

In this quickstart, you created a new App Configuration store and used it with a Java Spring app. For more information, see Spring on Azure. For further questions see the reference documentation, it has all of the details on how the Spring Cloud Azure App Configuration library works. To learn how to enable your Java Spring app to dynamically refresh configuration settings, continue to the next tutorial.

[!div class="nextstepaction"] Enable dynamic configuration