Skip to main content

Posts

Showing posts with the label Spring Profiles

SpringBoot : What are Profiles in Spring Boot Application

What are Profiles in Spring Boot Profiles can be seen as different environments in spring boot application. Suppose you are working on an application where you have different staging environments like Dev, QA, UAT, Production etc. So you will have different configurations for each environment, for this kind of applications what you need is having different values that can be switched depending upon some flags. In early days, people used to write configurations for all environments and comment the unused environments. But with Spring Boot we can do this without commenting any code by use of Profiles . Suppose we have database name configurations for our application as follows Dev     - ekiras_dev QA    - ekiras_qa UAT  - ekiras_uat Prod  - ekiras Now, we can use profiling in this case. How we will do it ? lets see Creating Profiles in Spring Boot  We will create different files for different environments and call then as profile in rest of the blog...

SpringBoot : How to set active profile in spring boot application

Points To Remember There are two ways to set the spring profiles Using OS environment variables Using -D arguments Must Read :: What are profiles in Spring boot. How to set active profile in spring boot application If you want to set Operating System Environment variables for your application the you can do as follows export SPRING_PROFILES_ACTIVE=development export SERVER_PORT=8090 gradle bootRun java -jar build/libs/demo-1.0.0.jar If you want to specify it each time while running jar then you can do as follows java -jar -Dspring.profiles.active=development build/libs/dem-1.0.0.jar java -jar -Dserver.port=8090 build/libs/demo-1.0.0.jar

Spring : How to get Current Profiles in Spring Application

Points To Remember You can use the Environment interface  in package org.springframework.core.env to get the current profiles in spring application. You can make use of the following methods to get the information about the profiles. acceptsProfiles(String ...profiles)  - returns if the profiles given in input is active. getActiveProfiles()  - Gives a list of profiles explicitly made active for this enviornment. getDefaultProfiles()  - gives a set of profiles to be active by default if no active profile is set explicitly. See how to set and configure profiles in spring. Must Read :: What are profiles in Spring boot. How to get Active Profiles in an Environment All you need to do is autowire  the Environmnet bean. call the getActiveProfiles method of the bean. package com.ekiras.config; import org.springframework.stereotype.Component; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.env.Environment; @Component class...