What Spring is and why teams use it, its modules, Spring vs Spring Boot, beans, IoC and DI, the IoC container, BeanFactory vs ApplicationContext, @Configuration and @Bean, and why constructor injection wins.
Published September 25, 2026
Every Spring interview starts with IoC and DI. The best answers explain the problem Spring solves (object wiring and cross-cutting concerns) before naming any annotations. Keep a small OrderService → OrderRepository example in mind, and use it throughout.
Short answer: Spring is an open-source framework for building Java applications. At its core is an IoC container that creates your objects (beans) and wires their dependencies together. Around that core, Spring provides ready-made infrastructure for web applications, data access, transactions, security, messaging and testing.
Key points to cover:
@Transactional).jakarta.*) packages.Learn it in depth → IoC Container Fundamentals
Short answer: Loose coupling through DI, easy unit testing (inject mocks), declarative transactions and security, consistent data access (including exception translation), and a huge ecosystem: Spring Boot, Data, Security, Cloud and Batch.
Key points to cover:
Short answer:
Separate projects build on top of the framework: Spring Boot, Spring Data, Spring Security, Spring Cloud, Spring Batch.
Key points to cover:
Short answer: Spring Framework provides the building blocks: DI, MVC, transactions. Spring Boot is an opinionated layer on top that makes those blocks quick to use. It adds auto-configuration, starter dependencies, an embedded server (a runnable JAR, no WAR deployment), externalised configuration and production features (Actuator).
| Spring Framework | Spring Boot | |
|---|---|---|
| Configuration | You configure everything (Java config or XML) | Auto-configured, sensible defaults |
| Dependencies | You pick compatible versions | Starters, with managed versions |
| Deployment | WAR on an external server | Executable JAR with embedded Tomcat, Jetty or Undertow |
| Production features | Add them yourself | Actuator: health, metrics, info |
Learn it in depth → What Is Spring Boot
Short answer: A bean is an object whose lifecycle is created, configured and managed by the Spring container. Any class can become a bean, either through a stereotype annotation (@Component, @Service, @Repository, @Controller) found by component scanning, or through an @Bean factory method.
Key points to cover:
@PostConstruct, @PreDestroy).new are not beans. Spring doesn't inject into them, and doesn't apply AOP to them (so no @Transactional).Learn it in depth → Bean Lifecycle in Detail
Short answer: Inversion of Control is the principle: your code doesn't create or control its collaborators; a framework does. Dependency Injection is the way Spring implements IoC. Dependencies are passed in (through a constructor, setter or field), instead of being constructed inside the class.
// Without DI: OrderService is welded to one implementation
class OrderService {
private final OrderRepository repo = new JdbcOrderRepository(); // hard to test or swap
}
// With DI: the container supplies whichever implementation is configured
@Service
class OrderService {
private final OrderRepository repo;
OrderService(OrderRepository repo) { this.repo = repo; } // injected
}
Key points to cover:
Learn it in depth → Spring Dependency Injection
Short answer: The container reads bean definitions (from component scanning, @Configuration classes or XML), instantiates beans, resolves and injects their dependencies, applies post-processors (which is how AOP proxies, @Autowired and @Value work), runs lifecycle callbacks, and destroys beans on shutdown.
Key points to cover:
NoSuchBeanDefinitionException or NoUniqueBeanDefinitionException.Learn it in depth → IoC Container Fundamentals
Short answer: BeanFactory, the basic container, which creates beans lazily on request, and ApplicationContext, which extends it and is what every real application uses.
Key points to cover:
ApplicationContext adds:
ApplicationEventPublisher).MessageSource) and resource loading.@Autowired and AOP work.AnnotationConfigApplicationContext, plus the web and servlet contexts that Spring Boot creates for you.@Configuration and @Bean used for?Short answer: @Configuration marks a class as a source of bean definitions. @Bean on one of its methods registers the method's return value as a bean. Use this for objects you can't annotate yourself, such as third-party classes, or for objects that need custom construction logic.
@Configuration
class HttpConfig {
@Bean
RestClient paymentsClient(@Value("${payments.base-url}") String baseUrl) {
return RestClient.builder().baseUrl(baseUrl).build(); // third-party object → bean
}
}
Key points to cover:
@Configuration classes are proxied by CGLIB, so calling one @Bean method from another returns the same singleton, not a new instance. With @Configuration(proxyBeanMethods = false) (the "lite" mode Boot uses internally), those calls create new objects.Learn it in depth → Component Scanning & Configuration
Short answer: Constructor injection.
final, so the object is immutable and fully initialised from the start.new Service(mockRepo), with no Spring context needed.@Service
class PaymentService {
private final PaymentGateway gateway;
private final AuditLog audit;
PaymentService(PaymentGateway gateway, AuditLog audit) { // single constructor: @Autowired not needed
this.gateway = gateway;
this.audit = audit;
}
}
Key points to cover:
@Autowired private Repo repo;) hides dependencies, prevents final fields, and needs reflection or a Spring context in tests. Avoid it outside test classes.Q: What happens if two beans implement the same interface?
A: Injection by type becomes ambiguous (NoUniqueBeanDefinitionException). Resolve it with @Primary on the default implementation, @Qualifier("name") at the injection point, or by injecting List<Interface> or Map<String, Interface> to get all of them.
Q: Is @Autowired required on constructors?
A: Not since Spring 4.3, when a class has exactly one constructor. With several constructors, mark the one Spring should use.
Q: What's the difference between @Component, @Service, @Repository and @Controller?
A: All four are component-scanned stereotypes. @Repository also enables exception translation into Spring's DataAccessException hierarchy. @Controller marks web request handlers. @Service is purely semantic: it labels the business layer.
Q: How does Spring handle circular dependencies?
A: With constructor injection, a cycle fails at startup (BeanCurrentlyInCreationException). Spring Boot 2.6+ also forbids field and setter cycles by default. The right fix is to redesign: extract the shared logic into a third bean, or use events.