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

انعكاس التحكم وحقن الاعتماديات (IoC وDI)

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

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

نظرة عامة

الموضوعات

  • انعكاس التحكم (Inversion of Control)
  • حقن الاعتماديات (Dependency injection)
  • الحقن عبر المُنشئ (Constructor injection)
  • دورة حياة الـ Bean

تعلّم أولًا

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

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

  1. Explain inversion of control and dependency injection, first without Spring.
  2. Use constructor injection and explain why it beats field injection.
  3. Resolve ambiguity between multiple implementations with @Primary and @Qualifier.
  4. Describe the bean lifecycle (instantiation, injection, initialization, destruction).

الدروس

  1. 01الربط اليدوي: المشكلة
  2. 02انعكاس التحكم (Inversion of control)
  3. 03الحقن عبر المُنشئ (Constructor injection)
  4. 04تنفيذات متعددة
  5. 05دورة حياة الـ Bean والاستدعاءات
  6. 06الاعتماديات الدائرية وكيف نتجنبها
  7. 07عرض عملي: ربط Shop Core يدويًا ثم باستخدام Spring
  8. 08عرض عملي: تشخيص فشل بدء التشغيل بسبب Bean غامض

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

Lab

JNR-009-LAB1 · Inject Shop Core

Outcomes: 1–4 · Time: ~2 h

  1. Take Shop Core's services (pricing, orders, payments) and the pricing strategies (for example RegularPricing, DiscountPricing).
  2. Register them as beans and inject through constructors.
  3. Add two PaymentProvider implementations; choose the one for orders with @Qualifier.
  4. Add a @PostConstruct log line to one bean and watch the order in the startup log.

Acceptance: no new for collaborators inside services; no field injection; the app starts with two payment providers present.

Exercises (assessment: explain a DI wiring diagram and fix an ambiguous-bean failure)

IDOutcomeTask
JNR-009-EX011Without Spring, refactor a class that does new EmailSender() so the sender is passed in; write a test using a fake.
JNR-009-EX021Explain IoC and DI in your own words and name who creates objects in each style.
JNR-009-EX032Convert a field-injected service to constructor injection; list two benefits.
JNR-009-EX043Given two beans of one interface and a startup failure, fix it with @Primary, then with @Qualifier.
JNR-009-EX054Add @PostConstruct and @PreDestroy logs to a bean and predict the order relative to the constructor.
JNR-009-EX062Break a circular dependency between two services by extracting a third class.
JNR-009-EX071Draw the wiring diagram for OrderController → OrderService → PaymentProvider, PricingStrategy, and mark who creates each.
أظهر الحلول
  • EX01 class Notifier { private final EmailSender sender; Notifier(EmailSender s){this.sender=s;} } with new Notifier(new FakeEmailSender()) in the test.
  • EX02 IoC: the framework, not your class, creates and connects objects. DI: dependencies are passed in (constructor). By hand, main or the class creates; with Spring, the container.
  • EX03 Benefits: final fields and complete objects; easy new in tests; dependencies visible in the signature.
  • EX04 @Primary on the default implementation. Or @Qualifier("paypal") on the constructor parameter.
  • EX05 Constructor, injection, @PostConstruct, (use), @PreDestroy on shutdown.
  • EX06 Move the shared logic into StockPolicy; both services depend on it; no cycle.
  • EX07 Spring creates all four beans; the controller gets the service, the service gets the provider and strategy. The wiring-diagram part of the assessment is covered by this exercise and the lab acceptance.

الاختبار

Outcomes: O1 IoC and DI · O2 constructor injection · O3 ambiguity · O4 bean lifecycle.

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

  1. 1Inversion of control means:O1
  2. 2Dependency injection is best described as:O1
  3. 3Why program against an interface like PaymentProvider?O1
  4. 4The preferred injection style in Spring is:O2
  5. 5A key benefit of constructor injection is:O2
  6. 6Why is field injection a weakness?O2
  7. 7Two beans implement PaymentProvider and nothing selects one. What happens?O3
  8. 8Which annotation marks the default among several beans?O3
  9. 9Correct bean order:O4
  10. 10Services A and B each need the other via constructors. The best fix:O4

الجزء العملي

Seven exercises JNR-009-EX01 to EX07 plus the lab. The assessment: explain a DI wiring diagram and fix an ambiguous-bean failure. Pass: quiz at least 8/10, diagram explained, failure fixed, lab acceptance met.

أخطاء شائعة

@Autowired on fields; services that look up beans from the context; circular dependencies "fixed" with @Lazy.

التقييم

Explain a DI wiring diagram and fix an ambiguous-bean failure.

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

The core of every Spring application; enables mocking (JNR-044).

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