This article contains Spring Boot Profiles Example using properties files. Profile based development is very useful while we have different configuration in development environments and some different environment in production. Here is example of different port in development while using another port in production.

spring boot profile example

spring boot profile example

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>spring-boot-profile-example</groupId>
    <artifactId>spring-boot-profile-example</artifactId>
    <version>1.0-SNAPSHOT</version>
    <!-- Inherit defaults from Spring Boot -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.4.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <!-- Package as an executable jar -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

Spring Boot will consider application-default.properties configuration and if same configuration found in profile properties files than it will consider profile properties file configuration.  When user will not passed profile at that time application-default.properties will be consider.

application-default.properties

server.port=8081

application-development.properties

server.port=5050

application-production.properties

server.port=5050

SpringBootConfig

package com.javadeveloperzone;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

/**
 * Created by JavaDeveloperZone on 19-07-2017.
 */

@SpringBootApplication
@ComponentScan // Using a root package also allows the @ComponentScan annotation to be used without needing to specify a basePackage attribute
public class SpringBootConfig {
    public static void main(String[] args) throws Exception {
        SpringApplication.run(SpringBootConfig.class, args);            // it wil start application
    }
}

 

ProfileCheckController

Spring provide org.springframework.core.env.Environment class which contains application configuration from where we can check that which current profile is activated in running application.

package com.javadeveloperzone.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.logging.Logger;

/**
 * Created by JavaDeveloperZone on 19-07-2017.
 */
@RestController
public class ProfileCheckController {

    private static final Logger logger = Logger.getLogger(ProfileCheckController.class.getName());

    @Autowired
    private Environment environment;

    @GetMapping("/checkProfile")
    public String checkProfile() {
       return "Spring Boot is running under "+environment.getActiveProfiles()[0] + " Profile";
    }
}

Demo

  1. Run with default profile

java -jar spring-boot-profile-example-1.0-SNAPSHOT

2017-07-27 18:14:39.649  INFO 153668 --- [           main] com.javadeveloperzone.SpringBootConfig   : No active profile set, falling back to default profiles: default
2017-07-27 18:29:33.701 INFO 175460 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8081(http)

2. Run with development profile

 java -Dspring.profiles.active=development -jar spring-boot-profile-example-1.0-SNAPSHOT.jar

2017-07-27 18:20:08.058  INFO 108868 --- [           main] com.javadeveloperzone.SpringBootConfig   : The following profiles are active: development
2017-07-27 18:29:33.701 INFO 175460 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
spring boot profiles example - Development Profile

spring boot profiles example – Development Profile

3. Run with production profile

java -Dspring.profiles.active=production -jar spring-boot-profile-example-1.0-SNAPSHOT.jar

 2017-07-27 18:22:08.058 INFO 108868 --- [ main] com.javadeveloperzone.SpringBootConfig : The foll owing profiles are active: production
2017-07-27 18:29:33.701 INFO 175460 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 6060 (http)
spring boot profiles example - Production Profile

spring boot profiles example – Production Profile

References:

Spring Boot Profiles Document

 

Was this post helpful?
Let us know, if you liked the post. Only in this way, we can improve us.
Yes
No

Leave a Reply

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