What is Spring Boot Profiles


What is Spring Boot Profiles

Generally software applications, we need to be able to run it under different environments or under different setups. For example, say our application sends emails. For sending emails we need to configure SMTP servers with correct server URL, user, password etc. While developing we will generally have different SMTP servers credentials then the Production servers. Let's assume we have two different kinds of environments, dev- Development and prod-Production. So we would like to run the same code in dev and prod environments but with different configurations for email. Similarly we might have similar requirements for database, logs, security, etc, where we want to have different services/configurations plugged while in developing vs in productions. Spring Profiles helps to segregate our application configurations and make them available only in certain environments. 
In this article, we will dive into a situation where we would like to use different configurations in different environments. To achieve this high level we need two things,
1) Way to provide different configurations under different environments
2) Way to use environment-specific values in our app.
In the following example, we have two profiles and two property files. We have a java class MailSenderProperties which encapsulates the email configuration. Spring Boot will populate MailSenderProperties based on the profile that is active.

Profile and Properties files

Property file naming scheme
The property file is generally named by application HYPHEN PROFILENAME HYPHEN.properties.
application-{_profile_}.properties
We have two profiles (dev, prod) and two profile-specific property files. application-dev.properties application-prod.properties Now based on the profile we set active the property values would be read from the corresponding properties file.

Properties files

application-dev.properties
This is the property file for dev profile.
mail.smtp.host=mail.dev.com
mail.smtp.port=7777  
mail.smtp.user=test_dev 
application-prod.properties
This is the property file for prod profile.
mail.smtp.host=mail.prod.com
mail.smtp.port=8888  
mail.smtp.user=prod_user
application.properties
This is the common property file, which contains general configurations like logging level and also the active profile.
#Other configurations
spring.profiles.active=dev
logging.level.root=info

Mapping the Profile specific value to java class

MailSenderProperties
Java class to encapsulate the MailSender values, which are read from the application-PROFILE.properties file. We use the @PropertySource annotation and specify the respective property file that should be used by classpath:application-${spring.profiles.active}.properties
@Component
@PropertySource("classpath:application-${spring.profiles.active}.properties")
public class MailSenderProperties {

  @Value("${mail.smtp.host}")
  private String smtpHost;

  @Value("${mail.smtp.port}")
  private Integer smtpPort;

  public String getSmtpHost() {
    return smtpHost;
  }

  public void setSmtpHost(String smtpHost) {
    this.smtpHost = smtpHost;
  }

  public Integer getSmtpPort() {
    return smtpPort;
  }

  public void setSmtpPort(Integer smtpPort) {
    this.smtpPort = smtpPort;
  }
}

Setting Active Profile

We can set the active profile a number of ways. Some of the ways we need to change code to set the active profile. Here we will cover some fo the ways to set the active profile without changing code. 1) Set an active profile in the application.properties file ( dev is the active profile) spring.profiles.active=dev 2) set the active profile via an environment variable ( prod is the active profile) export spring_profiles_active=prod
Summary
  • Though using Spring Profile we can use a different set of properties under different circumstances
  • Spring profile can be set both programmatically and though environment variables
  • We can have more than one active profile settings at any time.
  • If we don't set any profile then Spring boot uses a profile called default profile.

Next Reading

No comments :

Post a Comment

Please leave your message queries or suggetions.