

Table of Contents
1. Overview
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 an example of different port
in development while using another port
in production.
In this example, we have set current active provide using the command line but we can also set active profile programmatically: here is an article to set active profile programmatically.
2. Example

spring boot profile example
NOTE: application-default.properties which does not mean that we can write default properties in that file.
application-default.properties
use is only when no profile has been passed at stating of application then all configuration will be consider fromapplication-default.properties
file.If want to write some default properties value which will be remain same for all profiles then create
application.propreties
file and write those default properties over there. If same properties also found in profile-properties file thenapplication.proeprties
‘s value will be override.
2.1 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>
- When user will not pass profile at that time
application-default.properties
will be considered.
2.2 application-default.properties
If not profile will pass then the application will run on 8081 port.
server.port=8081
2.3 application-development.properties
When application will start on development profile then it will run on 5050 port.
server.port=5050
2.4 application-production.properties
When application will start with production profile then it will run on 6060 port.
server.port=6060
2.5 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 } }
2.6 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
Run with the 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
3. Run with the 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