Profiles in Spring Boot

Posted By :Rozi Ali |30th November 2020

Spring Boot Profiles

 

A profile can be describe as a set of properties and configuartion settings. An application can have multiple profiles which we may use to execute in different environments, like Dev, Stage, Production. Also, we can have different databases for different profile for a particular application.

In Spring Boot, we create a different properties file for every new profile. We can control the active state of these profiles in application.properties file, which is the master properties file.

Note: If more than one profile is specified, then the last one will gain precedence.

 

Also Read: Spring Boot Data Rest

 

Steps to create a profile in Spring Boot

 

Step 1

 

Create a properties file for a profile: Define file name as - application-{profile}.properties

Example- application-Dev.properties

spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
spring.datasource.username=root
spring.datasource.password=root

 

Step 2

 

Specify profile in application.properties file 

Here, in spring.profile.active property, we mention the profile name that we want to active

spring.profile.active=Dev
spring.application.name=Profiles

 

Step 3 

 

Next step is to configure database in DBConfiguration.java

@Profile annotation is used to set a bean to a defined profile 

@Configuration
@ConfigurationProperties("spring.datasource")
public class DBConfiguration{
    private String driverClassName;
    private String url;
    private String username;
    private String password;

    @Profile("Dev")
    @Bean
    public String devDatabaseConnection(){
          return "Dev database connection";
    }
}

Note: If we user @Profile annotation as @Profile(!Dev) then this specify that this bean does not belong to Dev. And if a bean does not specify a profile then it belongs to a default bean.

 

Also Read: Spring Boot Application Configured As Eureka Server

 

That's it! This is is how you create a profile and set it's status active.

 

Why You Should Choose Oodles For SaaS Product Development?

 

We are a 360-degree software development company that provides cross-platform SaaS app development services to address varied software project requirements. We have an experienced team of Java, PHP, and Python developers who use advanced frameworks, tools, and SDKs to build scalable web and mobile applications with custom features. For more detail, reach us out at [email protected].


About Author

Rozi Ali

Rozi Ali is an accomplished software developer with extensive experience in the field of JAVA. She possesses a solid grasp of programming languages such as Java/Spring-boot, Python, and Typescript/Nodejs/GraphQL. Rozi has a strong background in Object-oriented programming (OOP) and is skilled in working with both relational databases like MySql, PostgreSQL and non-relational databases like MongoDb. She is proficient in REST APIs, Microservices, and code deployment, along with the development tools such as Jira, Git, and Bash. Additionally, Rozi has experience working with Cloud providers such as AWS and Azure. She has contributed significantly to a number of projects, including Konfer, VNS, Influsoft, VN Platform, QuickDialog, and Oodles-Dashboard.

Request For Proposal

[contact-form-7 404 "Not Found"]

Ready to innovate ? Let's get in touch

Chat With Us