Skip to content
intermediatePhase ·

Profiles

Use Spring Profiles for environment-specific configuration.

30m
0 problems
Topic Progress0%

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

  1. Validation: Always validate input at the boundary
  2. Error Handling: Use structured error responses
  3. Logging: Log key events for debugging
  4. Testing: Unit, integration, and load tests
  5. Documentation: Keep docs updated with code changes

Practice Problems

0/3solved
Implement Spring Profiles

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
}
Spring Profiles Edge Cases

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, idempotency
Spring Profiles Testing Strategy

Write 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 injection

Quiz

1. How do you activate a Spring profile?

Question 1 options

2. What does @Profile("!test") mean?

Question 2 options

3. What is the primary purpose of Spring Profiles?

Question 3 options

4. What is a common mistake when implementing Spring Profiles?

Question 4 options

Flashcards

Question

How to activate a profile?

Answer

spring.profiles.active=dev (property, env var, or CLI)

Question

@Profile("!test") means?

Answer

Active in all profiles EXCEPT test

Question

What is Spring Profiles?

Answer

Spring Profiles is a key concept in backend development.

Question

When to use Spring Profiles?

Answer

Use Spring Profiles when building production systems that require reliability, scalability, and maintainability.

Question

Spring Profiles best practices

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