الحقن عبر المُنشئ (Constructor injection)
في هذا الدرس 4 موضوعات
هذا الدرس مخطط له. يعرض المخطط أدناه الموضوعات التي سيغطيها، ولم يُنشر الدرس الكامل بعد.
نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.
في هذا الدرس
use constructor injection and say why it beats field injection.
الموضوعات المخطط لها
- Spring passes beans into the constructor. With one constructor,
@Autowiredis 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 (
@Autowiredon a field) hides dependencies, needs reflection in tests, and preventsfinal.
تفاصيل المخطط المبدئي
عرض عملي
@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.
التطبيق
طبّق هذا الدرس بمختبر الوحدة وتمارينها واختبارها.
تعليم الدرس كمكتمل اختياري ويمكنك التراجع عنه في أي وقت. يبقى تقدمك محفوظًا في هذا المتصفح.