🎭
Configuration

Profiles

Dev, Test, Prod Environments
💡 Spring Profiles ek ACTOR ke different COSTUMES jaise hain — same application (actor), lekin environment (scene) ke hisaab se DIFFERENT configuration (costume) pehनता hai — dev mein "casual" (H2 database, verbose logging), prod mein "formal" (real database, minimal logging).

@Profile annotation se beans ko SPECIFIC environments ke liye restrict kiya ja sakta hai — sirf TAB active hoti hain jab matching profile ACTIVE ho. spring.profiles.active=dev (application.properties mein ya environment variable se) SET karta hai KAUN SA profile active hai.

Real-world use: dev profile mein lightweight H2 in-memory database (fast startup, koi setup nahi), prod profile mein actual PostgreSQL/MySQL connection. Isse SAME codebase, DIFFERENT environments mein, bina code change ke, chal sakta hai.

@Profile("dev")
@Bean
public DataSource devDataSource() {
    return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2).build();
}

@Profile("prod")
@Bean
public DataSource prodDataSource() {
    HikariConfig config = new HikariConfig();
    config.setJdbcUrl("jdbc:postgresql://prod-server/mydb");
    return new HikariDataSource(config);
}

// Activate karna:
// java -jar app.jar --spring.profiles.active=prod
🎭
Spring Profiles ek ACTOR ke different COSTUMES jaise hain — same application (actor), lekin environment (scene) ke hisaab se DIFFERENT configuration (costume) pehनता hai — dev mein "casual" (H2 database, verbose logging), prod mein "formal" (real database, minimal logging).
1 / 2
⚡ Quick Recap
  • @Profile("name") = bean sirf us profile ke active hone par create hoti hai
  • spring.profiles.active property se KAUN SA profile chalाना hai decide hota hai
  • Common profiles: dev, test, prod
Is page mein (2 subtopics)

@Profile annotation se DIFFERENT beans DIFFERENT environments (dev, test, prod) ke liye define kiye ja sakte hain — jaise dev mein H2 in-memory database, prod mein actual PostgreSQL. spring.profiles.active property se ACTIVE profile set hota hai.

@Configuration
public class DataSourceConfig {
    @Bean
    @Profile("dev")
    public DataSource devDataSource() {
        return new EmbeddedDatabaseBuilder().setType(H2).build();
    }

    @Bean
    @Profile("prod")
    public DataSource prodDataSource() {
        return new HikariDataSource();  // real production database
    }
}

// application.properties mein:
// spring.profiles.active=dev

Spring Boot mein, HAR profile ke liye ALAG properties file bhi ban sakti hai — application-dev.properties, application-prod.properties. ACTIVE profile ke hisaab se, us file ke values MAIN application.properties ke values ko OVERRIDE kar dete hain.

  • application.properties = common/default settings
  • application-dev.properties = dev-specific overrides
  • application-prod.properties = prod-specific overrides