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

الإعدادات (Configuration)

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

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

نظرة عامة

الموضوعات

  • Properties
  • YAML
  • الملفات الشخصية (Profiles)
  • متغيرات البيئة
  • الإعدادات الخارجية (Externalized configuration)

تعلّم أولًا

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

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

  1. Externalize configuration with properties files, YAML, and environment variables.
  2. Apply the twelve-factor principle of separating config from code.
  3. Explain configuration precedence and per-environment profiles.
  4. Keep secrets out of source control.

الدروس

  1. 01لماذا نُخرج الإعدادات من الكود11 د قراءة
  2. 02Properties وYAML11 د قراءة
  3. 03متغيرات البيئة وخصائص النظام11 د قراءة
  4. 04الـ Profiles وأولوية الإعدادات11 د قراءة
  5. 05الأسرار لا مكان لها في Git11 د قراءة
  6. 06عرض عملي: ملف JAR واحد وثلاث بيئات11 د قراءة

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

Lab

JNR-007-LAB1 · Configurable CLI

Outcomes: 1–4 · Time: ~1.5 h Read currency, tax rate, and an API key from a file in the Shop CLI. Allow each to be overridden by an environment variable. Commit a .env.example instead of real values and add .env to .gitignore. Acceptance: the same JAR behaves differently per environment; no secrets in Git; missing required values give a clear error; the README states the precedence order.

Exercises (assessment: quiz on precedence plus lab check; six exercises)

IDOutcomeTask
JNR-007-EX011Move three hard-coded values (currency, tax rate, shipping fee) into application.properties and load them.
JNR-007-EX021Write the same settings in YAML, and mark the indentation rule.
JNR-007-EX031Override one value with an environment variable and another with -D. Show both commands.
JNR-007-EX042Identify the twelve-factor violation: if (System.getenv("ENV").equals("prod")) fee = 5;. Fix it.
JNR-007-EX053Given file=0.19, profile file=0.16, env var=0.07, command line absent, which value wins? Write the order.
JNR-007-EX064A teammate committed .env with an API key. List the steps to fix it.
أظهر الحلول
  • EX01 Properties p = new Properties(); p.load(in); and p.getProperty("shop.currency").
  • EX02 shop: then two-space indented keys; no tabs.
  • EX03 SHOP_TAX_RATE=0.07 java -jar shop-cli.jar and java -Dshop.currency=USD -jar shop-cli.jar.
  • EX04 Environment-specific code. Move the fee into configuration (shop.shipping-fee) and set it per environment.
  • EX05 0.07 (environment variable). Order: file < profile file < environment variable < command line.
  • EX06 Revoke and replace the key; git rm --cached .env; add .env to .gitignore; commit .env.example; clean history if the policy needs it.
  • Outcome 3 is also covered by quiz Q06–Q07; outcome 2 by EX04 and quiz Q02.

الاختبار

Outcomes: O1 externalize config · O2 twelve-factor · O3 precedence and profiles · O4 secrets.

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

  1. 1Configuration is best kept:O1
  2. 2The twelve-factor rule for config says:O2
  3. 3Which reads an environment variable?O1
  4. 4How do you set a system property?O1
  5. 5In YAML, nesting is shown by:O1
  6. 6File says 0.19, environment variable says 0.07. The usual winner is:O3
  7. 7A profile is:O3
  8. 8Where should a real API key live?O4
  9. 9What do you commit for local setup?O4
  10. 10A key was committed by mistake. First, you:O4

الجزء العملي

Six exercises JNR-007-EX01 to EX06 plus the lab check (same JAR, different behavior). Pass: at least 8 of 10 quiz answers including the precedence question, five exercises correct, and no secrets in the repository.

أخطاء شائعة

Committing .env; environment-specific if statements in code.

التقييم

Quiz on precedence plus lab check.

الصلة بالمسار الوظيفي

Spring configuration (JNR-012) and secure configuration (JNR-042).

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