Spring Core Basics Interview Questions and Answers

Master Spring Core fundamentals with interview questions covering Spring Framework architecture, IoC Container, BeanFactory, ApplicationContext, dependency injection, modules, and enterprise best practices.


Introduction

Spring Framework is the most widely used Java framework for building enterprise applications.

Before Spring, Java EE applications required developers to manually manage:

  • Object creation
  • Dependency management
  • Transactions
  • Security
  • Configuration
  • Resource management

Spring simplified enterprise development by introducing Inversion of Control (IoC) and Dependency Injection (DI).

Today, almost every Spring technology is built on top of Spring Core:

  • Spring Boot
  • Spring MVC
  • Spring Security
  • Spring Data JPA
  • Spring Batch
  • Spring Cloud

Understanding Spring Core is essential for every Java developer.


Spring Framework Architecture

flowchart TD

SpringFramework --> SpringCore

SpringFramework --> SpringMVC

SpringFramework --> SpringData

SpringFramework --> SpringSecurity

SpringFramework --> SpringAOP

SpringFramework --> SpringBoot

SpringFramework --> SpringCloud

Q1. What is Spring Framework?

Answer

Spring Framework is an open-source Java framework used to build enterprise applications.

It provides infrastructure support for:

  • Dependency Injection
  • Aspect-Oriented Programming (AOP)
  • Transactions
  • Security
  • Data Access
  • REST APIs
  • Microservices

Advantages

  • Lightweight
  • Modular
  • Testable
  • Highly maintainable
  • Production ready

Q2. What is Spring Core?

Spring Core is the foundation of the Spring Framework.

It provides:

  • IoC Container
  • Dependency Injection
  • Bean Management
  • Bean Lifecycle
  • Configuration Management

All higher-level Spring modules depend on Spring Core.

Spring Core

flowchart LR

SpringCore --> IoCContainer

IoCContainer --> Beans

Beans --> Application

Q3. What are the major modules of Spring Framework?

Module Purpose
Spring Core IoC and DI
Spring Beans Bean management
Spring Context ApplicationContext
Spring AOP Aspect-Oriented Programming
Spring JDBC Database access
Spring ORM Hibernate integration
Spring MVC Web applications
Spring Security Authentication & Authorization
Spring Test Unit & Integration testing

Each module focuses on a specific enterprise requirement.


Q4. What is the IoC Container?

The IoC (Inversion of Control) Container is responsible for:

  • Creating objects
  • Managing beans
  • Injecting dependencies
  • Managing bean lifecycle

Instead of creating objects manually,

Spring creates and manages them.

IoC Container

flowchart LR

Application --> IoCContainer

IoCContainer --> CustomerService

IoCContainer --> PaymentService

IoCContainer --> Repository

Q5. What is a Spring Bean?

A Bean is an object managed by the Spring IoC Container.

Example

@Service

public class

CustomerService{

}

Spring creates the object automatically.

Characteristics

  • Managed by Spring
  • Dependency injected
  • Lifecycle controlled
  • Singleton by default

Q6. What is BeanFactory?

BeanFactory is the simplest IoC container.

Responsibilities

  • Bean creation
  • Dependency injection
  • Lazy initialization

Example

BeanFactory factory =
new XmlBeanFactory(...);

Features

  • Lightweight
  • Minimal memory usage
  • Suitable for small applications

Q7. What is ApplicationContext?

ApplicationContext extends BeanFactory.

Additional features

  • Internationalization (i18n)
  • Event publishing
  • AOP integration
  • Annotation processing
  • Resource loading

Example

ApplicationContext context =

new AnnotationConfigApplicationContext(

AppConfig.class);

BeanFactory vs ApplicationContext

BeanFactory ApplicationContext
Basic container Advanced container
Lazy initialization Eager singleton initialization
Limited features Enterprise features
Rarely used Most commonly used

Q8. What is Dependency Injection?

Dependency Injection means Spring provides required dependencies instead of objects creating them manually.

Without Spring

CustomerRepository repo =

new CustomerRepository();

With Spring

@Autowired

CustomerRepository repo;

Advantages

  • Loose coupling
  • Better testing
  • Easier maintenance

Q9. Why is Spring called Lightweight?

Spring is considered lightweight because

  • POJO-based programming
  • No mandatory application server
  • Modular architecture
  • Minimal resource usage
  • Easy integration

Applications use only the required modules.


Q10. Spring Core Best Practices

Prefer Constructor Injection

Avoid field injection.


Keep Beans Stateless

Improve scalability.


Use Interfaces

Promote loose coupling.


Avoid Manual Object Creation

Let Spring manage dependencies.


Use Annotation-Based Configuration

Prefer Java Config over XML.


Banking Example

flowchart TD

CustomerController --> CustomerService

CustomerService --> CustomerRepository

CustomerRepository --> PostgreSQL

SpringContainer --> CustomerController

SpringContainer --> CustomerService

SpringContainer --> CustomerRepository

The IoC container creates and wires all application components automatically.


Common Interview Questions

  • What is Spring Framework?
  • What is Spring Core?
  • What are Spring modules?
  • What is IoC?
  • What is Dependency Injection?
  • What is a Spring Bean?
  • BeanFactory vs ApplicationContext?
  • Why is Spring lightweight?
  • What are POJOs?
  • Spring Core best practices?

Quick Revision

Topic Summary
Spring Framework Enterprise Java framework
Spring Core Foundation module
IoC Spring manages objects
Dependency Injection Inject dependencies automatically
Bean Spring-managed object
BeanFactory Basic container
ApplicationContext Advanced container
POJO Plain Old Java Object
Annotation Configuration Preferred configuration style
Constructor Injection Recommended dependency injection

Bean Creation Lifecycle

sequenceDiagram
Application->>ApplicationContext: Start
ApplicationContext->>IoC Container: Scan Components
IoC Container->>Bean: Create Object
IoC Container->>Bean: Inject Dependencies
IoC Container-->>ApplicationContext: Ready
ApplicationContext-->>Application: Bean Available

Production Example – Banking Customer Management System

A banking application contains the following components:

  • Customer Controller
  • Customer Service
  • Customer Repository

Instead of creating objects manually,

Spring creates all beans during application startup.

Workflow

  • ApplicationContext starts.
  • Component scanning identifies annotated classes.
  • IoC Container creates beans.
  • Dependencies are injected automatically.
  • Beans are stored in the container.
  • Controllers use services without explicitly creating them.
flowchart LR

ApplicationContext --> CustomerController

ApplicationContext --> CustomerService

ApplicationContext --> CustomerRepository

CustomerController --> CustomerService

CustomerService --> CustomerRepository

CustomerRepository --> PostgreSQL

This architecture promotes loose coupling, maintainability, and easy testing, making it ideal for enterprise applications.


Key Takeaways

  • Spring Framework is a modular enterprise framework that simplifies Java application development.
  • Spring Core is the foundation of the Spring ecosystem and provides the IoC Container and Dependency Injection.
  • The IoC Container manages object creation, dependency injection, and bean lifecycle.
  • A Spring Bean is an object created and managed by the Spring container.
  • ApplicationContext is the preferred container because it extends BeanFactory with enterprise capabilities such as event publishing, AOP support, and internationalization.
  • Dependency Injection promotes loose coupling, easier testing, and better maintainability.
  • Spring's lightweight, modular architecture allows developers to use only the required components.
  • Mastering Spring Core concepts is essential before learning Spring Boot, Spring MVC, Spring Data JPA, Spring Security, and Spring Cloud.