Profiling in Spring Boot

Posted By : Prakhar Verma | 22-May-2018

Profiling is a way of handling an application configurable value in the different environment with different values.

Profiling can use with a unique profile names on the different application runnable environment.

For Example => An application can run on multiple environment like development environment , stagging environment and production environment and each environment have different configuration values.

These are the steps for creating profiling in spring boot

Step 1 => Create a simple spring boot project with name "SpringBootProfiling".

 

Step 2 => Add two maven dependecies.

          <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

Step 3=> Create new "application-development.properties" file in resources directory and add the database related configuration.

application.admin.name=parrow
#----------------------------Database Connection Configuration-------------------------#

spring.datasource.url=jdbc:mysql://localhost:3306/development
spring.datasource.username=test
spring.datasource.password=test@123
spring.jpa.hibernate.ddl-auto=update
spring.datasource.name=development
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.show-sql=true
spring.jpa.database-platform = org.hibernate.dialect.MySQL5Dialect

#--------------------------------------------------------------------------------------#
server.port=8080

Step 4=> Create new "application-production.properties" file in resources directory and add the database related configuration.

application.admin.name=jacks
#----------------------------Database Connection Configuration-------------------------#

spring.datasource.url=jdbc:mysql://localhost:3306/production
spring.datasource.username=abc
spring.datasource.password=abc@123
spring.jpa.hibernate.ddl-auto=update
spring.datasource.name=production
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.show-sql=true
spring.jpa.database-platform = org.hibernate.dialect.MySQL5Dialect

#--------------------------------------------------------------------------------------#
server.port=8085

Step 5=> Add new properties in "application.properties" file which exists in resources directory.

spring.profiles.active=development
#spring.profiles.active=production

Step 6 => Create a interface with name "EnvironmentConfig".

package com.profiling.config;

public interface EnvironmentConfig {

    String getAdminName();
    
}

Step 7 => Create a class with name "DevelopmentConfig".

package com.profiling.config;

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

public class DevelopmentConfig implements EnvironmentConfig{

    
    @Value("${application.admin.name}")
    private String adminName;

    public String getAdminName() {
        return adminName;
    }

    public void setAdminName(String adminName) {
        this.adminName = adminName;
    }
    
}

Step 8 => Create a class with name "DevelopementProfile".

package com.profiling.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

@Profile("development")
@Configuration
public class DevelopementProfile {

    private static final Logger log = LoggerFactory.getLogger(DevelopementProfile.class);

    @Bean
    public EnvironmentConfig environmentConfiguration(){
        log.info("*********************** Developement Configuration Bean Created *********************");
        return new DevelopmentConfig();
    }
    
}

Step 9 => Create a class with name "ProductionConfig".

package com.profiling.config;

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

public class ProductionConfig implements EnvironmentConfig{

    
    @Value("${application.admin.name}")
    private String adminName;

    public String getAdminName() {
        return adminName;
    }

    public void setAdminName(String adminName) {
        this.adminName = adminName;
    }
    
}

Step 10 => Create a class with name "ProductionProfile".

package com.profiling.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

@Profile("production")
@Configuration
public class ProductionProfile {

    private static final Logger log = LoggerFactory.getLogger(ProductionProfile.class);

    @Bean
    public EnvironmentConfig environmentConfiguration(){
        log.info("*********************** Production Configuration Bean Created *********************");
        return new ProductionConfig();
    }
    
}

Step 11 => Add you actual DB configuration in a properties file and run SpringBootProfiling Application.

Note => command for run application with profiling from a terminal

               java -jar -Dspring.profiles.active=development target/SpringBootProfiling.jar

 

 

About Author

Author Image
Prakhar Verma

Prakhar is a Web App Developer. Experienced in Java and always gives his best effort to complete the given task. He is self motivated and fun loving person.

Request for Proposal

Name is required

Comment is required

Sending message..