Dependency injection
25 questions in 7 subtopics.
Constructor, setter and field injection
Why is constructor injection recommended over field injection in Spring? Is@Autowired needed on the constructor?Rewrite this class to use constructor injection and explain what changes for tests:@Service
class OrderService {
@Autowired
OrderRepository repo;
@Autowired
PaymentClient payments;
}
@Autowired resolution
How does Spring choose which bean to inject into an@Autowired field of type PaymentGateway when there are two implementations?Startup fails with 'No qualifying bean of type InventoryClient available'. List the causes you would check.After upgrading to Spring Framework 6.1, constructor injection that relied on matching parameter names to bean names starts failing with NoUniqueBeanDefinitionException. Why?Your company is weighing a move of some services from Spring's runtime dependency injection to a compile-time DI framework to reduce startup time. How would you evaluate the move?@Qualifier and @Primary
There are twoDataSource beans, primaryDs and reportingDs. Show how to make primaryDs the default and still inject reportingDs where needed.Compare @Primary, @Qualifier, custom qualifier annotations and @Fallback. Which would you use in a shared library?Your platform library defines an ObjectMapper bean marked @Primary, and application teams keep fighting it. How would you redesign how the library provides defaults?Internal starters from different platform teams keep conflicting over beans of the same type, such as ObjectMapper, RestClient builders and DataSource. What governance would you set for how shared libraries contribute beans?Optional dependencies and ObjectProvider
Show three ways to inject a dependency that may not exist, such as an optionalMetricsPublisher.What can ObjectProvider do that a direct injection cannot?An internal starter must work whether or not the application uses Micrometer or a tracing library. How would you design its beans?Injecting collections
What happens when you injectList<Validator> or Map<String, Validator> in Spring, and how is the list order decided?A class injects List<EventHandler> but one handler is missing from the list. Its class is annotated @Component and logs a constructor message at startup. What could cause this?Design a payment router that selects one of several PaymentProvider beans by country at runtime, without a growing if-else block.Circular dependencies
Startup fails with 'The dependencies of some of the beans in the application context form a cycle' betweenOrderService and CustomerService. Why does Spring Boot fail here, and how would you fix the design?How does the Spring bean factory resolve circular dependencies between field-injected singletons when they are allowed, and why can this break with proxies?A large legacy codebase has 200 circular bean dependencies and runs with allow-circular-references=true. How would you handle this?Spring Boot changed its default to reject circular references. Looking back, what does this kind of default change teach about managing framework upgrades across a large organisation?@Lazy
What does@Lazy do on a bean class, and what does it do on an injection point?A team enabled spring.main.lazy-initialization=true to speed up startup, and now the first requests after deployment are slow and some fail with bean creation errors. What do you advise?When is @Lazy on an injection point a legitimate design tool, and when is it hiding a problem?No question matches these choices.