SPRING BOOT SERIES - LẮP RÁP

application.properties Không Bị Ignore: Nó Đã Thua Một Nguồn Cao Hơn

ENVIRONMENT - XANH = TẦNG PHÓNG TOEnvironment giữ một danh sách PropertySource. Resolver trả giá trị đầu tiên tìm thấy theo thứ tự ưu tiên thực tế.

Trong file

spring.datasource.url=jdbc:postgresql://localhost/dev vẫn được load cùng origin.

First match wins ở resolverCommand line, system property hoặc environment variable ở vị trí cao hơn có cùng key nên vòng lặp dừng trước file.

Trong runtime

SPRING_DATASOURCE_URL được relaxed binding nhận thành cùng configuration property và thắng file.

Mức 1 - nạp config data

ConfigDataEnvironmentPostProcessor

Chạy sớm để tìm, load và apply config documents

  1. postProcessEnvironment()Chạy ở order HIGHEST_PRECEDENCE + 10.
  2. ConfigDataEnvironment.processAndApply()Resolve location, profile, import và document.
  3. Packaged config dataClasspath application.* là lớp mặc định trong nhóm file.
  4. External cao hơn packagedFile ngoài jar override file bên trong theo location order.
  5. Profile-specific cao hơn genericapplication-prod.* override application.* cùng nhóm.
  6. Origin được giữMỗi value có thể mang resource và line để diagnostic.
Mức 2 - chọn nguồn thắng

PropertySourcesPropertyResolver.getProperty()

Danh sách cao -> thấp, dừng ở non-null đầu tiên

  1. MutablePropertySourcesEnvironment giữ ordered collection, không giữ một file duy nhất.
  2. for (PropertySource source : propertySources)Resolver đi theo đúng thứ tự collection.
  3. source.getProperty(key)Hỏi mỗi nguồn có key hiện tại hay không.
  4. Giá trị non-null đầu tiên thắngConvert type, resolve nested placeholder rồi return; nguồn thấp hơn không được hỏi.
  5. Null không che nguồn thấp hơnVí dụ null trong SPRING_APPLICATION_JSON được coi như missing.
Mức 3 - binding và chẩn đoán

Binder + Actuator Origin

Relaxed name và effective value là hai lớp cần xem cùng nhau

  1. High-priority runtime sourcesCommand line cao hơn JSON, system properties, environment variables và config data.
  2. ConfigurationPropertySourcesAdapt PropertySource để Binder hỗ trợ canonical/relaxed names.
  3. SPRING_DATASOURCE_URLSystem environment source và Binder có thể nối tên này với spring.datasource.url.
  4. /actuator/env/{property}Xem effective value, origin và các nguồn cạnh tranh nếu endpoint được expose/authorized.
  5. Trace config loaderBật log org.springframework.boot.context.config=trace để thấy location/document được load.

Phép kiểm: log tên PropertySource theo thứ tự, gọi environment.getProperty(key), mở /actuator/env/{key} trong môi trường an toàn và kiểm tra env vars, command line, system properties của đúng process/container. Không chỉ mở file trong repo.

Làm rõ quyền override

  • Ghi document precedence khi deployChỉ rõ key nào platform được quyền override.
  • Dùng @ConfigurationProperties có validationFail fast khi value thiếu hoặc sai type.
  • Giữ secret ngoài artifactNhưng audit origin và quyền truy cập diagnostic endpoint.

Khi KHÔNG được kết luận

  • File không thắng nghĩa là file không được loadOrigin thấp hơn vẫn tồn tại nhưng resolver đã dừng ở nguồn cao.
  • Environment variable luôn tên giống keySystem environment và Binder có quy tắc chuyển tên; test đúng key canonical.
  • Expose toàn bộ /env ở production là an toànEndpoint có thể lộ cấu trúc và metadata; cần authorization và sanitization.