Skip to content
Ameen Abozaid
Planned lessonJNR-009-L03

Constructor injection

  • Lesson 3 of 8
  • Junior
On This Lesson 4 topics

This lesson is planned. The outline below lists the topics it will cover. The full lesson has not been published yet.

In this lesson

use constructor injection and say why it beats field injection.

Planned topics

  • 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.
Draft outline details

Demonstration

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

Check your understanding

Why can a constructor-injected field be final?

Show answer

It is set once, in the constructor.

Practice

Apply this lesson with the module's lab, exercises and quiz.

Lab and exercises Quiz

Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.

Continue to next lesson

Type to search levels, phases, modules and lessons.