Saturday 4 August 2018

Spring boot: Profiles

In this post, I am going to show you how to use spring profiles.

We can map our beans, configurations to specific profiles (Ex: prod, dev, test etc.,) and can use when the respective profiles are activated.

How can I mark a component (or) configuration with profile?
Just add @Profile annotation on top of a @Component, @Configuration class.

How to activate a profile?
By adding the property 'spring.profiles.active' in application.properties file, you can activate the profile.

Ex
spring.profiles.active=dev,prod

Above statement activates dev, prod profiles.

Let me explain with an example. I am going to create 3 profiles dev, prod and test. When I activate the test profile and ran the application, only test profile specific bean is getting instantiated by spring framework.

DevProfile.java
package com.sample.myApp.model;

import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

@Component
@Profile("dev")
public class DevProfile {

 public DevProfile() {
  System.out.println("********************************");
  System.out.println("Dev profile is called");
  System.out.println("********************************");
 }
}

ProdProfile.java
package com.sample.myApp.model;

import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

@Component
@Profile("prod")
public class ProdProfile {
 public ProdProfile() {
  System.out.println("********************************");
  System.out.println("Prod profile is called");
  System.out.println("********************************");
 }
}

TestProfile.java
package com.sample.myApp.model;

import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

@Component
@Profile("test")
public class TestProfile {
 public TestProfile() {
  System.out.println("********************************");
  System.out.println("Test profile is called");
  System.out.println("********************************");
 }
}


application.properties
spring.profiles.active=test

Application.java
package com.sample.myApp;

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

@SpringBootApplication
public class Application {

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

  applicationContext.close();
 }

}

When you ran Application.java, you can able to see below messages in the console.
********************************
Test profile is called
********************************


Project structure looks like below.


Profile names and not operator
You can prefix a profile name with a not (!) operator.

Example
@Profile("!dev")

In the above example, a component is active only if the dev profile is not active.

What is default profile?
If a bean don’t specify a profile, then it belongs to ‘default’ profile. You can specify the default profile using the property ‘spring.profiles.default’. If you don’t specify the profile to use, then spring uses default profile configure using the property ‘spring.profiles.default’

How to activate the profile using command line argument?
Pass below command line argument to activate the ‘dev’ profile.
-Dspring.profiles.active=dev


Previous                                                 Next                                                 Home

No comments:

Post a Comment