Spring Boot Profiling – Configure for Different Environments

Spring Boot Profiling – Configure for Different Environments :-

Spring Boot Profiling provides a way to segregate parts of your application configuration and make it only available in the certain environment.It is one of those concepts which we don’t use if we are working in development or just for learning purpose. Spring Boot Profiling is needed for applications which need’s to load different configurations for different environments Such as QA, DEV OR PROD.

In this Post, we will see how we use different database Url for different environments and let spring boot load the Url based on environment specified or profiling mentioned.



In order to demonstrate Spring Boot Profiling, we will create two different property files for prod and QA environments. I am using PostgreSQL for dev environment and MySQL for prod environment. Let us Check the Project structure below.

Project Structure:-

spring boot profiles

Let’s check property files for different environments, we are using different property files for different config in our application.Let’s configure database URL for those property files.




application.properties

 

This is the default properties for Spring Boot. You can see it has a property spring.profiles.active as a prod, this means that spring boot will look for the default property as application-prod.properties.The configuration written in the application-prod.properties is loaded by default if we don’t specify any profile while running my spring boot application.We need these types of configuration while creating any application .Let’s check this application here  Where we can use profiling .




We can also specify active profiles inapplication.properties and replace them using the command line switch.We can also specify multiple active profiles, for example:- spring.profiles.active=dev, prod in our properties file.

application-prod.properties

 

application-dev.properties

 

DemoApplication.java:-

 

Here we are printing out the database URL which is getting loaded in Spring Boot Profiling.


Running the Application:-

Navigate to the Project directory and run mvn clean install .

Now type this command mvn spring-boot: run, once you run this maven command you will see something like this below:-

spring profiling

You can see our prod database Url is now loaded by default. Now let’s assume I am in development machine and want to run development database config, so I need to run my application in development mode. Please run following command line switch to switch to your dev profile.

spring boot profiling

output:-

Here the Postgresql database url must be loaded if everything is correct.

spring boot profiling

This is how we do profiling in a real-world application, spring also provides the Programmatic way of doing profiling. Please check out this link to find about Programmatic Way of Spring Boot Profiling.

Check this Book below to Learn More Basics Of Spring Boot .

It’s Suggested to do and apply configurations to deployment .

Related Links:-

Creating REST Service with Spring Boot

Spring MVC Basic Setup and Hello World
Custom ClassLoaders In Java
Overload Or Override Static Methods in Java

 

17 thoughts on “Spring Boot Profiling – Configure for Different Environments

  1. Hi there would you mind sharing which blog
    platform you’re using? I’m going to start my own blog in the near future but
    I’m having a difficult time deciding between BlogEngine/Wordpress/B2evolution and Drupal.
    The reason I ask is because your design and style seems different then most blogs and
    I’m looking for something completely unique.
    P.S Apologies for getting off-topic but I had to ask!

    1. Hello Buddy,
      You Can use WordPress as WordPress has hell lot of feature. Then Go ahead and customize things you don’t like or don’t want to keep in your blog.Wordpress is Super Easy and lots of themes to choose.

  2. This is very interesting, You’re a very skilled blogger.
    I’ve joined your rss feed and look forward to seeking more of
    your fantastic post. Also, I’ve shared your web site in my
    social networks!

  3. Pretty great post. I just stumbled upon your blog and wished to say
    that I’ve really loved browsing your blog posts.
    In any case I will be subscribing to your rss feed and I hope you write once more very soon!

  4. I think this is among the most significant info for me.
    And i’m glad reading your article. But wanna remark on few general things, The web site style is perfect, the
    articles is really excellent : D. Good job, cheers

Leave a Reply

Your email address will not be published. Required fields are marked *