Spring Boot services · Chapter 4 of 41

Dependency injection

By the end of this chapter you can let Spring pass beans into your constructors, choose between two beans of one type, and explain why constructor injection beats field injection. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Spring foundations
  • 5 tasks
  • Spring Boot
  • In the all labs plan

The first 2 chapters of Spring Boot services are free. This one comes with the all labs plan.

Why you need this

The counters take UPI and cash, and Meera has signed a card partner for next month. Ravi has found what checkout looks like today:

public class CheckoutService {
  private final UpiPayment upi = new UpiPayment(); // decided here, for ever
}

Every new way to pay means editing checkout, and a test of checkout really charges UPI. Last chapter you could look beans up with getBean, but then checkout depends on the whole context and still chooses for itself. What Kabir wants is the other way round: checkout asks for a PaymentMethod, and Spring decides which one to hand over.

What you build: 5 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    CheckoutService.java: keep the constructor's beans and pay with the default method

    In CheckoutService, keep the three things the constructor receives (defaultMethod, refundMethod and methods) in fields, and write checkout(amount) so it pays with the default method.

    checkout(40) is "₹40 paid by UPI" and checkout(25) is "₹25 paid by UPI". Use the method the constructor was given; do not create a UpiPayment yourself.

  2. Task 2

    CashPayment.java: register cash as a second payment bean, with UPI primary

    Make CashPayment a component named cash, so the context holds exactly two PaymentMethod beans, named cash and upi.

    Two beans of one type stop the app starting, because checkout's defaultMethod cannot choose. Make UpiPayment the primary PaymentMethod, so asking the context for one PaymentMethod gives the UpiPayment.

  3. Task 3

    CheckoutService.java: send every refund back in cash

    Write refund(amount) so it refunds through the refund method, and make the constructor's refundMethod parameter receive the bean named cash, although upi is the primary bean.

    refund(40) is "₹40 refunded in cash", while checkout keeps using UPI: checkout(50) is still "₹50 paid by UPI".

  4. Task 4

    CheckoutService.java: pay with any method by its bean name

    Write pay(method, amount) so it pays with the payment bean of that name, taken from the map the constructor received: pay("cash", 25) is "₹25 paid in cash" and pay("upi", 50) is "₹50 paid by UPI".

    An unknown name throws an IllegalArgumentException with exactly the message "Unknown payment method: bitcoin" for pay("bitcoin", 25). Write methods() so it returns the map's bean names sorted: ["cash", "upi"].

  5. Task 5 · interview question

    Interview: show why constructor injection beats field injection

    Every field of CheckoutService is final and none has @Autowired, and there is at least one field. CheckoutService must work as a plain object without Spring: built with new and hand-made payment methods, checkout uses the first one, refund the second and pay looks the name up in the given map.

    Be ready to explain why constructor injection is better than field injection.

What you will learn

  • Constructor injection: a class lists what it needs as constructor parameters, and Spring passes the beans in.
  • @Primary and @Qualifier: choosing when two beans have the same type.
  • Every bean of a type: a List or Map parameter collects them all.
  • Why not field injection: the interview question.

More in Spring foundations

Build "Dependency injection" yourself

Tapri Cloud: the counters’ Java grows into a Spring Boot service with a database. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Spring Boot services are free. This one comes with the all labs plan.