Scrivio Interview library

Dependency injection

25 questions in 7 subtopics.

Constructor, setter and field injection

ConceptFoundations6 min read
Why is constructor injection recommended over field injection in Spring? Is @Autowired needed on the constructor?
CodingFoundations6 min read
Rewrite this class to use constructor injection and explain what changes for tests:
@Service
class OrderService {
@Autowired
OrderRepository repo;
@Autowired
PaymentClient payments;
}
ConceptIntermediate6 min read
When is setter injection still a reasonable choice?
LeadershipPrincipal6 min read
Spring's recommended injection style changed from XML setter injection to field injection with annotations to constructor injection. What drove these changes and what does it teach about framework conventions?

@Autowired resolution

InternalsFoundations5 min read
How does Spring choose which bean to inject into an @Autowired field of type PaymentGateway when there are two implementations?
DebuggingFoundations6 min read
Startup fails with 'No qualifying bean of type InventoryClient available'. List the causes you would check.
DebuggingIntermediate6 min read
After upgrading to Spring Framework 6.1, constructor injection that relied on matching parameter names to bean names starts failing with NoUniqueBeanDefinitionException. Why?
ScenarioPrincipal7 min read
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

CodingFoundations5 min read
There are two DataSource beans, primaryDs and reportingDs. Show how to make primaryDs the default and still inject reportingDs where needed.
ConceptIntermediate5 min read
Compare @Primary, @Qualifier, custom qualifier annotations and @Fallback. Which would you use in a shared library?
DesignAdvanced6 min read
Your platform library defines an ObjectMapper bean marked @Primary, and application teams keep fighting it. How would you redesign how the library provides defaults?
LeadershipPrincipal6 min read
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

CodingFoundations5 min read
Show three ways to inject a dependency that may not exist, such as an optional MetricsPublisher.
ConceptIntermediate6 min read
What can ObjectProvider do that a direct injection cannot?
DesignAdvanced6 min read
An internal starter must work whether or not the application uses Micrometer or a tracing library. How would you design its beans?

Injecting collections

ConceptFoundations6 min read
What happens when you inject List<Validator> or Map<String, Validator> in Spring, and how is the list order decided?
DebuggingFoundations6 min read
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?
DesignIntermediate7 min read
Design a payment router that selects one of several PaymentProvider beans by country at runtime, without a growing if-else block.

Circular dependencies

DebuggingFoundations7 min read
Startup fails with 'The dependencies of some of the beans in the application context form a cycle' between OrderService and CustomerService. Why does Spring Boot fail here, and how would you fix the design?
InternalsIntermediate6 min read
How does the Spring bean factory resolve circular dependencies between field-injected singletons when they are allowed, and why can this break with proxies?
LeadershipAdvanced6 min read
A large legacy codebase has 200 circular bean dependencies and runs with allow-circular-references=true. How would you handle this?
LeadershipPrincipal6 min read
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

ConceptFoundations6 min read
What does @Lazy do on a bean class, and what does it do on an injection point?
ScenarioIntermediate7 min read
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?
DesignAdvanced7 min read
When is @Lazy on an injection point a legitimate design tool, and when is it hiding a problem?