{"id":11493,"date":"2022-01-25T14:46:17","date_gmt":"2022-01-25T20:46:17","guid":{"rendered":"https:\/\/threecloud.wpengine.com\/?p=11493"},"modified":"2023-05-01T19:40:20","modified_gmt":"2023-05-02T00:40:20","slug":"disabling-azure-app-configuration-spring-boot","status":"publish","type":"post","link":"https:\/\/3cloudsolutions.com\/resources\/disabling-azure-app-configuration-spring-boot\/","title":{"rendered":"Disabling Azure App Configuration &#8211; Spring Boot"},"content":{"rendered":"<h2>1. Overview<\/h2>\n<p>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 <strong>spring-cloud-azure-appconfiguration-config<\/strong> dependency is often used.<\/p>\n<p>One of the challenges when working with the Spring Cloud Azure App Configuration library is disabling it when developing locally.<\/p>\n<p>In this quick tutorial we&#8217;ll see how we can disable the library at application startup.<\/p>\n<h2>2. Example App Overview<\/h2>\n<p>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.<\/p>\n<h3>2.1 Dependency<\/h3>\n<p>Below is the dependency we are using to connect to the Azure App Configuration service.<\/p>\n<pre>&lt;dependency&gt;\r\n&lt;groupId&gt;com.microsoft.azure&lt;\/groupId&gt;\r\n&lt;artifactId&gt;spring-cloud-azure-appconfiguration&lt;\/artifactId&gt;\r\n&lt;version&gt;1.3.0&lt;\/version&gt;\r\n&lt;\/dependency&gt;<\/pre>\n<h3>2.2 Database Configuration<\/h3>\n<p>Here is the configuration class that will pull the database configurations from the Azure App configuration service and KeyVault on application startup.<\/p>\n<pre>@ConfigurationProperties(prefix = \"config\")\r\npublic class AppConfigValues {\r\n\r\nprivate string dbDriverClassName\r\nprivate string dbUrl\r\nprivate string dbUsername\r\nprivate string dbPassword\r\n}<\/pre>\n<h3>2.3 Data Source Configuration<\/h3>\n<p>We inject the previous configurations into our DataSourceConfig class, that will then be used to set up our data source bean.<\/p>\n<pre>@Configuration\r\npublic class DataSourceConfig {\r\n\r\nprivate final AppConfigValues config;\r\n\r\npublic DataSourceConfig(AppConfigValues config) {\r\nthis.config = config;\r\n}\r\n\r\n@Bean\r\npublic DataSource getDataSource() { ... }\r\n}<\/pre>\n<p>Now that we have an idea of how our application is configured to work with Azure App configuration, let&#8217;s take a look at how we can disable the library for local app development.<\/p>\n<h2>3. The Solution: Disabling Spring Cloud Azure App Configuration Dependency<\/h2>\n<p>We&#8217;ll go ahead and make the necessary changes to disable the Spring Cloud App Configuration library.<\/p>\n<h3>3.1 Bootstrap Properties<\/h3>\n<p>If you&#8217;re not familiar with the bootstrap.properties file, let&#8217;s briefly discuss what it&#8217;s used for.<\/p>\n<p>The bootstrap.yaml or bootstrap.properties file is used <strong>for configuring the bootstrap context<\/strong>. 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 <a href=\"https:\/\/cloud.spring.io\/spring-cloud-static\/spring-cloud.html#_the_bootstrap_application_context\">Spring Cloud Bootstrap Context documentation<\/a> for more information.<\/p>\n<p>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.<\/p>\n<p>Lucky for us, Microsoft has provided a property that allows us to do just that.<\/p>\n<p>Refer to the Microsoft documentation below.<\/p>\n<p><a href=\"https:\/\/docs.microsoft.com\/en-us\/java\/api\/overview\/azure\/spring-cloud-starter-appconfiguration-config-readme?view=azure-java-stable\">Spring Cloud for Azure<\/a><\/p>\n<p>Let&#8217;s go ahead and add the <code>spring.cloud.azure.appconfiguration<\/code> property to our <code>bootstrap.properties<\/code> file and set its value to false.<\/p>\n<p><code>spring.cloud.azure.appconfiguration.enabled=false<\/code><\/p>\n<h3>3.2 Disabling the Data Source config file<\/h3>\n<p>Since the Azure spring cloud configuration is disabled, our application will fail to start due to missing configurations.<\/p>\n<p>We can use spring profiles to disable the initialization of the data source bean.<\/p>\n<p>If you&#8217;re not familiar with Spring profiles, it allows us to bootstrap beans based on a number of profiles. We&#8217;ll be using them to activate the DataSourceConfig when we&#8217;re deployed to Azure and disable it when running locally. The changes below basically allow us to bootstrap the Data Source bean when the <strong>Azure profile<\/strong> is active.<\/p>\n<p>Add the below profile annotation to the data source class.<\/p>\n<pre>@Configuration\r\n@Profile(\"azure\")\r\npublic class DataSourceConfig {\r\n\r\nprivate final AppConfigValues config;\r\n\r\npublic DataSourceConfig(AppConfigValues config) {\r\nthis.config = config;\r\n}\r\n\r\n@Bean\r\npublic DataSource getDataSource() { ... }\r\n}<\/pre>\n<h3>3.3 Spring Profiles local properties<\/h3>\n<p>Next, we&#8217;ll add a properties file with the local database configurations. Using <code>application-&lt;env&gt;.properties<\/code> allows us to use configurations based on a particular active spring profile.<\/p>\n<p>Add the file <strong>application-local.properties<\/strong> to your resource folder with the below configurations. Here we&#8217;re using a local MySQL installation. Adjust this to the database you are using.<\/p>\n<pre>application-local.properties\r\nspring.datasource.url=jdbc:mysql:\/\/${MYSQL:localhost}:3306\/schemaname\r\nspring.datasource.username=username\r\nspring.datasource.password=password\r\nspring.datasource.driver-class-name=com.mysql.cj.jdbc. Driver<\/pre>\n<h3>3.4 Final Steps<\/h3>\n<p>Lastly Spring Boot needs to know which profile we want active. You can do it in a number of ways.<\/p>\n<p>Let&#8217;s take a look at a few of the options we have to do so.<\/p>\n<p>Property<br \/>\n<code>spring.profiles.active=local<\/code><\/p>\n<p>JVM System Parameter<br \/>\n<code>-Dspring.profiles.active=local<\/code><\/p>\n<p>Environment Variables<br \/>\n<code>export spring_profiles_active=local<\/code><\/p>\n<p>To learn more about spring profiles visit spring.io documentation on <a href=\"https:\/\/docs.spring.io\/spring-boot\/docs\/1.2.0.M1\/reference\/html\/boot-features-profiles.html\">Spring Profiles<\/a><\/p>\n<p>That&#8217;s it! We should be able to run our application now with the Azure Spring Cloud App Configuration library completely disabled.<\/p>\n<h2>4. Summary<\/h2>\n<p>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.<\/p>\n<p><strong>References<\/strong><\/p>\n<ol>\n<li><a href=\"https:\/\/docs.microsoft.com\/en-us\/java\/api\/overview\/azure\/spring-cloud-starter-appconfiguration-config-readme?view=azure-java-stable\">https:\/\/docs.microsoft.com\/en-us\/java\/api\/overview\/azure\/spring-cloud-starter-appconfiguration-config-readme?view=azure-java-stable<\/a><\/li>\n<li><a href=\"https:\/\/azure.microsoft.com\/en-us\/services\/mysql\/\">https:\/\/azure.microsoft.com\/en-us\/services\/mysql\/<\/a><\/li>\n<li><a href=\"https:\/\/docs.spring.io\/spring-boot\/docs\/1.2.0.M1\/reference\/html\/boot-features-profiles.html\">https:\/\/docs.spring.io\/spring-boot\/docs\/1.2.0.M1\/reference\/html\/boot-features-profiles.html<\/a><\/li>\n<li><a href=\"https:\/\/cloud.spring.io\/spring-cloud-static\/spring-cloud.html#_the_bootstrap_application_context\">https:\/\/cloud.spring.io\/spring-cloud-static\/spring-cloud.html#_the_bootstrap_application_context<\/a><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>1. Overview Azure App Configuration provides a service to centrally manage application settings and feature&mldr;<\/p>\n","protected":false},"author":68,"featured_media":11501,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"content-type":"","footnotes":""},"categories":[292,264],"tags":[],"class_list":["post-11493","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-app-innovation","category-enterprise-apps","topics-blog"],"acf":[],"_links":{"self":[{"href":"https:\/\/3cloudsolutions.com\/wp-json\/wp\/v2\/posts\/11493","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/3cloudsolutions.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/3cloudsolutions.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/3cloudsolutions.com\/wp-json\/wp\/v2\/users\/68"}],"replies":[{"embeddable":true,"href":"https:\/\/3cloudsolutions.com\/wp-json\/wp\/v2\/comments?post=11493"}],"version-history":[{"count":0,"href":"https:\/\/3cloudsolutions.com\/wp-json\/wp\/v2\/posts\/11493\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/3cloudsolutions.com\/wp-json\/wp\/v2\/media\/11501"}],"wp:attachment":[{"href":"https:\/\/3cloudsolutions.com\/wp-json\/wp\/v2\/media?parent=11493"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/3cloudsolutions.com\/wp-json\/wp\/v2\/categories?post=11493"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/3cloudsolutions.com\/wp-json\/wp\/v2\/tags?post=11493"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}