Spring Boot Profiles in Practice
Spring Boot mein, PROFILES ko PRACTICALLY APPLY karne ka MOST COMMON PATTERN hai — application-{profile}.properties files (application-dev.properties, application-prod.properties). spring.profiles.active property (environment variable, command-line arg, ya main application.properties mein) DECIDE karta hai KAUN SA profile ACTIVE hai.
ACTIVE profile ki properties, DEFAULT application.properties ke UPAR OVERRIDE hoti hain — SIRF WO PROPERTIES jo profile-specific file mein DEFINED hain, OVERRIDE hoती hain; BAAKI SAB, DEFAULT file se hi AATI hain (MERGE hota hai, REPLACE nahi).
# application.properties (COMMON, saare profiles ke liye):
spring.application.name=bookstore
# application-dev.properties:
spring.datasource.url=jdbc:h2:mem:testdb
logging.level.com.example=DEBUG
# application-prod.properties:
spring.datasource.url=jdbc:postgresql://prod-server/bookstore
logging.level.com.example=WARN
# Activate karna:
java -jar app.jar --spring.profiles.active=prod
# Ya environment variable se (Docker/Kubernetes mein COMMON):
export SPRING_PROFILES_ACTIVE=prod- application-{profile}.properties = profile-specific overrides
- spring.profiles.active = KAUN SA profile ACTIVE hai, decide karta hai
- Profile properties, DEFAULT properties ke UPAR MERGE hoती hain (replace nahi)
Agar KOI PROFILE explicitly ACTIVATE nahi kiya jaata, Spring Boot "default" profile use karta hai — application-default.properties (agar EXIST karti hai) ya SIRF application.properties LOAD hoti hai.
Tests mein, @ActiveProfiles("test") annotation se SPECIFIC profile ACTIVATE ki ja sakti hai — application-test.properties (typically H2 in-memory database, MOCK external services) TESTS ke liye USE hoती hai, PRODUCTION database ko TOUCH kiye BINA.
@SpringBootTest
@ActiveProfiles("test")
class OrderServiceTest {
// application-test.properties use hogi is test class ke liye
}