
Play Store Application link β Spring Boot in 13 steps – App on Google Play
Github project link – https://github.com/kuldeep101990/Profiles_and_Environment_Management
Step 1: Setting Up a Spring Boot Project
Using Spring Initializr
- Go to https://start.spring.io/.
- Fill in the details:
- Project: Maven
- Language: Java
- Spring Boot: 3.x.x
- Dependencies: Spring Web
- Click Generate, and extract the downloaded ZIP file.
Import into IDE
- Open your IDE (e.g., IntelliJ or Eclipse).
- Import the extracted project as a Maven project.
Step 2: Using Profiles in Spring Boot
Spring Boot allows you to manage different configurations using profiles.
Create Profile-Specific Property Files
- application.properties (Default configuration):
server.port=8080
app.message=Default Profile
- application-dev.properties (Development configuration):
server.port=8081
app.message=Development Profile
- application-prod.properties (Production configuration):
server.port=8082
app.message=Production Profile
Activate a Profile
Profiles can be activated via:
- application.properties:
spring.profiles.active=dev
- Command Line:
java -Dspring.profiles.active=prod -jar demo-0.0.1-SNAPSHOT.jar
- Environment Variables:
Set the SPRING_PROFILES_ACTIVE
variable in your operating system or deployment environment.
Step 3: Access Profile-Specific Properties
Example: Display Active Profile
package com.example.demo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
@RestController
class ProfileController {
@Value("${app.message}")
private String message;
@GetMapping("/profile")
public String getProfileMessage() {
return "Active Profile Message: " + message;
}
}
Run the application and access http://localhost:<port>/profile
to see the message based on the active profile.
Step 4: Customizing Configurations with @ConfigurationProperties
Example: Create a Custom Configuration Class
package com.example.demo;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConfigurationProperties(prefix = "app")
public class AppConfig {
private String name;
private String description;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
Add Configuration to Properties Files
- application-dev.properties:
app.name=Demo App
app.description=Development Configuration
- application-prod.properties:
app.name=Demo App
app.description=Production Configuration
Access the Configuration in a Controller
package com.example.demo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
class ConfigController {
private final AppConfig appConfig;
public ConfigController(AppConfig appConfig) {
this.appConfig = appConfig;
}
@GetMapping("/config")
public String getConfig() {
return "App Name: " + appConfig.getName() + ", Description: " + appConfig.getDescription();
}
}
Run the application and access http://localhost:<port>/config
to see the profile-specific configuration.
Step 5: Using @Value for Inline Property Injection
Example: Inject Individual Properties
package com.example.demo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
class ValueController {
@Value("${app.name}")
private String appName;
@Value("${app.description}")
private String appDescription;
@GetMapping("/value")
public String getValueProperties() {
return "App Name: " + appName + ", Description: " + appDescription;
}
}
Conclusion
In this guide, we:
- Used profiles for environment-specific configurations.
- Activated profiles via multiple methods.
- Customized configurations using
@ConfigurationProperties
and@Value
.