springjava.com
  • Core Java
  • Spring Core
  • Spring Boot
  • Spring MVC
  • Angular Tutorial

Related Posts

  • Spring Boot Annotations How to Upload a File in Spring Boot JSP How to create a Spring Boot project in VS Code How to create a Spring Boot project in IntelliJ IDEA How to create a Spring Boot project in STS How to Create and Setup Spring Boot Project in Eclipse Spring Boot CRUD Operations with JSP Spring Boot Pagination & Sorting with REST API Spring Boot CRUD with Thymeleaf Sending Mail Using Gmail SMTP Server In Spring Boot
Spring Boot

Configure Profiles in Spring Boot App

Last Updated: 11-07-2022 Springjava

When we are developing some application then that application is having different stages are going to like:
      • Development Stage
      • Testing Stage
      • Production Stage
These stages are having different configurations then how will manage these configurations in different files and how to activate a specific stage of the configuration file?
Profiles provide a solution to this problem. This will help manage and activate specific stages of the configuration file of the application.

configure_profiles_in_spring_boot_app

In this Spring Boot application, we created three properties files related to stages of application development:
     1. Development Stage of this application(application-dev.properties)
     2. Testing Stage of this application(application-qa.properties)
     3. Production Stage of this application(application-prod.properties)
In this application, we will activate a specific stage of the application through the application.properties file.
In the application-dev.properties file, we will configure the server port number and message. This server port number configuration identifies that the development stage of the profile is activated and shows the message which is defined in this properties file. When this application after active in this properties file then the console is showing “dev” is active now.
In the application-qa.properties file, we will configure the server port number and message. This server port number configuration identifies that the testing stage of the profile is activated and shows the message which is defined in this properties file. When the application after active this properties file then the console is also showing “qa” is active now.
In the application-prod.properties file, we will configure the server port number and message. This server port number configuration identifies that the production stage of the profile is activated and shows the message which is defined in this properties file. When the application after active this properties file then the console is also showing “prod” is active now.

Development Process:
1. Keep eclipse IDE ready(STS Integrated) 
2. Create a Spring Boot Starter Project
3. Create application.properties files for stages
4. Create a Rest Controller
5. Run the application

1. Keep eclipse IDE ready(STS Integrated) 
Refer to this article How to Create Spring Project in IDE to create Spring Boot Project in Eclipse IDE.

2. Create a Spring Boot Starter Project
Add the following dependencies: 
    • Spring Web

configure_profiles_in_spring_boot_app

Maven Dependency
pom.xml:

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>2.7.0</version>

<relativePath /> <!-- lookup parent from repository -->

</parent>

<groupId>com.emp</groupId>

<artifactId>Spring_Boot_Profiles_POC</artifactId>

<version>0.0.1-SNAPSHOT</version>

<name>Spring_Boot_Profiles_POC</name>

<description>Demo project for Spring Boot</description>

<properties>

<java.version>16</java.version>

</properties>

<dependencies>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-test</artifactId>

<scope>test</scope>

</dependency>

</dependencies>

<build>

<plugins>

<plugin>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>

</plugin>

</plugins>

</build>
</project>

3. Create application.properties files for stages
application-dev.properties: 

server.port=4545
spring.profile.message=Welcome to Development Environment

application-qa.properties:

server.port=4646
spring.profile.message=Welcome to Testing Environment

application-prod.properties:

server.port=4444
spring.profile.message=Welcome to Production Environment

application.properties:

spring.profiles.active=dev

→ spring.profiles.active=____ is activating your properties file configuration.

4. Create a Rest Controller
HomeController.java:

package com.poc.controller;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RestController;

@RestController
public class HomeController {

@Value(value="${spring.profile.message}")
private String msg;

@GetMapping("/")
public String getMsg() {
return msg;

    }
}

→ @Value is used for fetching value from the application.properties file.

5. Run the application
Type this URL http://localhost:4545 on the browser.

configure_profiles_in_spring_boot_app

We are activating the application-qa.properties file for this we can type spring.profiles.active=qa in the application.properties file. After that, run the project. Then see your console log.
Type this URL http://localhost:4646 on the browser.

configure_profiles_in_spring_boot_app

We are activating the application-prod.properties file for this we can type spring.profiles.active=prod in the application.properties file. After that, run the project. Then see your console log.
Type this URL http://localhost:4444 on the browser.

configure_profiles_in_spring_boot_app

Conclusion:
This topic is explained What are the Profiles? How to use Profiles in the Spring Boot project?

What is Spring Boot How to Create a Spring Boot Project in IDE Spring MVC with Spring Boot Configure

Leave your thought here

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

springjava_com
  • springjavateam@gmail.com
  • springjava.com
Learn
    • Core Java
    • Spring Core
    • Spring Boot
    • Spring MVC
    • Angular Tutorial
Quick links
  • About Us
  • Contact Us
  • Privacy Policy
Subscribe to Blog via Email
Subscribe
© 2021 Spring Java. All Rights Reserved.
springjava.com
  • Core Java
  • Spring Core
  • Spring Boot
  • Spring MVC
  • Angular Tutorial
  • About Us
  • Contact Us