0Pricing
Spring Boot 4 Microservices & REST APIs · Pelajaran

Profil untuk Lingkungan

Ganti konfigurasi untuk setiap lingkungan menggunakan profil.

Profil untuk Lingkungan adalah pelajaran Spring Boot 4 Microservices & REST APIs gratis di CoddyKit. Ini adalah pelajaran 2 dari 4. Kamu bisa membaca pelajaran lengkapnya di bawah secara gratis — lalu praktikkan langsung di browser dengan editor kode bawaan dan tutor AI 24/7. Ini adalah bagian dari jalur belajar Spring Boot 4 Microservices & REST APIs, dan progresmu tersinkronisasi di web dan aplikasi CoddyKit. Kursus Spring Boot 4 Microservices & REST APIs mencakup 4 pelajaran total.

Bagian dari pelajaran ini belum diterjemahkan dan ditampilkan dalam bahasa Inggris.

Why Profiles Exist

Real applications run in several environments — local, dev, staging, production — each needing different datasources, log levels, and feature toggles. Profiles let one codebase carry many configurations and activate the right set at runtime.

A profile is just a named label. Beans and property files can be tagged so they apply only when that profile is active.

Profile-Specific Property Files

Boot automatically loads application-{profile}.yml on top of the base application.yml. The profile file overrides matching keys while inheriting everything else.

# application.yml (base, always loaded)
spring:
  jpa:
    show-sql: false

# application-dev.yml (loaded when dev active)
spring:
  jpa:
    show-sql: true
  datasource:
    url: jdbc:h2:mem:devdb

Activating a Profile

Set spring.profiles.active to choose which profiles run. You can do this in a file, as an environment variable, or on the command line.

# Command line
java -jar app.jar --spring.profiles.active=prod

# Environment variable
export SPRING_PROFILES_ACTIVE=prod

# Multiple profiles
java -jar app.jar --spring.profiles.active=prod,metrics

Conditional Beans with @Profile

@Profile on a bean or configuration class makes it register only when that profile is active. This is ideal for swapping implementations between environments.

@Configuration
public class PaymentConfig {
    @Bean
    @Profile("prod")
    public PaymentGateway liveGateway() {
        return new StripeGateway();
    }

    @Bean
    @Profile("!prod")
    public PaymentGateway fakeGateway() {
        return new InMemoryGateway();
    }
}

Profile Expressions

The @Profile value supports simple logic: ! for NOT, & for AND, and | for OR. Group expressions with parentheses.

@Profile("!prod")                // anything except prod
@Profile("prod & metrics")       // both active
@Profile("dev | test")           // either active
@Profile("prod & (eu | us)")     // grouped

The Default Profile

When no profile is active, Spring enables the implicit default profile. You can target it explicitly with @Profile("default") for fallback beans used during plain local runs.

@Bean
@Profile("default")
public DataSeeder localSeeder() {
    return new SampleDataSeeder();
}

Profile Groups

A profile group bundles several profiles under one name, so activating the group activates them all. Great for composing cross-cutting concerns.

spring:
  profiles:
    group:
      production:
        - prod
        - metrics
        - swagger-off
# Now: --spring.profiles.active=production turns on all three

Including Profiles

spring.profiles.active sets the primary profiles, while spring.profiles.include adds extra ones unconditionally, regardless of which primary profile is selected.

spring:
  profiles:
    include:
      - common-logging
      - audit

Inspecting Active Profiles at Runtime

You can read active profiles from the Environment bean. This is useful for guard logic or for logging which configuration is live.

@Component
public class StartupLogger {
    private final Environment env;
    public StartupLogger(Environment env) { this.env = env; }

    @PostConstruct
    void log() {
        System.out.println("Active: "
            + String.join(",", env.getActiveProfiles()));
    }
}

Profiles in Tests

In integration tests, annotate the class with @ActiveProfiles to force a specific configuration, such as an in-memory database profile.

@SpringBootTest
@ActiveProfiles("test")
class OrderServiceTest {
    // loads application-test.yml + @Profile("test") beans
}

Common Pitfalls

Watch for these mistakes:

  • Forgetting to set spring.profiles.active in production, so dev defaults leak in
  • Putting secrets in application-dev.yml and committing it
  • Overusing @Profile where a simple property toggle would do

Quick Check

Test your grasp of profile expressions.

Recap

Profiles tailor one codebase to many environments.

  • application-{profile}.yml overrides base config
  • Activate via spring.profiles.active (file, env var, or CLI)
  • @Profile conditionally registers beans, with !, &, | expressions
  • Profile groups bundle related profiles
  • Use @ActiveProfiles in tests

Pertanyaan yang Sering Diajukan

Apakah pelajaran “Profil untuk Lingkungan” gratis?

Ya — teks lengkap “Profil untuk Lingkungan” gratis dibaca di sini di web. Untuk praktiknya secara interaktif (editor kode bawaan dan tutor AI 24/7) dan buka sisa kursus Spring Boot 4 Microservices & REST APIs, upgrade ke CoddyKit PRO. Kursus Spring Boot 4 Microservices & REST APIs mencakup 4 pelajaran total.

Apa yang akan aku pelajari di “Profil untuk Lingkungan”?

Ganti konfigurasi untuk setiap lingkungan menggunakan profil. Kamu berlatih Spring Boot 4 Microservices & REST APIs dengan kode praktik yang langsung kamu jalankan di browser, dan tutor AI 24/7 menjawab pertanyaanmu saat kamu mengerjakan pelajaran ini.

Apakah aku perlu pengalaman untuk memulai Spring Boot 4 Microservices & REST APIs?

Tidak diperlukan pengalaman sebelumnya. Spring Boot 4 Microservices & REST APIs di CoddyKit dirancang untuk pemula hingga pelajar tingkat lanjut, jadi kamu bisa memulai di sini atau dari awal dan belajar sesuai kecepatan kamu sendiri. Ini adalah pelajaran 2 dari 4.

Berapa lama pelajaran “Profil untuk Lingkungan” memakan waktu?

Sebagian besar pelajaran CoddyKit memakan waktu sekitar 5–10 menit. Setiap pelajaran ringkas dan interaktif, jadi kamu membuat kemajuan stabil dan melanjutkan dari tempat kamu tinggalkan di web dan aplikasi.

Bisakah aku menulis dan menjalankan kode dalam pelajaran Spring Boot 4 Microservices & REST APIs ini?

Ya. Setiap pelajaran Spring Boot 4 Microservices & REST APIs menyertakan editor kode bawaan, jadi kamu menulis dan menjalankan kode nyata langsung di browser dan mendapatkan umpan balik AI instan — tidak diperlukan penyiapan lokal.

Semua pelajaran dalam kursus ini

  1. application.properties dan YAML
  2. Profil untuk Lingkungan
  3. Konfigurasi Aman Tipe dengan @ConfigurationProperties
  4. Konfigurasi Eksternal dan Penimpaan
← Kembali ke Spring Boot 4 Microservices & REST APIs