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 https://start.spring.io/ 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

    Table of ContentsTools usedProject StructureCreate new Spring boot projectMaven configurationConfigure datasource in Application.propertiesCreate database tableCreate model classCreate Repository interface and implementationCreate service interface and implementationCreate Application classRun the applicationVerify database outputGithub Source code In this post, we will see Spring boot JDBC example. As we already know Spring boot does lot of auto configurations which […]

  • 02 January

    Spring boot devtools tutorial

    Table of ContentsOverviewJRebelIntroduction to Spring Boot DevToolsAdding Devtools to your projectSpring Boot DevTools FeaturesProperty DefaultsLoggingCachingAutomatic RestartLive ReloadGlobal SettingsRemote ApplicationsFile System WatchersConclusion 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 […]

  • 26 December

    Spring boot H2 Database example

    Table of ContentsWhat is H2 database?Tools usedGithub Source code:Project StructureCreate new Spring boot projectMaven configurationH2 database configurationOverride H2 databse defaultsModelRepositoryServiceControllerSpring boot main fileRun the applicationTest the applicationCreate movie objectGet all movie objectsDelete movie id 3 In this post, we are going to see how to create Spring boot application integrating with H2 database. What is […]

  • 22 December

    Spring Boot CrudRepository

    Table of ContentsTools usedProject Structure:Wiring up MongoDBThe Repository ClassThe Save methodThe User Entity ClassBringing the UserDao and CrudRepository togetherServiceControllerApplication classRunning the code sampleConclusionSource code 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 […]

  • 10 April

    Difference between Spring and Spring boot

    Table of ContentsSpring frameworkSpring bootAdvantages of Spring BootSpring vs 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 […]

  • 17 September

    Spring Boot SOAP Web service Example

    Table of ContentsTools usedProject structureAdd wsdl4j dependencyXSD file to define domainGenerate domain classes based on XSDCreate Book repository with dummy dataDefine Book service endpointDefine web service configurationRun the applicatonTest WSDL URLTest Spring soap web serviceSource code In this post, we will see how to create soap web services with Spring boot. We will create contract […]

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.