إدارة الإعدادات
في هذه الصفحة 8 موضوعات
نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.
نظرة عامة
الموضوعات
- application.yml
- الـ Profiles
- @ConfigurationProperties
- التحقق (Validation)
تعلّم أولًا
2 ساعة تدريب (تقدير)
مخرجات التعلم
- Configure the application with
application.ymland profile-specific files. - Bind configuration to typed, immutable
@ConfigurationPropertiesrecords. - Validate configuration at startup so the app fails fast on bad values.
- Explain Spring's property-source precedence.
الدروس
المختبر والتمارين
Lab
JNR-012-LAB1 · Shop settings
Outcomes: 1–4 · Time: ~2 h
- Create a
ShopPropertiesrecord: currency, tax rate, page-size limits (default and max). - Validate it with
@Validatedand constraints. - Add
application.ymlplusapplication-dev.ymlandapplication-prod.ymlwith different values. - Inject
ShopPropertiesinto a service; no@Valuein the code. - Prove precedence: override one value with an environment variable and a command-line argument.
Acceptance: invalid config prevents startup with a clear message; profiles switch values; no secrets in committed YAML.
Exercises (assessment: lab check plus precedence quiz)
| ID | Outcome | Task |
|---|---|---|
| JNR-012-EX01 | 1 | Add a dev profile that changes the log level and the port; start with it. |
| JNR-012-EX02 | 2 | Convert three @Value usages to one @ConfigurationProperties record. |
| JNR-012-EX03 | 2 | Map page-size-max in YAML to a Java record component; state the name rule. |
| JNR-012-EX04 | 3 | Add @Min(1) to the page size and set it to 0; read the failure message. |
| JNR-012-EX05 | 4 | Set the same property in application.yml, a profile file, an env var, and a command-line argument; predict, then check the winner. |
| JNR-012-EX06 | 4 | Decide where each lives: database password, tax rate, page-size limit, API token. |
أظهر الحلول
- EX01
application-dev.ymlwithserver.port: 8081andlogging.level.com.shop: DEBUG; start with--spring.profiles.active=dev. - EX02
@ConfigurationProperties(prefix="shop") record ShopProperties(...); inject it where@Valuefields were. - EX03
pageSizeMax; kebab-case or camelCase in the file, relaxed binding matches it. - EX04 Startup fails with a binding or validation error naming
shop.page-size-maxand the constraint. - EX05 Command line wins, then the environment variable, then the profile file, then the base file.
- EX06 Password and token: environment or secret store. Tax rate and page size: committed YAML (non-secret) with profile overrides.
الاختبار
Outcomes: O1 YAML and profiles · O2 typed properties · O3 validation · O4 precedence.
اختبار ذاتي لك: لا توجد درجات، ولا يُحفظ أو يُرسل أي شيء. اختر إجابة ثم تحقق منها، ويمكنك المحاولة مرة أخرى كما تشاء.
الجزء العملي
Six exercises JNR-012-EX01 to EX06 plus the lab (with the precedence check in step 5). Pass: quiz at least 8/10, lab acceptance met, EX05 predicted correctly.
أخطاء شائعة
@Value scattered everywhere; secrets in application.yml.
التقييم
Lab check plus precedence quiz.
الصلة بالمسار الوظيفي
Deployment configuration (JNR-052).