SpringSpringBootHow we use Spring Profiles using SpringBoot

How we use Spring Profiles using SpringBoot

In this article, we’re going to analyze Spring Profiles using Spring Boot and see how we can use it effectively in our project.

Introduction to Spring Profiles using Spring Boot

Spring Profiles offers a powerful and quick way to monitor the environment-based code and configuration. Using Spring Profiles, it is possible to isolate portions of our program and make it accessible only in some settings.  We need to use the @Profile annotation to restrict the availability of any @Component or @Configuration.

What is the use of @profile

The key entry point for the Spring Profile is the @Profile annotation that can be used to group items together. Take a basic example for a Database link bean where we want to make sure that a certain DB connection is only involved in DEV mode but not in output or QA/Stage mode. We will use the @Profile tag to do this.

@Service
@Profile("development")
public class DBConnectionDev implements  DatabaseService {
    
    @Override
    public void getDBConnection() {
        System.out.println(" DB connection established for dev Env");
        System.out.println("In this post we undertand the use of Spring Profiles using SpringBoot ");

    }
}

Because we have annotated DevDBConnection bean with the “development” profile, it will only be available in the Spring container if the development profile is active, in other words, if the development profile is not active, this bean will not be available/working.

The default configuration used for the Spring Profile is the same. Both beans that do not have a profile annotation are included in the default profile. You may also set the default profile in the Spring Boot by @Profile(“default”) or @Profile({“default”,””development”}).

Use of @Profile Annotation

Spring Boot offers a variety of ways to enable the profile. We may transfer profile information through the command line or use the application.properties, Spring Boot also offers a way to programmatically set the profile.

Using Command Line

We can send the profile information to Spring Boot by using the command prompt—

spring.profiles.active=development,stage.

Using Property File

In our application.properties or application.yaml, we will use the regular Spring environment property spring.profiles.active property to define active profiles.

spring.profiles.active=development,staging

Programmatically setting profile

You can programmatically set the active profile by calling the setAdditionalProfiles(…) method given by the SpringApplication class.

SpringApplication app = new SpringApplication(Application.class);
app.setAdditionalProfiles("development","production");

Please notice that the spring.profiles.active property follows the same ordering law as the other properties identified by the Spring Boot. The highest source property would overwrite every other property specified in the hierarchy. Please refer to the Spring Boot documents to see how Spring Boot reads these assets.

Profile Specific Configurations

One of the most interesting and powerful functionality offered by Spring Boot is the ability to identify and enable a profile specific application.properties file by main application.properties file.

To use profile related configuration files, we need a framework naming convention-{profile}.properties where the profile specifies the name of the intended profile. Profile-specific files will be loaded from the same place as the application.properties file, also be mindful that profile-specific properties will overwrite the properties specified in the default application.properties, regardless of whether the profile-specific files are within or outside your packed container.

To properly understand this, let’s use the same example of Database configuration where we want to specify various DB settings for Creation and Output. To do this using configuration files, we can define 2 configuration files, namely application-production.properties and application-development.properties.

application-production.properties

db.url=jdbc:oracle:thin:@<host>:1521:<sid>
db.driver=oracle.jdbc.driver.OracleDriver
db.username=<username>
db.password=<password>
db.tableprefix= 

application-development.properties

db.url=jdbc:hsqldb:file:configurations
db.driver=org.hsqldb.jdbcDriver
db.username=sa
db.password=
db.tableprefix=

Understand by Example

DataBase Service

public interface DatabaseService {

    void getDBConnection();
}

Development Profile

@Service
@Profile("development")
public class DevDBConnection implements  DatabaseService {

    @Override
    public void getDBConnection() {
        System.out.println("DEV DB connection established for onurdesk ");
    }
}

Production Profile

@Service
@Profile("production")
public class ProdDBConnection implements DatabaseService {

    @Override
    public void getDBConnection() {
        
        System.out.println("Product DB connection establish");
    }
}

Spring Boot Runner

@SpringBootApplication
public class OnurdeskApplication implements CommandLineRunner{

   @Autowired
    DatabaseService databaseService;

    public static void main(String[] args) {

      SpringApplication.run(OnurdeskApplication.class, args);
   }

    /**
     * Callback used to run the bean.
     *
     * @param args incoming main method arguments
     * @throws Exception on error
     */
    @Override
    public void run(String... args) throws Exception {
        databaseService.getDBConnection();
    }
}

output

if you run above example with development profile as active profile, we will have the following output

DEV DB connection established

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe Today

GET EXCLUSIVE FULL ACCESS TO PREMIUM CONTENT

Get unlimited access to our EXCLUSIVE Content and our archive of subscriber stories.

Exclusive content

Latest article

More article