INTRODUCTION

We are learning one of spring framework use which is SPRING Profile. How spring profile helps us to store configuration , database URL , credentials according to environments. So we can covering spring profile with example.

What is Spring Profile ?

Spring profile is basically segregation between different run time environment where a spring application run. Basically spring profile provide you the define different configuration , credentials for different environments. When you have multiple environments we can use spring profile.


For example , if we are using MYSQL data on local/dev/qa environments we can not use the same hardware or software used in production as it is costly so to avoid such cases spring profile comes in picture, we can maintain different URL for lower environments and different for production environment. 

Why do we need spring profile ?

The purpose of spring profiles is to activate different beans in different environments at bootstrap time and if you want your application to behave differently in each environment without making code changes. An application is calling different external API or connecting external sources like database, external URL. Thus, we should add these configurations into Spring’s @Configuration class, properties files or YAML files so it makes easy to maintain and manage according to environments.

Spring @Profile Annotation : 

Using @profile annotation we can achieve spring profile in your application. We can use @Profile annotation at different level on your projects like at class level and at method level.
  • Annotating configuration class
You can use @Profile annotation on configuration class , so bean is created if specific profile is active and which profile is using.

Example : 

Dev profiling

@Configuration
@Profile("dev")
public class DevConfig {

  // specific environment bean created according to profile.
}


Production profiling

@Configuration
@Profile("prod")
public class ProdConfig {

  // specific environment bean created according to profile
}

We may apply multiple profiles using @Profile annotation.

@Configuration
@Profile("dev", "qa","pre-prod")
public class CommonConfig {

  // specific environment bean created according to profile.
}


  • Annotating Individual beans 
You can use @Profile annotation at bean creation method in configuration class. So according @Profile annotation respective bean is created when we add profile name is @Profile annotation

Example : 

Consider a scenario where an application uses different Database providers in different environments.
there are two @Bean methods to create a Datasource instance – Oracle data source and MySQL data source.

@Configuration
public class DatabaseBeanConfig {

@Bean
@Profile("dev","qa")
public MysqlDatabaseService CommonDatabaseConfig() {

return new MysqlDatabaseService();
 }

@Bean
@Profile("prod")
public OracleDatabaseService ProdDatabaseConfig() {

return new OracleDatabaseService ();
 }

}


How we can activate profile ?

For activating profile , you can use above method.

Example : 

We can add profile name is application.properties file as "spring.profiles.active" as profile name.

If spring.profile.active is dev/qa then MysqlDatabaseService datasource bean is created and for prod it

is OracleDatabaseService datasource bean is created.


What is issue with @Profile annotation ?

Issue with @Profile annotation is configuration is spread over all code base. It spread all required configuration in multiple places so it is difficult to manage and maintain the configurations. Redundancy of code is increased. Sometimes incorrectly adding profile gives you error.


What is profile specific properties file over @Profile annotation ?


As we have seen issue with @Profile annotation we can use different properties file for different environment. Spring boot will automatically pick up and load the properties file as per active profile.

Properties files are names as per environments like application - {profile}.properties. like for dev it is
  
application-dev.properties , for qa it is application-qa.properties and for prod it is application-

prod.properties.

Example :

1) Create a specific profile and activate it. 

we have different database URL, port number and credentials according to environments. so we can create different application.properties files according to environment in your project at src/main/resources.



application-dev.properties

spring.datasource.url : jdbc:mysql://http://192.45.10.1:4506/dev-database
spring.datasource.username : suraj
spring.datasource.password : ****


application-prod.properties

spring.datasource.url : jdbc:mysql://http://192.45.10.4:4507/prod-database
spring.datasource.username : avnee
spring.datasource.password : ****

and activate required profile for your application using

2) Activate your profile: 

If we want to activate multiple profiles then we can pass comma-separated names of those profiles.

We can set it in many places, for example, in application.properties file:

spring.profiles.active : dev, qa


3) Use command line for activate the profile.

java -jar my-application.war -- spring profiles.active = dev

CONCLUSION

We have learned how to use profile in your spring boot applications. We can use @Profile annotation as per requirement. But @Profiles annotation have some cons. So best practice is to create different properties file for different environment. It will make easier to find and maintain the configurations.


USEFUL LINKS : 

GIT commands : 

https://knowledgegainwithsuraj.blogspot.com/2023/07/use-git-like-expert.html

Spring Boot Annotations : 

https://knowledgegainwithsuraj.blogspot.com/2023/07/spring-boot-annotations.html

Rest API using spring boot : 

https://knowledgegainwithsuraj.blogspot.com/2023/07/create-rest-api-using-spring-boot.html


ABOUT US

I am Suraj Ladda and I am a Software Developer in IT Industry

CONTACT US

surajladda07@gmail.com