Spring boot profiles example

In this tutorial, we will see about Spring boot profiles.


Need for profiles

Application development is complex process. You might have different environment for application development.

  • Dev
  • SIT
  • QA
  • Prod

We require different configuration based on the environment.

For example: Using a different database or different queues.

Let’s create a simple spring boot application.

Step 1: Go to <a href="https://start.spring.io/">https://start.spring.io/</a> and create spring boot projects as per below screenshot.

SpringIntializr

Step 2: Import the maven project in eclipse.

step 3: Create a package named "org.arpit.java2blog.controller"
create a controller class named "HelloWorldController.java"

Step 4: Put “message=java2blog” in application.properties.

Step 5: Create a package named “org.arpit.java2blog”
create a class named “SpringBootHelloWorldApplication.java”

Step 6: Run the application
When you run above application, you will get below output:



2018-08-03 10:40:21.649 INFO 27143 — [ main] o.a.java2blog.SpringBootProfileExample : Started SpringBootProfileExample in 4.693 seconds (JVM running for 5.474)
2018-08-03 10:40:36.761 INFO 27143 — [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet ‘dispatcherServlet’
2018-08-03 10:40:36.762 INFO 27143 — [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet ‘dispatcherServlet’: initialization started
2018-08-03 10:40:36.789 INFO 27143 — [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet ‘dispatcherServlet’: initialization completed in 27 ms

Step 7:Go to browser and browse url “http://localhost:8080/welcome”

Spring boot output
As you can see, we are able to fetch “message” from application.properties.


Profiles for environment specific properties

Let’s configuration two profile- dev and prod for demonstration.
You need to create .properties file with application-"$ProfileName".
For example:
For dev profile, you need to crate application-dev.properties, so whatever you put in application-dev.properties, it will override properties from application.properties.
Suppose you need to start qa profile at port “8081”, you can put following entry in application in application-qa.properties

Let’s create two properties file for our application.
For dev profile

For prod profile

Setting active profile

When you run spring boot application, you need to set active profile. There are multiple ways to set active profile.

  1. By using -Dspring.profiles.active=dev in VM Arguments
  2. By using spring.profiles.active=dev in application.properties

Let’s run the application with dev profile now. We will put spring.profiles.active=dev in VM arguments and click on run.

Spring boot dev profile

When you run the application and browse http://localhost:8080/welcome”.

Spring boot dev output

Spring boot @Profile annotation

You can use @Profile annotation in java configuration to create profile based configuration.


Project Structure

Spring boot profile example


Source code

That’s all about Spring boot profiles.



import_contacts

You may also like:

Related Posts

  • 11 January

    Spring boot JDBC example

    In this post, we will see Spring boot JDBC example. As we already know Spring boot does lot of auto configurations which help us to avoid a lot of boilerplate code. In the case of JDBC, spring boot provides auto-configuration such as creating DataSource and JDBCTemplate objects automatically based on application.properties. So you just need […]

  • 02 January

    Spring boot devtools tutorial

    In this post, we’ll be looking up what does spring has to offer for developer experience. Overview Using IDEs like Eclipse, Spring Tool Suite, Intellij Idea a typical development setup includes an application code base that gets deployed on a Web Application Server like Wildfly or Tomcat or even Glassfish. The speed of development is […]

  • 26 December

    Spring boot H2 Database example

    In this post, we are going to see how to create Spring boot application integrating with H2 database. What is H2 database? H2 is open source database. It is very fast and smaller in size. It is in-memory database and keeps all data in memory. If you start and stop the application, all the data […]

  • 22 December

    Spring Boot CrudRepository

    In this post, we’ll be looking up how to create and use Spring Boot CrudRepository. SpringBoot CrudRepository provides sophisticated CRUD functionality for the type of entity you want to be managed.This interface acts primarily as a marker interface to capture the types to work with and to help you to discover interfaces that extend this […]

  • 10 April

    Difference between Spring and Spring boot

    In this post, we will see difference between Spring and Spring boot. If you have followed spring project over time, you might have noticed it has become increasingly complex. If you want to create a spring application, you still need to put a lot of efforts. Spring boot is introduced to save time to bootstrap […]

  • 17 September

    Spring Boot SOAP Web service Example

    In this post, we will see how to create soap web services with Spring boot. We will create contract first soap web service with Spring boot. We will focus on how to define configurations for soap web services. Tools used JDK 1.8, Eclipse, Maven Spring-boot – Underlying application framework wsdl4j – for publishing WSDL for our […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.