Configurazione esternalizzata e sovrascritture
Sovrascriva la configurazione tramite l'ambiente e la riga di comando.
Configurazione esternalizzata e sovrascritture è una lezione Spring Boot 4 Microservices & REST APIs gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Spring Boot 4 Microservices & REST APIs, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Spring Boot 4 Microservices & REST APIs include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
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
Impara Java con un tutor IA — gratis
Scrivi ed esegui vero codice nel tuo browser, ricevi aiuto istantaneo da un tutor IA disponibile 24/7, e riprendi da dove hai lasciato sul web o nell'app.
- Corsi
- 24
- Lezioni
- 93
Domande Frequenti
La lezione «Configurazione esternalizzata e sovrascritture» è gratuita?
Sì — il testo completo di «Configurazione esternalizzata e sovrascritture» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Spring Boot 4 Microservices & REST APIs, passa a CoddyKit PRO. Il corso Spring Boot 4 Microservices & REST APIs include 4 lezioni in totale.
Cosa imparerò in «Configurazione esternalizzata e sovrascritture»?
Sovrascriva la configurazione tramite l'ambiente e la riga di comando. Eserciti Spring Boot 4 Microservices & REST APIs con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Spring Boot 4 Microservices & REST APIs?
Non è richiesta alcuna esperienza precedente. Spring Boot 4 Microservices & REST APIs su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.
Quanto tempo richiede la lezione «Configurazione esternalizzata e sovrascritture»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Spring Boot 4 Microservices & REST APIs?
Sì. Ogni lezione Spring Boot 4 Microservices & REST APIs include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- application.properties e YAML
- Profili per gli ambienti
- Configurazione type-safe con @ConfigurationProperties
- Configurazione esternalizzata e sovrascritture