How to read app config values from Java SpringBoot app

Kikrigen 0 Reputation points
2023-02-03T08:43:57.93+00:00

Hello All,

I have a Springboot app that is trying to connect and read values from an app config resource.

Some keys in the Azure app config are in this format

/application/config.datasource.jdbc-url

/application/config.datasource.password

/application/config.datasource.username

I have a config Java class with prefix ("config"), but I don't know what member variables I should have in order to access "datasource.jdbc-url"

If it was just /application/config.username

then I could just use the below

String username;

but for some of the configs that include multiple dots and some dashes (which Java identifiers can't have), how can I read the values?

Thank you!

Azure App Configuration
Azure App Configuration
An Azure service that provides hosted, universal storage for Azure app configurations.
208 questions
Azure Spring Apps
Azure Spring Apps
An Azure platform as a service for running Spring Boot applications at cloud scale. Previously known as Azure Spring Cloud.
109 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. MughundhanRaveendran-MSFT 12,421 Reputation points
    2023-02-18T06:18:59.51+00:00

    @Kikrigen

    You can use the Spring Cloud Azure App Configuration library to read the values from the Azure App Configuration resource. The library allows you to use the @Value annotation to inject the values from the App Configuration resource into your application.

    For example, you can use the following code to inject the value of the "datasource.jdbc-url" key into a member variable in your config class:

    @Value("${config.datasource.jdbc-url}") private String jdbcUrl;

    The library will automatically replace the "config" prefix with the actual key prefix in the App Configuration resource.

    You can also use the @ConfigurationProperties annotation to map the entire key-value store to a Java object.

    For example, you can create a DataSourceConfig class with member variables for jdbcUrl, username, and password, and use the following code to map the entire key-value store to the DataSourceConfig object:

    @ConfigurationProperties("config.datasource") public class DataSourceConfig { private String jdbcUrl; private String username; private String password; // getters and setters }

    And then you can use the DataSourceConfig object in your application to access the values of the keys.

    You can also use the Azure Key Vault to store the sensitive information like passwords and use the Azure App Configuration to store the references to the Key Vault secrets.

    You can use the following code to inject the value of the "datasource.password" key from the Key Vault into a member variable in your config class:

    @Value("@Microsoft.KeyVault(SecretUri=config.datasource.password)") private String password;

    The library will automatically replace the "config" prefix with the actual key prefix in the App Configuration resource and retrieve the value from the Key Vault.

    You can also use the @ConfigurationProperties annotation to map the entire key-value store to a Java object and use the Key Vault references in the object.

    0 comments No comments