انتقل إلى المحتوى
أمين أبوزيد
درس مخطط لهJNR-009-L03

الحقن عبر المُنشئ (Constructor injection)

  • الدرس 3 من 8
  • Junior
في هذا الدرس 4 موضوعات

هذا الدرس مخطط له. يعرض المخطط أدناه الموضوعات التي سيغطيها، ولم يُنشر الدرس الكامل بعد.

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

في هذا الدرس

use constructor injection and say why it beats field injection.

الموضوعات المخطط لها

  • Spring passes beans into the constructor. With one constructor, @Autowired is not needed.
  • Fields can be final: the object is complete and never half-built.
  • Dependencies are visible in the signature; tests just call new.
  • Field injection (@Autowired on a field) hides dependencies, needs reflection in tests, and prevents final.
تفاصيل المخطط المبدئي

عرض عملي

java
@Service
public class OrderService {
    private final PaymentProvider payment;
    public OrderService(PaymentProvider payment) { this.payment = payment; }
}

اختبر فهمك

Why can a constructor-injected field be final?

أظهر الإجابة

It is set once, in the constructor.

التطبيق

طبّق هذا الدرس بمختبر الوحدة وتمارينها واختبارها.

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

تعليم الدرس كمكتمل اختياري ويمكنك التراجع عنه في أي وقت. يبقى تقدمك محفوظًا في هذا المتصفح.

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