Interview question
How do you manage configuration across different environments in Spring Boot? Spring Boot में different environments के लिए configuration कैसे manage करते हैं?
Answer
Use Spring Profiles for environment-specific configuration. Externalize configuration using application-{profile}.yml or environment variables. Implement ConfigServer for centralized management in microservices.
// application.yml (base)
spring:
application:
name: app
profiles:
active: ${SPRING_PROFILES_ACTIVE:dev}
// application-dev.yml
spring:
datasource:
url: jdbc:mysql://localhost:3306/appdev
password: dev123
jpa:
hibernate:
ddl-auto: create-drop
cache:
type: simple
// application-prod.yml
spring:
datasource:
url: jdbc:mysql://mysql-prod:3306/appprod
password: ${DB_PASSWORD} # From environment
jpa:
hibernate:
ddl-auto: validate
cache:
type: redis
redis:
host: ${REDIS_HOST}
port: ${REDIS_PORT}
logging:
level:
root: WARN
// Programmatic Profile Usage
@Configuration
@Profile('prod')
public class ProdConfig {
@Bean
public DataSource prodDataSource() {
// Production database config
}
}
@Configuration
@Profile('dev')
public class DevConfig {
@Bean
public DataSource devDataSource() {
// H2 in-memory for testing
}
}
// Spring Cloud Config Server (Centralized)
// config-server-pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
// Config Server application.yml
spring:
cloud:
config:
server:
git:
uri: https://github.com/config-repo
searchPaths: configs/**
// Config Client
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
// bootstrap.yml (Config Client)
spring:
cloud:
config:
uri: http://config-server:8888
name: app
profile: prod
// Accessing Config
@Component
public class AppConfig {
@Value('${database.host}')
private String databaseHost;
@Value('${cache.ttl:3600}')
private int cacheTtl; // Default value
}
// Runtime Property Refresh
@RestController
@RefreshScope // Allows refresh without restart
public class ConfigController {
@Value('${feature.flag}')
private boolean featureFlag;
// POST /actuator/refresh to reload
}
// Environment Variables (Priority)
// 1. Command line arguments
// 2. System properties
// 3. OS environment variables
// 4. application-{profile}.yml
// 5. application.yml
// Running with different profiles
java -jar app.jar --spring.profiles.active=prod
java -Dspring.profiles.active=staging -jar app.jar
export SPRING_PROFILES_ACTIVE=prod && java -jar app.jarConfiguration Management:
Profiles का use करना:
- application-dev.yml
- application-prod.yml
- application-staging.yml
Activation:
spring.profiles.active=prod
Environment Variables:
${DATABASE_URL}
${API_KEY}
Config Server:
- Centralized configuration
- Multiple microservices
- Version control (Git)
- Runtime refresh
@RefreshScope:
- Restart के बिना config update
- POST /actuator/refresh
Priority Order:
1. Command line args
2. System properties
3. Environment variables
4. profile-specific YAML
5. Default YAML
Best Practice:
- Secrets: Environment variables
- Configuration: Config Server
- Defaults: application.ymlWas this answer clear?