<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Muhammad Zaid</title>
    <description>The latest articles on DEV Community by Muhammad Zaid (@mhdzaid).</description>
    <link>https://dev.to/mhdzaid</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F329160%2F23862272-6756-4975-afb2-92806b6ed805.png</url>
      <title>DEV Community: Muhammad Zaid</title>
      <link>https://dev.to/mhdzaid</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mhdzaid"/>
    <language>en</language>
    <item>
      <title>How to configure Profiles in Spring Boot</title>
      <dc:creator>Muhammad Zaid</dc:creator>
      <pubDate>Tue, 08 Sep 2020 13:11:15 +0000</pubDate>
      <link>https://dev.to/mhdzaid/how-to-configure-profiles-in-spring-boot-16jo</link>
      <guid>https://dev.to/mhdzaid/how-to-configure-profiles-in-spring-boot-16jo</guid>
      <description>&lt;ul&gt;
&lt;li&gt;&lt;p&gt;First step is to create a project in &lt;a href="https://start.spring.io/"&gt;Spring Initializer&lt;/a&gt; using the default values as shown in image. Or if you're integrating in your current project, you can go to the next step.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Q_wNtDVf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/i8kythluqjsk73azw3hk.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Q_wNtDVf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/i8kythluqjsk73azw3hk.PNG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create &lt;code&gt;application.yaml&lt;/code&gt; file in your spring-boot project at location &lt;strong&gt;&lt;em&gt;src/main/resources&lt;/em&gt;&lt;/strong&gt; folder.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Now create a &lt;code&gt;.yaml&lt;/code&gt; file for each profile we want to configure. For &lt;code&gt;dev&lt;/code&gt; I'm naming my file &lt;code&gt;application-dev.yaml&lt;/code&gt;, for &lt;code&gt;staging&lt;/code&gt; &lt;code&gt;application-staging.yaml&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Now in your &lt;code&gt;pom.xml&lt;/code&gt; file, copy the following lines&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;profiles&amp;gt;
        &amp;lt;profile&amp;gt;
            &amp;lt;id&amp;gt;dev&amp;lt;/id&amp;gt;
            &amp;lt;properties&amp;gt;
                &amp;lt;activatedProperties&amp;gt;dev&amp;lt;/activatedProperties&amp;gt;
            &amp;lt;/properties&amp;gt;
            &amp;lt;activation&amp;gt;
                &amp;lt;activeByDefault&amp;gt;true&amp;lt;/activeByDefault&amp;gt;
            &amp;lt;/activation&amp;gt;
        &amp;lt;/profile&amp;gt;
        &amp;lt;profile&amp;gt;
            &amp;lt;id&amp;gt;staging&amp;lt;/id&amp;gt;
            &amp;lt;properties&amp;gt;
                &amp;lt;activatedProperties&amp;gt;staging&amp;lt;/activatedProperties&amp;gt;
            &amp;lt;/properties&amp;gt;
        &amp;lt;/profile&amp;gt;
&amp;lt;/profiles&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Over here we're defining our two profiles &lt;code&gt;dev&lt;/code&gt; and &lt;code&gt;staging&lt;/code&gt;. The line &lt;code&gt;&amp;lt;activeByDefault&amp;gt;true&amp;lt;/activeByDefault&amp;gt;&lt;/code&gt; means that the profile &lt;code&gt;dev&lt;/code&gt; will be active if no profile has been selected while running the spring boot project.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Now in the &lt;code&gt;application.yaml&lt;/code&gt; file, put the following lines
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;spring:
  profiles:
    active: @activatedProperties@
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This would be reading the &lt;code&gt;&amp;lt;activatedProperties&amp;gt;&lt;/code&gt; tag in your &lt;code&gt;pom.xml&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In &lt;code&gt;application-dev.yaml&lt;/code&gt; copy the following lines
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;spring:
  profiles: dev
server:
  port: 8093
application:
  current-profile: dev
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;In &lt;code&gt;application-staging.yaml&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;spring:
  profiles: staging
server:
  port: 8094
application:
  current-profile: staging
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Notice that the &lt;code&gt;port&lt;/code&gt; property in &lt;code&gt;application-dev.yaml&lt;/code&gt; and &lt;code&gt;application-staging.yaml&lt;/code&gt; is different.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Now, to run the application using &lt;code&gt;dev&lt;/code&gt; profile, run the command&lt;br&gt;
&lt;code&gt;mvn spring-boot:run -Dspring-boot.run.profiles=dev&lt;/code&gt;&lt;br&gt;
and for staging&lt;br&gt;
&lt;code&gt;mvn spring-boot:run -Dspring-boot.run.profiles=staging&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;For &lt;code&gt;dev&lt;/code&gt;, application will run on port &lt;code&gt;8093&lt;/code&gt; and for &lt;code&gt;staging&lt;/code&gt;, on &lt;code&gt;8094&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The advantage of using profiles is the we can configure different properties for different environments. Another advantage would be to use different variables in different environments. To show this we can create a class &lt;code&gt;ActiveProfile.java&lt;/code&gt; in the application.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Copy the following lines&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.springframework.beans.factory.annotation.Value&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.springframework.context.event.ContextRefreshedEvent&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.springframework.context.event.EventListener&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.springframework.stereotype.Component&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="nd"&gt;@Component&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ActiveProfile&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="nd"&gt;@Value&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"${application.current-profile}"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;profile&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="nd"&gt;@EventListener&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ContextRefreshedEvent&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;onStartUp&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The current profile is : "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;  &lt;span class="n"&gt;profile&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Annotating &lt;code&gt;onStartUp()&lt;/code&gt; function with &lt;code&gt;@EventListener(ContextRefreshedEvent.class)&lt;/code&gt; means that, this function would run right after the application is ready.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;@Value&lt;/code&gt; annotation will pick up the value from either &lt;code&gt;application-dev.yaml&lt;/code&gt; or &lt;code&gt;application-staging.yaml&lt;/code&gt; depending on which profile you run it with. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;After running the application you'll see that result for the &lt;code&gt;System.out.println&lt;/code&gt; would be either &lt;code&gt;dev&lt;/code&gt; or &lt;code&gt;staging&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The code for the above tutorial is in the following &lt;a href="https://github.com/mhdzaid/spring-profiles"&gt;Github Link&lt;/a&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>springboot</category>
      <category>spring</category>
    </item>
  </channel>
</rss>
