Chaturmind
LearnDSASystem DesignInterview PrepDevOpsEngineering GrowthBlog
Start learning
Chaturmind

Structured learning paths for engineers who want to go deep. Written by practitioners.

Learn

  • Java
  • DSA
  • System Design
  • Spring Boot
  • AI / ML
  • DevOps
  • Engineering Growth
  • Java Interview Prep

Company

  • Blog
  • Contact

Legal

  • Privacy Policy
  • Terms of Service

© 2026 Chaturmind. All rights reserved.

Built for engineers who want to go deep.


← Java Interview Prep: Fresher to 2 Years

Java Basics, JVM & Memory

  • JDK, JRE, JVM & the main Method — Interview Questions
  • JVM Memory & Garbage Collection — Interview Questions
  • Data Types, Wrapper Classes & Equality — Interview Questions

Object-Oriented Programming

  • Classes, Objects, Packages & Access Modifiers — Interview Questions
  • Inheritance, Composition, this & super — Interview Questions
  • Polymorphism, Overloading & Overriding — Interview Questions
  • Abstraction, Interfaces & Encapsulation — Interview Questions
  • Constructors, Singleton, Anonymous Classes & Immutability — Interview Questions
  • Design Patterns & SOLID Basics — Interview Questions

Strings, Keywords, Exceptions & Serialization

  • Strings, String Pool, StringBuilder & StringBuffer — Interview Questions
  • static & final Keywords — Interview Questions
  • Exceptions, Generics, Enums & Reflection — Interview Questions
  • Serialization & transient — Interview Questions

Collections Framework

  • Collections Framework Basics — Interview Questions
  • HashMap, HashSet & TreeMap Internals — Interview Questions

Multithreading Basics

  • Threads, Synchronization & volatile Basics — Interview Questions

Java 8+ & Stream API

  • Java 8 to Java 21 Features — Interview Questions
  • Stream API Coding Questions (Part 1) — Interview Questions
  • Stream API Coding Questions (Part 2) — Interview Questions

Coding Round Programs

  • Classic Number & String Programs — Interview Questions
  • String & Collection Programs — Interview Questions
  • Array & String Problem Solving — Interview Questions

Spring Framework Core

  • Spring IoC, Dependency Injection & Beans — Interview Questions
  • Spring Injection Types, Scopes, Profiles & WebFlux — Interview Questions

Spring Boot Essentials

  • Spring Boot Fundamentals — Interview Questions
  • Spring Boot Runners, Servers & Configuration — Interview Questions
  • Spring Boot Controllers, Profiles, Actuator & DevTools — Interview Questions
  • Spring Boot Testing, Exceptions & Auto-Configuration — Interview Questions
  • REST APIs, Swagger, Embedded Servers & Key Annotations — Interview Questions

Spring MVC

  • Spring MVC Architecture & DispatcherServlet — Interview Questions
  • Spring MVC Request Mapping & Controllers — Interview Questions
  • Spring MVC Forms, Views & Interceptors — Interview Questions
  • Spring MVC Exceptions, Security & Dependency Injection — Interview Questions
  • Spring MVC Data Binding, Static Resources & Path Variables — Interview Questions
  • Spring MVC i18n, Testing, File Uploads & Scaling — Interview Questions

Hibernate & Spring Data JPA

  • Hibernate & JPA Core Concepts — Interview Questions
  • Hibernate Performance, Mapping & Scenarios — Interview Questions

SQL

  • SQL Basics, Keys, Normalization & Transactions — Interview Questions
  • SQL Joins, Triggers, Procedures, Functions & Indexes — Interview Questions
  • SQL "Difference Between" Questions — Interview Questions
  • SQL Query Writing (Part 1) — Interview Questions
  • SQL Query Writing (Part 2) — Interview Questions

Microservices Basics

  • Microservices, API Gateway & Communication — Interview Questions
  • Service Discovery, Data Consistency & Deployment — Interview Questions
  • Microservices Monitoring, Security & Resilience — Interview Questions

Maven & Git

  • Maven — Interview Questions
  • Git — Interview Questions
Chaturmind
← Java Interview Prep: Fresher to 2 Years

Java Basics, JVM & Memory

  • JDK, JRE, JVM & the main Method — Interview Questions
  • JVM Memory & Garbage Collection — Interview Questions
  • Data Types, Wrapper Classes & Equality — Interview Questions

Object-Oriented Programming

  • Classes, Objects, Packages & Access Modifiers — Interview Questions
  • Inheritance, Composition, this & super — Interview Questions
  • Polymorphism, Overloading & Overriding — Interview Questions
  • Abstraction, Interfaces & Encapsulation — Interview Questions
  • Constructors, Singleton, Anonymous Classes & Immutability — Interview Questions
  • Design Patterns & SOLID Basics — Interview Questions

Strings, Keywords, Exceptions & Serialization

  • Strings, String Pool, StringBuilder & StringBuffer — Interview Questions
  • static & final Keywords — Interview Questions
  • Exceptions, Generics, Enums & Reflection — Interview Questions
  • Serialization & transient — Interview Questions

Collections Framework

  • Collections Framework Basics — Interview Questions
  • HashMap, HashSet & TreeMap Internals — Interview Questions

Multithreading Basics

  • Threads, Synchronization & volatile Basics — Interview Questions

Java 8+ & Stream API

  • Java 8 to Java 21 Features — Interview Questions
  • Stream API Coding Questions (Part 1) — Interview Questions
  • Stream API Coding Questions (Part 2) — Interview Questions

Coding Round Programs

  • Classic Number & String Programs — Interview Questions
  • String & Collection Programs — Interview Questions
  • Array & String Problem Solving — Interview Questions

Spring Framework Core

  • Spring IoC, Dependency Injection & Beans — Interview Questions
  • Spring Injection Types, Scopes, Profiles & WebFlux — Interview Questions

Spring Boot Essentials

  • Spring Boot Fundamentals — Interview Questions
  • Spring Boot Runners, Servers & Configuration — Interview Questions
  • Spring Boot Controllers, Profiles, Actuator & DevTools — Interview Questions
  • Spring Boot Testing, Exceptions & Auto-Configuration — Interview Questions
  • REST APIs, Swagger, Embedded Servers & Key Annotations — Interview Questions

Spring MVC

  • Spring MVC Architecture & DispatcherServlet — Interview Questions
  • Spring MVC Request Mapping & Controllers — Interview Questions
  • Spring MVC Forms, Views & Interceptors — Interview Questions
  • Spring MVC Exceptions, Security & Dependency Injection — Interview Questions
  • Spring MVC Data Binding, Static Resources & Path Variables — Interview Questions
  • Spring MVC i18n, Testing, File Uploads & Scaling — Interview Questions

Hibernate & Spring Data JPA

  • Hibernate & JPA Core Concepts — Interview Questions
  • Hibernate Performance, Mapping & Scenarios — Interview Questions

SQL

  • SQL Basics, Keys, Normalization & Transactions — Interview Questions
  • SQL Joins, Triggers, Procedures, Functions & Indexes — Interview Questions
  • SQL "Difference Between" Questions — Interview Questions
  • SQL Query Writing (Part 1) — Interview Questions
  • SQL Query Writing (Part 2) — Interview Questions

Microservices Basics

  • Microservices, API Gateway & Communication — Interview Questions
  • Service Discovery, Data Consistency & Deployment — Interview Questions
  • Microservices Monitoring, Security & Resilience — Interview Questions

Maven & Git

  • Maven — Interview Questions
  • Git — Interview Questions
HomeLearnJava Interview PrepJava Interview Prep: Fresher to 2 YearsSpring Framework Core
✓ FreeBeginner· 7 min read

Spring IoC, Dependency Injection & Beans — Interview Questions

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


How to use this lesson

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.

Q1. What is Spring?

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:

  • The problem it solves: without Spring, every class creates and configures its own collaborators, which makes code tightly coupled and hard to test. Spring moves that job into the container.
  • Two core technologies:
    • Dependency injection: objects receive their dependencies instead of creating them.
    • AOP: cross-cutting concerns such as transactions, security and caching are applied declaratively (@Transactional).
  • Spring Framework 6 (the basis of Spring Boot 3) needs Java 17+, and uses Jakarta EE (jakarta.*) packages.

Learn it in depth → IoC Container Fundamentals

Q2. What are the advantages of the Spring framework?

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:

  • It's non-invasive: your classes are plain Java objects (POJOs). They don't have to extend framework classes.
  • Integration: Spring connects to JDBC/JPA, Kafka, RabbitMQ, Redis, MongoDB and OAuth2 providers through consistent abstractions.
  • Productivity: Spring Boot's auto-configuration removes most of the setup.

Q3. What are the main modules of the Spring framework?

Short answer:

  • Core container: Beans, Core, Context, SpEL.
  • AOP.
  • Data access: JDBC, ORM, transactions.
  • Web: Spring MVC and WebFlux.
  • Messaging.
  • Test.

Separate projects build on top of the framework: Spring Boot, Spring Data, Spring Security, Spring Cloud, Spring Batch.

Key points to cover:

  • Being precise here earns credit: Security, Boot and Cloud are separate projects in the Spring ecosystem, not modules of the core framework.

Q4. What's the difference between Spring and Spring Boot?

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 FrameworkSpring Boot
ConfigurationYou configure everything (Java config or XML)Auto-configured, sensible defaults
DependenciesYou pick compatible versionsStarters, with managed versions
DeploymentWAR on an external serverExecutable JAR with embedded Tomcat, Jetty or Undertow
Production featuresAdd them yourselfActuator: health, metrics, info

Learn it in depth → What Is Spring Boot

Q5. What is a Spring bean?

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:

  • A bean has a name, a type, a scope (singleton by default), dependencies, and lifecycle callbacks (@PostConstruct, @PreDestroy).
  • Objects you create with 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

Q6. What are IoC and DI?

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:

  • Benefits: loose coupling, easy mocking in unit tests, and configuration moved out of the business code.

Learn it in depth → Spring Dependency Injection

Q7. What is the role of the IoC container?

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:

  • At startup, it builds a dependency graph. Missing or ambiguous beans cause the application to fail fast, with errors such as NoSuchBeanDefinitionException or NoUniqueBeanDefinitionException.

Learn it in depth → IoC Container Fundamentals

Q8. What types of IoC container does Spring have?

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:
    • Eager creation of singletons at startup, so configuration errors show up immediately.
    • Event publishing (ApplicationEventPublisher).
    • Internationalisation (MessageSource) and resource loading.
    • Automatic registration of bean post-processors, which are what make @Autowired and AOP work.
  • Common implementations: AnnotationConfigApplicationContext, plus the web and servlet contexts that Spring Boot creates for you.

Q9. What are @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

Q10. What is the best way to inject beans, and why?

Short answer: Constructor injection.

  • Dependencies are explicit and required.
  • Fields can be final, so the object is immutable and fully initialised from the start.
  • It's trivially testable with new Service(mockRepo), with no Spring context needed.
  • A large constructor makes it obvious when a class is doing too much.
@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:

  • Field injection (@Autowired private Repo repo;) hides dependencies, prevents final fields, and needs reflection or a Spring context in tests. Avoid it outside test classes.
  • Use setter injection only for genuinely optional dependencies, or ones that are reconfigured later.

Follow-up questions this topic invites — and their answers

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.

Previous

Array & String Problem Solving — Interview Questions

Next

Spring Injection Types, Scopes, Profiles & WebFlux — Interview Questions

AI Tutor

Lesson: Spring IoC, Dependency Injection & Beans — Interview Questions

Quick actions

AI responses can be inaccurate. Verify critical information.