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

Add a key-value

Add the following key-value to the App Configuration store and leave Label and Content Type with their default values. For more information about how to add key-values to a store using the Azure portal or the CLI, go to Create a key-value.

Key Value
/application/config.message Hello

Connect to an App Configuration store

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.

To install the Spring Cloud Azure Config starter module, add the following dependency to your pom.xml file:

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

<dependencyManagement>
    <dependencies>
        <dependency>
        <groupId>com.azure.spring</groupId>
        <artifactId>spring-cloud-azure-dependencies</artifactId>
        <version>5.8.0</version>
        <type>pom</type>
        <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

Code the application

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.

  1. Create a new Java file named MyProperties.java, and add the following lines:

    import org.springframework.boot.context.properties.ConfigurationProperties;
    
    @ConfigurationProperties(prefix = "config")
    public class MyProperties {
        private String message;
    
        public String getMessage() {
            return message;
        }
    
        public void setMessage(String message) {
            this.message = message;
        }
    }
    
  2. Create a new Java file named HelloController.java, and add the following lines:

    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class HelloController {
        private final MyProperties properties;
    
        public HelloController(MyProperties properties) {
            this.properties = properties;
        }
    
        @GetMapping
        public String getMessage() {
            return "Message: " + properties.getMessage();
        }
    }
    
  3. In the main application Java file, add @EnableConfigurationProperties to enable the MyProperties.java configuration properties class to take effect and register it with the Spring container.

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

    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() {
        }
    
    }
    
  5. 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}
    
  6. 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

If you don't want to continue using the resources created in this article, delete the resource group you created here to avoid charges.

Important

Deleting a resource group is irreversible. The resource group and all the resources in it are permanently deleted. Ensure that you don't accidentally delete the wrong resource group or resources. If you created the resources for this article inside a resource group that contains other resources you want to keep, delete each resource individually from its respective pane instead of deleting the resource group.

  1. Sign in to the Azure portal, and select Resource groups.
  2. In the Filter by name box, enter the name of your resource group.
  3. In the result list, select the resource group name to see an overview.
  4. Select Delete resource group.
  5. You're asked to confirm the deletion of the resource group. Enter the name of your resource group to confirm, and select Delete.

After a few moments, the resource group and all its resources are deleted.

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.