انعكاس التحكم وحقن الاعتماديات (IoC وDI)
في هذه الصفحة 8 موضوعات
نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.
نظرة عامة
الموضوعات
- انعكاس التحكم (Inversion of Control)
- حقن الاعتماديات (Dependency injection)
- الحقن عبر المُنشئ (Constructor injection)
- دورة حياة الـ Bean
تعلّم أولًا
3 ساعة تدريب (تقدير)
مخرجات التعلم
- Explain inversion of control and dependency injection, first without Spring.
- Use constructor injection and explain why it beats field injection.
- Resolve ambiguity between multiple implementations with
@Primaryand@Qualifier. - Describe the bean lifecycle (instantiation, injection, initialization, destruction).
الدروس
المختبر والتمارين
Lab
JNR-009-LAB1 · Inject Shop Core
Outcomes: 1–4 · Time: ~2 h
- Take Shop Core's services (pricing, orders, payments) and the pricing strategies (for example
RegularPricing,DiscountPricing). - Register them as beans and inject through constructors.
- Add two
PaymentProviderimplementations; choose the one for orders with@Qualifier. - Add a
@PostConstructlog 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)
| ID | Outcome | Task |
|---|---|---|
| JNR-009-EX01 | 1 | Without Spring, refactor a class that does new EmailSender() so the sender is passed in; write a test using a fake. |
| JNR-009-EX02 | 1 | Explain IoC and DI in your own words and name who creates objects in each style. |
| JNR-009-EX03 | 2 | Convert a field-injected service to constructor injection; list two benefits. |
| JNR-009-EX04 | 3 | Given two beans of one interface and a startup failure, fix it with @Primary, then with @Qualifier. |
| JNR-009-EX05 | 4 | Add @PostConstruct and @PreDestroy logs to a bean and predict the order relative to the constructor. |
| JNR-009-EX06 | 2 | Break a circular dependency between two services by extracting a third class. |
| JNR-009-EX07 | 1 | Draw 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;} }withnew 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,
mainor the class creates; with Spring, the container. - EX03 Benefits:
finalfields and complete objects; easynewin tests; dependencies visible in the signature. - EX04
@Primaryon the default implementation. Or@Qualifier("paypal")on the constructor parameter. - EX05 Constructor, injection,
@PostConstruct, (use),@PreDestroyon 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.
اختبار ذاتي لك: لا توجد درجات، ولا يُحفظ أو يُرسل أي شيء. اختر إجابة ثم تحقق منها، ويمكنك المحاولة مرة أخرى كما تشاء.
الجزء العملي
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).