انتقل إلى المحتوى
أمين أبوزيد
JNR-012

إدارة الإعدادات

  • 5 درس
  • 2 ساعة تدريب (تقدير)
في هذه الصفحة 8 موضوعات

نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.

نظرة عامة

الموضوعات

  • application.yml
  • الـ Profiles
  • @ConfigurationProperties
  • التحقق (Validation)

تعلّم أولًا

2 ساعة تدريب (تقدير)

مخرجات التعلم

  1. Configure the application with application.yml and profile-specific files.
  2. Bind configuration to typed, immutable @ConfigurationProperties records.
  3. Validate configuration at startup so the app fails fast on bad values.
  4. Explain Spring's property-source precedence.

الدروس

  1. 01application.yml والـ Profiles
  2. 02إعدادات مُنمّطة بـ @ConfigurationProperties
  3. 03التحقق من الإعدادات
  4. 04أولوية مصادر الخصائص
  5. 05عرض عملي: ShopProperties مُنمّطة ومتحقَّق منها وتفشل مبكرًا

المختبر والتمارين

Lab

JNR-012-LAB1 · Shop settings

Outcomes: 1–4 · Time: ~2 h

  1. Create a ShopProperties record: currency, tax rate, page-size limits (default and max).
  2. Validate it with @Validated and constraints.
  3. Add application.yml plus application-dev.yml and application-prod.yml with different values.
  4. Inject ShopProperties into a service; no @Value in the code.
  5. 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)

IDOutcomeTask
JNR-012-EX011Add a dev profile that changes the log level and the port; start with it.
JNR-012-EX022Convert three @Value usages to one @ConfigurationProperties record.
JNR-012-EX032Map page-size-max in YAML to a Java record component; state the name rule.
JNR-012-EX043Add @Min(1) to the page size and set it to 0; read the failure message.
JNR-012-EX054Set the same property in application.yml, a profile file, an env var, and a command-line argument; predict, then check the winner.
JNR-012-EX064Decide where each lives: database password, tax rate, page-size limit, API token.
أظهر الحلول
  • EX01 application-dev.yml with server.port: 8081 and logging.level.com.shop: DEBUG; start with --spring.profiles.active=dev.
  • EX02 @ConfigurationProperties(prefix="shop") record ShopProperties(...); inject it where @Value fields 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-max and 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.

اختبار ذاتي لك: لا توجد درجات، ولا يُحفظ أو يُرسل أي شيء. اختر إجابة ثم تحقق منها، ويمكنك المحاولة مرة أخرى كما تشاء.

  1. 1Which file adds settings only for the dev profile?O1
  2. 2Choose a profile without editing files:O1
  3. 3Annotation to bind a group of settings to a type:O2
  4. 4Why a record for properties?O2
  5. 5page-size-max in YAML binds to the Java name:O2
  6. 6What does @Validated on a properties record enable?O3
  7. 7A property fails validation. The app:O3
  8. 8Which source wins?O4
  9. 9Highest priority among these:O4
  10. 10Where do database passwords belong?O4

الجزء العملي

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

اكتب للبحث في المستويات والمراحل والوحدات والدروس.