0Pricing
Spring Boot 4 Microservices & REST APIs · Leçon

Configuration externalisée et remplacement

Remplacez la configuration via l'environnement et la ligne de commande.

Configuration externalisée et remplacement est une leçon Spring Boot 4 Microservices & REST APIs gratuite sur CoddyKit. Ceci est la leçon 4 sur 4. Tu peux lire la leçon complète ci-dessous gratuitement — puis la pratiquer en direct dans le navigateur avec un éditeur de code intégré et un tuteur IA 24/7. Elle fait partie du parcours d'apprentissage Spring Boot 4 Microservices & REST APIs, et ta progression se synchronise sur le web et l'application CoddyKit. Le cours Spring Boot 4 Microservices & REST APIs comprend 4 leçons au total.

Certaines parties de cette leçon n'ont pas encore été traduites et s'affichent en anglais.

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 application files
  • Defaults in code / @Value fallbacks

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.com

Overriding 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=s3cret

Relaxed 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.port

Quick Check

Test your understanding of precedence.

Recap

Externalized config makes one artifact run everywhere.

  • Many sources merge into one Environment by 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/env to debug where a value originated

Questions Fréquemment Posées

La leçon « Configuration externalisée et remplacement » est-elle gratuite ?

Oui — le texte complet de « Configuration externalisée et remplacement » est gratuit à lire ici sur le web. Pour la pratiquer de manière interactive (un éditeur de code intégré et un tuteur IA 24/7) et déverrouiller le reste du cours Spring Boot 4 Microservices & REST APIs, passe à CoddyKit PRO. Le cours Spring Boot 4 Microservices & REST APIs comprend 4 leçons au total.

Qu'est-ce que j'apprendrai dans « Configuration externalisée et remplacement » ?

Remplacez la configuration via l'environnement et la ligne de commande. Tu pratiques Spring Boot 4 Microservices & REST APIs avec du code pratique que tu exécutes directement dans le navigateur, et un tuteur IA 24/7 répond à tes questions au fur et à mesure que tu avances dans la leçon.

Dois-je avoir de l'expérience pour commencer Spring Boot 4 Microservices & REST APIs ?

Aucune expérience préalable n'est requise. Spring Boot 4 Microservices & REST APIs sur CoddyKit est structuré pour les débutants jusqu'aux apprenants avancés, donc tu peux commencer ici ou depuis le début et avancer à ton rythme. Ceci est la leçon 4 sur 4.

Combien de temps prend la leçon « Configuration externalisée et remplacement » ?

La plupart des leçons CoddyKit prennent environ 5–10 minutes. Chacune est courte et interactive, tu progresses régulièrement et tu repiques exactement où tu t'es arrêté sur le web et l'app.

Peux-tu écrire et exécuter du code dans cette leçon Spring Boot 4 Microservices & REST APIs ?

Oui. Chaque leçon Spring Boot 4 Microservices & REST APIs inclut un éditeur de code intégré, tu écris et exécutes du vrai code directement dans ton navigateur et tu reçois des retours IA instantanés — aucune configuration locale requise.

Toutes les leçons de ce cours

  1. application.properties et YAML
  2. Profils pour les environnements
  3. Configuration avec vérification des types grâce à @ConfigurationProperties
  4. Configuration externalisée et remplacement
← Retour à Spring Boot 4 Microservices & REST APIs