1. Overview
Azure App Configuration provides a service to centrally manage application settings and feature flags. When developing a Spring Boot application that utilizes Azure App Configuration Microsoft spring-cloud-azure-appconfiguration-config dependency is often used.
One of the challenges when working with the Spring Cloud Azure App Configuration library is disabling it when developing locally.
In this quick tutorial we’ll see how we can disable the library at application startup.
2. Example App Overview
In this particular example we are configured to connect to the Azure Database for MySQL servers. The database credentials and configurations are stored in the Azure App configuration and KeyVault services.
2.1 Dependency
Below is the dependency we are using to connect to the Azure App Configuration service.
<dependency> <groupId>com.microsoft.azure</groupId> <artifactId>spring-cloud-azure-appconfiguration</artifactId> <version>1.3.0</version> </dependency>
2.2 Database Configuration
Here is the configuration class that will pull the database configurations from the Azure App configuration service and KeyVault on application startup.
@ConfigurationProperties(prefix = "config") public class AppConfigValues { private string dbDriverClassName private string dbUrl private string dbUsername private string dbPassword }
2.3 Data Source Configuration
We inject the previous configurations into our DataSourceConfig class, that will then be used to set up our data source bean.
@Configuration public class DataSourceConfig { private final AppConfigValues config; public DataSourceConfig(AppConfigValues config) { this.config = config; } @Bean public DataSource getDataSource() { ... } }
Now that we have an idea of how our application is configured to work with Azure App configuration, let’s take a look at how we can disable the library for local app development.
3. The Solution: Disabling Spring Cloud Azure App Configuration Dependency
We’ll go ahead and make the necessary changes to disable the Spring Cloud App Configuration library.
3.1 Bootstrap Properties
If you’re not familiar with the bootstrap.properties file, let’s briefly discuss what it’s used for.
The bootstrap.yaml or bootstrap.properties file is used for configuring the bootstrap context. The bootstrap context is responsible for loading configuration properties from the external sources and for decrypting properties in the local external configuration files. Refer to the Spring Cloud Bootstrap Context documentation for more information.
Since we know external configurations are handled by the bootstrap context that gives us an idea of where we have an opportunity to disable the library initialization.
Lucky for us, Microsoft has provided a property that allows us to do just that.
Refer to the Microsoft documentation below.
Let’s go ahead and add the spring.cloud.azure.appconfiguration
property to our bootstrap.properties
file and set its value to false.
spring.cloud.azure.appconfiguration.enabled=false
3.2 Disabling the Data Source config file
Since the Azure spring cloud configuration is disabled, our application will fail to start due to missing configurations.
We can use spring profiles to disable the initialization of the data source bean.
If you’re not familiar with Spring profiles, it allows us to bootstrap beans based on a number of profiles. We’ll be using them to activate the DataSourceConfig when we’re deployed to Azure and disable it when running locally. The changes below basically allow us to bootstrap the Data Source bean when the Azure profile is active.
Add the below profile annotation to the data source class.
@Configuration @Profile("azure") public class DataSourceConfig { private final AppConfigValues config; public DataSourceConfig(AppConfigValues config) { this.config = config; } @Bean public DataSource getDataSource() { ... } }
3.3 Spring Profiles local properties
Next, we’ll add a properties file with the local database configurations. Using application-<env>.properties
allows us to use configurations based on a particular active spring profile.
Add the file application-local.properties to your resource folder with the below configurations. Here we’re using a local MySQL installation. Adjust this to the database you are using.
application-local.properties spring.datasource.url=jdbc:mysql://${MYSQL:localhost}:3306/schemaname spring.datasource.username=username spring.datasource.password=password spring.datasource.driver-class-name=com.mysql.cj.jdbc. Driver
3.4 Final Steps
Lastly Spring Boot needs to know which profile we want active. You can do it in a number of ways.
Let’s take a look at a few of the options we have to do so.
Property
spring.profiles.active=local
JVM System Parameter
-Dspring.profiles.active=local
Environment Variables
export spring_profiles_active=local
To learn more about spring profiles visit spring.io documentation on Spring Profiles
That’s it! We should be able to run our application now with the Azure Spring Cloud App Configuration library completely disabled.
4. Summary
In this article, we explored how we can disable use of the Azure App Configuration library for local development. This involves using spring profiles to disable initialization of the app config library and setting the spring cloud app configuration property to false in the bootstrap.properties file.
References
- https://docs.microsoft.com/en-us/java/api/overview/azure/spring-cloud-starter-appconfiguration-config-readme?view=azure-java-stable
- https://azure.microsoft.com/en-us/services/mysql/
- https://docs.spring.io/spring-boot/docs/1.2.0.M1/reference/html/boot-features-profiles.html
- https://cloud.spring.io/spring-cloud-static/spring-cloud.html#_the_bootstrap_application_context