Skip to content
Ameen Abozaid
JNR-009

IoC and DI

  • 8 Lessons
  • 3 h practice (estimate)
On this page 8 topics

Overview

Topics

  • Inversion of Control
  • dependency injection
  • constructor injection
  • bean lifecycle

Learn first

3 h of practice (estimate)

Learning outcomes

  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).

Lessons

  1. 01Wiring by hand: the problem
  2. 02Inversion of control
  3. 03Constructor injection
  4. 04Multiple implementations
  5. 05Bean lifecycle and callbacks
  6. 06Circular dependencies and how to avoid them
  7. 07Demo: wiring Shop Core by hand, then with Spring
  8. 08Demo: diagnosing an ambiguous-bean startup failure

Lab and exercises

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.
Show solutions
  • 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.

Quiz

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

A self-check for you: nothing is graded, saved or sent anywhere. Choose an answer, then check it. You can try again as often as you like.

  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

Practical part

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.

Common mistakes

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

Assessment

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

Career relevance

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

Type to search levels, phases, modules and lessons.