Konfigurasi Eksternal dan Penimpaan
Timpa konfigurasi melalui lingkungan dan baris perintah.
Konfigurasi Eksternal dan Penimpaan adalah pelajaran Spring Boot 4 Microservices & REST APIs gratis di CoddyKit. Ini adalah pelajaran 4 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.
The 12-Factor Idea
A core principle of portable apps is storing configuration in the environment, not in code. The same artifact should run anywhere, with behavior changed only by external config.
Spring Boot embraces this: a single jar reads from many sources and merges them with a well-defined precedence.
Many Sources, One Environment
Boot collects configuration from numerous property sources — files, environment variables, command-line args, and more — into one unified Environment.
- Each source contributes key/value pairs
- Higher-priority sources win on conflicts
- Your code never knows or cares where a value came from
Precedence Order (High to Low)
When the same key appears in several sources, the highest-priority source wins. A simplified, commonly used ordering:
- Command-line arguments
- OS environment variables
- Profile-specific
application-{profile}files - Base
applicationfiles - Defaults in code /
@Valuefallbacks
This is why a CLI flag can override anything baked into the jar.
Overriding with Command-Line Args
Pass --key=value after the jar. These have very high precedence and are ideal for one-off overrides or container entrypoints.
java -jar app.jar \
--server.port=9090 \
--spring.profiles.active=prod \
--app.mail.host=smtp.prod.example.comOverriding with Environment Variables
Environment variables use an UPPER_SNAKE_CASE form of the property name: dots and dashes become underscores. This is the standard way to inject config in containers.
# property: app.mail.host
export APP_MAIL_HOST=smtp.prod.example.com
# property: server.port
export SERVER_PORT=9090
# property: spring.datasource.password
export SPRING_DATASOURCE_PASSWORD=s3cretRelaxed Binding for Env Vars
Because env var names are constrained, Spring maps them back to canonical property names. APP_MAIL_MAX_RETRIES resolves to app.mail.maxRetries. This lets you set any property from the environment.
Injecting Secrets at Deploy Time
Keep secrets out of the jar; reference them with placeholders and let the platform supply real values via env vars. The committed file holds only a safe default or none.
spring:
datasource:
url: ${DB_URL}
username: ${DB_USER:app}
password: ${DB_PASSWORD}External Config Files
Point Boot at config outside the jar with spring.config.location or add extra files with spring.config.additional-location. Operators can drop a file next to the deployment without rebuilding.
java -jar app.jar \
--spring.config.additional-location=file:/etc/myapp/Importing Config
Modern Boot supports spring.config.import to pull in extra documents, optional files, or even config server entries, merged with the normal precedence rules.
spring:
config:
import:
- optional:file:/etc/myapp/override.yml
- configserver:Random Values
The random property source generates values on demand — handy for test data or ephemeral identifiers, though not for stable secrets.
app:
instance-id: ${random.uuid}
jitter-ms: ${random.int(0,500)}Debugging Where a Value Came From
When a value is not what you expect, inspect the merged Environment. The actuator /env endpoint lists every property source and the winning value, making precedence problems easy to diagnose.
# requires spring-boot-starter-actuator + exposure
GET /actuator/env/server.portQuick Check
Test your understanding of precedence.
Recap
Externalized config makes one artifact run everywhere.
- Many sources merge into one
Environmentby precedence - CLI args > env vars > profile files > base files > code defaults
- Env vars use UPPER_SNAKE_CASE relaxed binding
- Inject secrets via placeholders at deploy time
- Use
/actuator/envto debug where a value originated
Belajar Java dengan tutor AI — gratis
Tulis dan jalankan kode asli di browser kamu, dapatkan bantuan instan dari tutor AI 24/7, dan lanjutkan di mana kamu tinggalkan di web atau aplikasi.
- Kursus
- 24
- Pelajaran
- 93
Pertanyaan yang Sering Diajukan
Apakah pelajaran “Konfigurasi Eksternal dan Penimpaan” gratis?
Ya — teks lengkap “Konfigurasi Eksternal dan Penimpaan” 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 “Konfigurasi Eksternal dan Penimpaan”?
Timpa konfigurasi melalui lingkungan dan baris perintah. 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 4 dari 4.
Berapa lama pelajaran “Konfigurasi Eksternal dan Penimpaan” 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
- application.properties dan YAML
- Profil untuk Lingkungan
- Konfigurasi Aman Tipe dengan @ConfigurationProperties
- Konfigurasi Eksternal dan Penimpaan