Spring Boot Profile Example

May 22, 2020 | No comments

Spring Boot Profile Examples

This article show example of a Spring Boot Profile and how to use it. Spring Boot application can define multiple Profiles, and at a time we can have more than one Profile active. Activating a certain profile can let the Application use the profile-specific property values or profile-specific Beans. To use different values based on different Profiles, either we can use profile-specific properties files. Or we can also define beans under different Profiles.

Profile specific beans

In Spring Boot we can have Beans which are populated with different values based on the profile that is active. The following example shows a Configuration class PaymentProperties which contains a Bean PaymentProp defined under two profiles. For profile "dev" we are setting currency as USD but for profile "prod" we are setting currency as Pound.
@Profile at the @Bean
The following example shows how to use @Profile annotation at the method level. We have defined two methods(same PaymentProp Beans, different implementations under different Profile) inside the PaymentProperties class. Both methods are annotated with different Profiles. Essentially we have defined profile-specific beans.
@Configuration
public class PaymentProperties {

  @Profile("dev")
  @Bean(name="PaymentProp")
  public HashMap getPaymentServerDev() {
      HashMap paymentMethod = new HashMap<>();
      paymentMethod.put("Currency", "USD");
      paymentMethod.put("ChargePercentage", "1");
      paymentMethod.put("Gateway", "BOA");
      return paymentMethod;
   }
  
  
  @Profile("prod")
  @Bean(name="PaymentProp")
  public HashMap getPaymentServerProd() {
      HashMap paymentMethod = new HashMap<>();
      paymentMethod.put("Currency", "Pound");
      paymentMethod.put("ChargePercentage", "4");
      paymentMethod.put("Gateway", "BankOfEngland");
      return paymentMethod;
  }
  
}
Now When we load the bean, based on which profile is active, Spring Boot will load the corresponding Bean definition.

@Profile with @PropertySource

In this example, we have only one Class defined as MailSenderProperties. Based on the active profile spring boot will populate this class with values read from respective property files.
MailSenderProperties
The following class demonstrates how to use Profile to read profile based configuration files. In this case the MailSenderProperties class with be populated with values from the respective properties file.
@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;
  }
}
Important Points
With Profile specific beans we have to make sure that the beans are available under the respective profiles, before using it. We can have multiple profiles active at a time so we can have multiple beans also populated. @Bean name is important as that is the name by which we can access the beans defined.

Further Reading

No comments :

Post a Comment

Please leave your message queries or suggetions.