Updated April 10, 2023
Definition of Spring Boot Profiles
While creating applications in spring boot we have different stages in the development and they often treated as dev, test, and prod. This helps us to have a different profile for every environment, we use @profile annotation to implement this in the spring boot application also. By the use of setting different profiles, we can set different seating for each environment without affecting the other environment. This is very useful by the use of it we can use the local, development environment the way we want. In the coming section of the tutorial, we will see what are the steps needed to set up this in our spring boot application. Also, we will look internally at how it works and implement it for beginners to understand it better.
Syntax:
As we already know that we have to make some configuration in order to use this in spring boot, it is all related to making configuration only. We can have the properties set into the application.properties file. Let’s take a closer look at the syntax for beginners to use understand it better see below;
@Profile(value=" your profile name profile")
As you can see we have used @Profile annotation and inside it, we have provided the name for our profile. Let’s take a closer look at the sample piece of syntax for beginners which is easy to understand better see below;
e.g. :
@Profile(value="local")
The above piece of syntax will active the local profile for us. In the coming section of the tutorial, we will see how to use and configure it fully to use this while application development.
How profiles work in Spring boot?
In this section, we will see how we can use this while programming in spring boot, also the internal working of the profile setting in spring boot. As we now know that profiles are very important when it comes to set the different setting for every environment we have because the development of the spring boot includes different environment such as prod, dev, test and other if we have. This is important because we cannot have the same database and many more things with every environment we have, it also prevents us to make any conflict on the prod database also. In this section first, we will see how we can start setting up the profiles in the spring application see below;
Let’s get started;
1) The first step we need to make the application file for every environment we have. In spring boot we have to follow some standard given by it, we can create the application file in the below format;
application-{profile}.properties
As you can see you can create the application file like above, in the profile part you can mention the name of the profile you want.
2) Inside the application file we can give the name for the profile by using the key and properties defined by the spring boot application. Below see the syntax of how we can do this in our application see below;
e.g. :
spring:
application:
name: DemoProfiles
profile: local
3) For the name part of the application properties file we can have this in place see below;
a) application-local.properties: This properties file will be responsible to add the set of the environments for local.
b) application-dev.properties: This properties file will be responsible to add the set of the environments for development.
c) application-prod.properties: This properties file will be responsible to add the set environment for production.
d) application-test.properties: This properties file will be responsible to add the set of the environments for the test.
4) Now we will see how to make any profile active, using the application properties file in spring boot. To make any profile active in the application we have to make changes in the properties file commonly we have. Where we can set the profile we want to use for the current purpose. Below we will see how we can make use of this property we can make the necessary change;
e.g. :
spring. profiles.active=your_prfile_name
As you can see in the above line of syntax we are using the profile active property from the spring boot to make the profile active in the application. Let’s taken a look at the sample syntax for beginners to make use of this better see below;
e.g. :
spring. profiles.active=dev
Let’s take look at the steps need to perform while configuring the profile into the spring boot appellation, which is as follows
1) First we have to create the spring boot application from scratch by using a spring initializer after that we can import the change into the editor.
2) We do not need to add any specific dependency for this because spring boot provides this in the basic structure.
3) While using the annotation required for this we have to have an import statement at the place otherwise we will receive an error.
4) Let’s take a closer look at the file;
e.g. :
@SpringBootApplication
public class DemoProfileApplication {
public static void main(String[] args) throws IOException {
SpringApplication.run(DemoProfileApplication.class, args);
}
@Configuration
public class Config {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
@Bean
@Profile({"dev"})
public void configDev() throws IOException {
// logic goes hee ..///
}
@Bean
@Profile({"prod"})
public void configProd() throws IOException {
// logic goes hee ..///
}
}
}
5) Make changes to the application.properties file like below;
e.g. :
spring. profiles.active=local
6) Make this necessary changes into the spring boot application and try to run it.
7) Right-click on the application then you will see below logs on your console when the application is up and running, for reference, I am attaching the screenshot;
Output:
Conclusion
By the use of profiler on the spring boot application we can make the different environut settings as per our need and run the application local without impacting any changes to the production environment data. so it is a best practice to use profiling in the application. This is a standard followed by the developers to make the best use of the application.
Recommended Articles
This is a guide to Spring Boot Profiles. Here we discuss definition, syntax, and parameters, How do profiles work in Spring boot? examples with code implementation. You may also have a look at the following articles to learn more –