Spring Profiles
Profile-Specific Configuration Files
application.properties ← Shared config
application-dev.properties ← Development
application-staging.properties ← Staging
application-prod.properties ← Production
Activation Methods
# Command line
java -jar app.jar --spring.profiles.active=prod
# Environment variable
SPRING_PROFILES_ACTIVE=prod
# In application.properties
spring.profiles.active=dev
# Programmatically
System.setProperty("spring.profiles.active", "dev");
Profile-Specific Beans
@Configuration
@Profile("dev")
public class DevConfig {
@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.H2)
.build();
}
}
@Configuration
@Profile("prod")
public class ProdConfig {
@Bean
public DataSource dataSource() {
return DataSourceBuilder.create()
.url("jdbc:postgresql://prod-db:5432/mydb")
.build();
}
}
Multi-Profile
@Service
@Profile("!test") // Active in all profiles except test
public class RealPaymentService implements PaymentService { }
@Service
@Profile("test")
public class MockPaymentService implements PaymentService { }
Spring Best Practices
Configuration
- Use properties over YAML for simple configs
- Externalize configuration
- Use profiles for environments
- Validate on startup
Bean Management
- Prefer constructor injection
- Use appropriate scope
- Implement lazy initialization
- Clean up resources
Security
- Use Spring Security
- Implement CSRF protection
- Use method-level security
- Log security events
Key Points
- Understanding Spring Profiles is essential for production systems
- Always consider scalability and maintainability
- Test thoroughly before deploying to production
- Monitor performance and set up alerting
Common Patterns
- Validation: Always validate input at the boundary
- Error Handling: Use structured error responses
- Logging: Log key events for debugging
- Testing: Unit, integration, and load tests
- Documentation: Keep docs updated with code changes
Practice Problems
Design and implement a solution for Spring Profiles in a backend system. Consider scalability, error handling, and production readiness.
Solution
// Spring Profiles implementation
// Key aspects: validation, error handling, logging, testing
public class SpringProfiles {
// Production-ready implementation
}Identify and handle edge cases for Spring Profiles. What happens under high load, with invalid input, or during failures?
Solution
// Edge case handling:
// 1. Null/empty input -> validation
// 2. High load -> rate limiting, queuing
// 3. Failures -> retries, circuit breaker
// 4. Concurrent access -> locks, idempotencyWrite a testing strategy for Spring Profiles. Include unit tests, integration tests, and performance tests.
Solution
// Test plan:
// - Unit: 80% coverage target
// - Integration: API contracts
// - Performance: latency, throughput
// - Chaos: failure injectionQuiz
1. How do you activate a Spring profile?
2. What does @Profile("!test") mean?
3. What is the primary purpose of Spring Profiles?
4. What is a common mistake when implementing Spring Profiles?
Flashcards
Question
How to activate a profile?
Click to reveal answer
Answer
spring.profiles.active=dev (property, env var, or CLI)
Question
@Profile("!test") means?
Click to reveal answer
Answer
Active in all profiles EXCEPT test
Question
What is Spring Profiles?
Click to reveal answer
Answer
Spring Profiles is a key concept in backend development.
Question
When to use Spring Profiles?
Click to reveal answer
Answer
Use Spring Profiles when building production systems that require reliability, scalability, and maintainability.
Question
Spring Profiles best practices
Click to reveal answer
Answer
Follow SOLID principles, write clean code, test thoroughly, document decisions, and monitor in production.
Revision Notes
Key Takeaways
- 1.Profiles separate config per environment
- 2.Activate: property, env var, or command line
- 3.@Profile on beans for environment-specific logic
- 4.Use ! for negation
Interview Tips
- •Know how to manage multiple environments
- •Explain profile-specific bean creation
Cheat Sheet
Spring Profiles
- Files: application-{profile}.properties/yml
- Activate: spring.profiles.active=prod
- Bean: @Profile("dev") or @Profile("!test")
- Multi: spring.profiles.active=dev,local