Spring boot

Spring boot profiles for different environments

How to add Profile-specific Properties?

In addition to application.properties files, profile-specific properties can also be defined by using the following naming convention:

application.properties
application.default.properties
application-{custom-profile}.properties.

The Environment has a set of default profiles (by default, [default]) that are used if no active profiles are set.
In other words, if no profiles are explicitly activated, then properties from application-default.properties are loaded.

Profile-specific properties are loaded from the same locations as standard application.properties, with profile-specific files always overriding the non-specific ones, whether or not the profile-specific files are inside or outside your packaged jar.

You can use a spring.profiles.active Environment property to specify which profiles are active.

application.properties

server.port=8081
spring.profiles.active=dev

application-prod.properties

server.port=8082

application-dev.properties

server.port=8083

@Profile Configuration for different Environment

Spring Profiles provide a way to segregate parts of your application configuration and make it be available only in certain environments.
Any @Component, @Configuration or @ConfigurationProperties can be marked with @Profile to limit when it is loaded.

DevProfileController.java

package springBootProfiles.controller;

import org.springframework.context.annotation.Profile;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Profile("dev")
@RequestMapping(value = "/")
public class DevProfileController {

 @GetMapping
 public String message() {
  return "A message from dev";
 }

}

ProdProfileController.java

package springBootProfiles.controller;

import org.springframework.context.annotation.Profile;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Profile("prod")
@RequestMapping(value = "/")
public class ProdProfileController {

 @GetMapping
 public String message() {
  return "A message from prod";
 }

}

Launch spring boot application ( SpringBootProfilesApplication.java)

package springBootProfiles;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootProfilesApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringBootProfilesApplication.class, args);
	}

}

Configuring pom.xml

There is no special starter or dependency for configuring profiles, everything comes with spring parent starter.

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.2.6.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.candidjava.spring.boot</groupId>
	<artifactId>springBootProfiles</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>springBootProfiles</name>
	<description>Demo project for Spring Boot Profiles</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

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

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

Download

Download source code from my github account Click here