Constructor injection makes a class's required collaborators visible. It also lets a plain unit test supply those collaborators without starting the Spring container. Bean selection is a separate question: the container must know which implementation to inject.

These Spring Boot interview questions use two message formatters. Neither sends messages. Keeping formatting separate from delivery makes the example small enough to reason about.

What happens when two beans share an interface?

package example;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

interface MessageFormatter { String format(String name); }

@Component("plainFormatter")
class PlainFormatter implements MessageFormatter {
    public String format(String name) { return "Hello, " + name; }
}

@Component("formalFormatter")
class FormalFormatter implements MessageFormatter {
    public String format(String name) { return "Welcome, " + name; }
}

@Service
class WelcomeText {
    private final MessageFormatter formatter;
    WelcomeText(@Qualifier("plainFormatter") MessageFormatter formatter) {
        this.formatter = formatter;
    }
    String forName(String name) { return formatter.format(name); }
}

The qualifier narrows the matching candidates. A single constructor does not need an Autowired annotation. With several matching implementations, explain the intended selection explicitly rather than relying on an accidental naming arrangement.

When would Primary be suitable?

A primary bean can express the default candidate for a single-valued injection point. A qualifier is useful when that particular consumer needs a specific role. Neither annotation replaces understanding how many beans exist or what each one is responsible for.

If the consumer genuinely needs every implementation, inject a collection and define how it uses them. Do not select the first element without a documented ordering rule. “All formatters” and “the default formatter” are different requirements.

How does a plain test work?

package example;

class WelcomeTextCheck {
    public static void main(String[] args) {
        var text = new WelcomeText(name -> "Test: " + name);
        if (!text.forName("Mina").equals("Test: Mina")) {
            throw new AssertionError("Wrong formatter result");
        }
    }
}

This checks WelcomeText's delegation. It does not prove the Spring qualifier is correct. Add a small context test for registration and selection. Keep the difference visible when explaining your test evidence.

Does singleton mean thread-safe?

Spring's default singleton scope reuses a bean within its container. It does not synchronize mutable fields automatically. A formatter with no mutable request-specific fields is easier to share than a service that stores the current user's draft in an instance variable.

Keep per-request values in method arguments or an appropriate scoped owner. If a singleton needs a shorter-lived collaborator, understand the supported provider or proxy mechanism rather than holding an instance beyond its intended lifetime.

What does a circular dependency suggest?

If A needs B and B needs A, inspect the responsibilities. Extracting a shared policy or moving orchestration into another service may remove the cycle. Changing everything to field injection can conceal the design problem without resolving it.

How should you answer?

  • Name the required dependency in the constructor.
  • Explain how candidates are selected.
  • Keep mutable data within its correct lifetime.
  • Test class behavior separately from container wiring.
  • Use the smallest framework scope that proves the claim.

The useful answer connects injection to clear ownership and testability. Listing annotations alone does not explain why the selected collaborator is correct.

References and next practice

docs.spring.io — spring di

spring-boot question collection · Browse interview topics · Practise a question in Interview Challenge · How to Answer Technical Interview Questions Out Loud