Saturday, 4 August 2018

Spring Boot: Profile specific properties

In my previous posts, I explained about spring boot profiles, in this post, I am going to explain how can we configure profile specific properties.

How to define profile specific properties?
Profile specific properties are specified using below naming convention.

Syntax
application-{profile}.properties

Let me explain with an example.

application.properties
# Applicaiton name is common for all the profiles
applicationName=Chat Server

# Set the profile to dev
spring.profiles.active=dev
As you see above file, I set 'dev' as my active profile.

application-dev.properties
systemURL=chatserver.dev.com
version=1.5
environment=Development

application-prod.properties
systemURL=chatserver.prod.com
version=1.4
environment=Production


application-test.properties
systemURL=chatserver.test.com
version=1.4
environment=Testing

ApplicationConfigurations.java
package com.sample.myApp.configurations;

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

@Configuration
@ConfigurationProperties
public class ApplicationConfigurations {

 private String systemURL;
 private String version;
 private String environment;
 private String applicationName;

 public String getApplicationName() {
  return applicationName;
 }

 public void setApplicationName(String applicationName) {
  this.applicationName = applicationName;
 }

 public String getSystemURL() {
  return systemURL;
 }

 public void setSystemURL(String systemURL) {
  this.systemURL = systemURL;
 }

 public String getVersion() {
  return version;
 }

 public void setVersion(String version) {
  this.version = version;
 }

 public String getEnvironment() {
  return environment;
 }

 public void setEnvironment(String environment) {
  this.environment = environment;
 }

}

Application.java
package com.sample.myApp;

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

import com.sample.myApp.configurations.ApplicationConfigurations;

@SpringBootApplication
public class Application {

 public static void main(String[] args) {
  ConfigurableApplicationContext applicationContext = SpringApplication.run(Application.class, args);

  ApplicationConfigurations appConfig = applicationContext.getBean(ApplicationConfigurations.class);

  System.out.println("****************************************");
  System.out.println("Application Name : " + appConfig.getApplicationName());
  System.out.println("Environment : " + appConfig.getEnvironment());
  System.out.println("System URL : " + appConfig.getSystemURL());
  System.out.println("Version : " + appConfig.getVersion());
  System.out.println("****************************************");

  applicationContext.close();
 }

}


When you ran ‘Application.java’, you can able to see below messages in the console.
****************************************
Application Name : Chat Server
Environment : Developement
System URL : chatserver.dev.com
Version : 1.5
****************************************


Project structure looks like below.


What is the behavior, if no profiles are activated?
If no profiles are activated, then properties are loaded from 'application.properties' file. If you specify a profile, then profile-specific files always override the non-specific ones.

What is the behavior, when I specify multiple profiles?
If several profiles are specified, a last-wins strategy applies.





Previous                                                 Next                                                 Home

No comments:

Post a Comment