Spring Boot profiles

In this article, we will see profiles for spring boot project –

Github Link –

 

What are Profiles?

Every enterprise application has many environments, like:

Dev | Test | Stage | Prod | UAT / Pre-Prod

Each environment requires a setting that is specific to them. For example, in DEV, we do not need to constantly check database consistency. Whereas in TEST and STAGE, we need to. These environments host specific configurations called Profiles.

How do we Maintain Profiles?

In spring boot application, we need different configurations for different environments. We have application.properties file to maintain application specific properties. In order to create profiles for different environments , we need to create various properties files as below –

DEV – application-dev.properties

TEST– application-test.properties

PROD – application-prod.properties

Step 1: Create below project structure in IntelliJ

Step 2: We are creating 3 different property files for below mentioned environments –

DEV – application-dev.properties

TEST– application-test.properties

PROD – application-prod.properties

Of course, the application.properties will remain as a master properties file, but if we override any key in the profile-specific file, the latter will gain precedence.

Step 3: Specify in application.properties file which environment property needs to be loaded

spring.profiles.active=prod

spring.application.name=springbootprofiles

app.message = This is Application property for ${spring.application.name}

Here we are loading PROD properties when application gets booted.

Step 4: Define DB configuration properties for in respective properties file and add code in DBConfiguration.class to pick the appropriate settings.

package com.myjavablog.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

@Configuration
@ConfigurationProperties("spring.datasource")
public class DBConfiguration {

    private String driverClassName;
    private String url;
    private String username;
    private String password;

    public String getDriverClassName() {
        return driverClassName;
    }

    public void setDriverClassName(String driverClassName) {
        this.driverClassName = driverClassName;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Profile("dev")
    @Bean
    public String devDatabaseConnection() {
        System.out.println("DB connection for DEV - H2");
        System.out.println(driverClassName);
        System.out.println(url);
        return "DB connection for DEV - H2";
    }

    @Profile("test")
    @Bean
    public String testDatabaseConnection() {
        System.out.println("DB Connection to TEST -H2");
        System.out.println(driverClassName);
        System.out.println(url);
        return "DB Connection to TEST -H2";
    }

    @Profile("prod")
    @Bean
    public String prodDatabaseConnection() {
        System.out.println("DB Connection to PROD - H2");
        System.out.println(driverClassName);
        System.out.println(url);
        return "DB Connection to PROD - H2";
    }
}

This will load the properties file as per the spring.profiles.active property we have mentioned in application.properties file.

Step 5: Output

As per the application boot time logs below, you can see PROD database is loaded during application boot up.

2019-12-01 23:29:07.199  INFO 5728 --- [           main] rmationExtractorJdbcDatabaseMetaDataImpl : HHH000262: Table not found: user_details
2019-12-01 23:29:07.419  INFO 5728 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
DB Connection to PROD - H2
org.h2.Driver
jdbc:h2:mem:proddb;DB_CLOSE_ON_EXIT=FALSE
2019-12-01 23:29:09.147  INFO 5728 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot[email protected]e25951c: startup date [Sun Dec 01 23:28:49 IST 2019]; root of context hierarchy

You can connect to database as shown below –

Bitnami