Project Implementation to Configure Active Profile in Spring Boot via Maven
Now, we will develop a simple Spring project that can configure the active profile of the Spring application.
Step 1: We will create a spring project using Spring STS IDE by adding below mentioned dependencies to the project.
Dependencies:
- Spring Web
- Lombok
- Spring Dev Tools
Once the Spring project has created, the file structure of the project will look like the image below.
Step 2: Open the pom.xml and put the below code for the configure active profiles in the Spring Boot application.
<profiles>
<profile>
<id>dev</id>
<properties>
<activatedProperties>dev</activatedProperties>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<activatedProperties>prod</activatedProperties>
</properties>
</profile>
</profiles>
Step 3: Open the application.properties file and put the below code the profiles configuration.
spring.application.name=spring-profiles-demo
spring.profiles.active=dev
Step 4: Open the application-dev.properties file and put the below code the development configuration of the application.
greeting.message=Hello (Development)
Step 5: Open the application-prod.properties file and put the below code the production configuration of the application.
greeting.message=Hello (Production)
Step 6: Create the new java class named as GreetingsMessage.
GreetingsMessage.java
package org.example.springprofilesdemo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
@Service
public class GreetingsMessage {
// Injecting greeting.message property value from application.properties
@Value("${greeting.message}")
private String greetingMessage;
// Method to greet the user with the configured message
public String greetUser(String name) {
return greetingMessage + " " + name + "!";
}
}
Step 7: Create a new java class named as GreetingsController.
GreetingsController.java
package org.example.springprofilesdemo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class GreetingsController {
// Autowiring GreetingsMessage bean
@Autowired
private GreetingsMessage greetingsService;
// Mapping GET requests to /greet/{name}
@GetMapping("/greet/{name}")
public String greet(@PathVariable String name) {
// Invoking the greetUser method of GreetingsMessage bean
return greetingsService.greetUser(name);
}
}
Step 8: Open the main class file and write the below code.
package org.example.springprofilesdemo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringProfilesDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringProfilesDemoApplication.class, args);
}
}
Step 9: Once completed the project, run the application as spring project. If runs successfully, then the run the application at port 8080. Refer the below image for the better understanding.
Output:
GET http://localhost:8080/greet/mahesh
Set the production of the application properties of the application.
Open application.properties change the profile.
spring.application.name=spring-profiles-demo
spring.profiles.active=prod
Again the run the application:
Output:
GET http://localhost:8080/greet/syam
If we follow the above steps, then we can successfully configure the active profiles in the Spring Boot application.
Configure Active Profile in SpringBoot via Maven
In Spring Boot, Profiles allows us to define sets of configurations for different environments or use cases. Active Profile in Spring Boot dynamically changes the behavior of the application based on the active profile. Maven is a popular tool that can be used to build the automation tool for Java projects. It can be utilized to manage the profiles in the Spring Boot applications effectively.
Contact Us