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
postProcessEnvironment()Chạy ở orderHIGHEST_PRECEDENCE + 10.ConfigDataEnvironment.processAndApply()Resolve location, profile, import và document.- Packaged config dataClasspath
application.*là lớp mặc định trong nhóm file. - External cao hơn packagedFile ngoài jar override file bên trong theo location order.
- Profile-specific cao hơn generic
application-prod.*overrideapplication.*cùng nhóm. - 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
MutablePropertySourcesEnvironment giữ ordered collection, không giữ một file duy nhất.for (PropertySource source : propertySources)Resolver đi theo đúng thứ tự collection.source.getProperty(key)Hỏi mỗi nguồn có key hiện tại hay không.- 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.
- 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
- High-priority runtime sourcesCommand line cao hơn JSON, system properties, environment variables và config data.
ConfigurationPropertySourcesAdapt PropertySource để Binder hỗ trợ canonical/relaxed names.SPRING_DATASOURCE_URLSystem environment source và Binder có thể nối tên này vớispring.datasource.url./actuator/env/{property}Xem effective value, origin và các nguồn cạnh tranh nếu endpoint được expose/authorized.- 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
@ConfigurationPropertiescó 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.