Topic 4: – 5 steps of Profiles and Environment Management in Spring Boot

image 2

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

  1. Go to https://start.spring.io/.
  2. Fill in the details:
    • Project: Maven
    • Language: Java
    • Spring Boot: 3.x.x
    • Dependencies: Spring Web
  3. 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

  1. application.properties (Default configuration):
server.port=8080
app.message=Default Profile

  1. application-dev.properties (Development configuration):
server.port=8081
app.message=Development Profile

  1. application-prod.properties (Production configuration):
server.port=8082
app.message=Production Profile

Activate a Profile

Profiles can be activated via:

  1. application.properties:
spring.profiles.active=dev

  1. Command Line:
java -Dspring.profiles.active=prod -jar demo-0.0.1-SNAPSHOT.jar

  1. 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

  1. application-dev.properties:
app.name=Demo App
app.description=Development Configuration

  1. 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:

  1. Used profiles for environment-specific configurations.
  2. Activated profiles via multiple methods.
  3. Customized configurations using @ConfigurationProperties and @Value.

Leave a Reply

Your email address will not be published. Required fields are marked *