Java Developer Interview Question

How does dependency injection work in Spring, and why do you prefer constructor injection?

What the interviewer is probing, how to structure your answer, and a spoken example you can adapt.

Quick answer

The container builds beans and supplies their collaborators, so classes declare what they need rather than constructing it. Constructor injection is preferred because the dependencies are mandatory and visible, the fields can be final so the object is immutable and fully initialized, the class is testable with plain new and no reflection, and a circular dependency fails loudly at startup instead of hiding behind lazy field injection.

Why interviewers ask this

It is a design question wearing a framework label. The interviewer wants to hear about testability and immutability rather than the mechanics of the annotations, plus awareness of bean scopes and the singleton trap of putting mutable state in a shared bean. Whether you can explain how you would test the class without a Spring context tells them a lot about your unit testing habits.

How to structure your answer

  • Describe inversion of control in one sentence.
  • Give the concrete advantages of constructor injection.
  • Mention scopes and the danger of state in a singleton.
  • Explain how this shapes your tests.

Example answer

Spoken example, first person

The container owns object creation, so my class asks for what it needs in its constructor and Spring resolves and injects it. I use constructor injection because it makes the contract obvious: if a class takes five collaborators, that is now visible in the signature rather than hidden across five annotated fields, and it usually tells me the class is doing too much. The fields can be final, so the object is fully constructed and safe to publish across threads, and I can instantiate it in a test with plain constructor arguments and no Spring context at all, which keeps unit tests fast. Circular dependencies also surface at startup as a failure rather than being papered over. On scope, beans are singletons by default, so I keep them stateless; a mutable field on a singleton service is a race condition waiting for concurrent requests. When I need per request state I pass it as a method parameter rather than reaching for a request scoped bean. Field injection I actively avoid, since it hides dependencies and needs reflection to set up in a test.

Walking into this interview soon? GhostPilot listens to your live call, spots the question the moment it is asked, and puts a structured answer on your screen in real time. Try it on your next mock, or grab a $29 Session Pass, no subscription, for the real thing.

See how it works

Follow-up questions to expect

  • How would you handle two beans of the same type?
  • What happens when you inject a prototype bean into a singleton?
  • How do you test a component that depends on the current request?

Related java developer questions

Your interviewer will ask their own version of this. Paste your actual job description into the free Question Predictor and get the 20 questions that role is most likely to ask, with what each one is really probing.

Predict my questions

Rehearse the hard questions before they are asked

Practise with a live copilot, then walk in ready. A $29 Session Pass gets you through the interview with no subscription and no lock-in.

Get GhostPilot