IoC and Dependency Injection in Spring
Inversion of Control means Spring creates your objects and wires their dependencies, instead of your code calling new. Dependency injection is how it does that. This is the first Spring question in almost every Java backend interview.
What you will learn
- Explain IoC, the ApplicationContext and beans
- Compare constructor, setter and field injection
- Resolve multiple candidates with @Primary and @Qualifier
- Know @Component, @Service, @Repository, @Configuration and @Bean
Key concepts
Bean
An object created and managed by the Spring container. Declared with stereotype annotations found by component scanning, or with @Bean methods inside @Configuration classes.
ApplicationContext
The IoC container. It reads bean definitions, creates beans in dependency order, injects dependencies, runs lifecycle callbacks and publishes events.
Constructor injection
The recommended style: dependencies are final, the class cannot be created half-initialised, it is easy to test with new, and circular dependencies fail fast. With one constructor, @Autowired is optional.
Stereotypes
@Component (generic), @Service (business logic), @Repository (data access, translates persistence exceptions to DataAccessException), @Controller / @RestController (web).
Step by step
Choosing between two implementations
public interface PaymentGateway { Receipt charge(Money m); }
@Service @Primary
class RazorpayGateway implements PaymentGateway { ... }
@Service("stripeGateway")
class StripeGateway implements PaymentGateway { ... }
@Service
class CheckoutService {
private final PaymentGateway defaultGateway;
private final PaymentGateway stripe;
CheckoutService(PaymentGateway defaultGateway, @Qualifier("stripeGateway") PaymentGateway stripe) {
this.defaultGateway = defaultGateway; // Razorpay, because of @Primary
this.stripe = stripe;
}
}Examples
Registering a third-party class as a bean
@Configuration
class HttpConfig {
@Bean
RestClient pricingClient(RestClient.Builder builder, @Value("${pricing.url}") String url) {
return builder.baseUrl(url).build();
}
}Practise it
Try these without looking back. If you can explain each one out loud, you are ready for this question.
- Inject all PaymentGateway beans as a Map<String, PaymentGateway> and choose one at runtime.
- Explain why field injection makes unit testing harder.
Interview questions
What interviewers ask next on this topic. Answer out loud first, then open the model answer.
BeanFactory vs ApplicationContext?
BeanFactory is the basic container with lazy bean creation. ApplicationContext extends it with eager singleton creation, events, internationalisation, environment and profiles, and AOP integration. Applications use ApplicationContext.
How does Spring handle circular dependencies?
With constructor injection it cannot create either bean and fails at startup with BeanCurrentlyInCreationException. Spring Boot 2.6+ also forbids circular references by default. Fix the design: extract a third bean, or use events.
@Component vs @Bean?
@Component goes on your own class and is picked up by component scanning. @Bean goes on a method in a @Configuration class and is used when you need custom construction or are registering a class you do not own.
Summary
Spring's ApplicationContext creates and wires beans; prefer constructor injection, use stereotypes for your classes and @Bean for others, and resolve multiple candidates with @Primary or @Qualifier.
Stuck on this one? Talk to Sumit.
Keys: F focus mode · ← → previous / next topic · Esc leave focus