Module 1 - Spring Core
Module 1 - Spring Core
ChatGPT Auto
Hey Gpt I have my review meeting on spring core, for which I have
some topics that is provided by our mentors
You have to act as a experienced Spring professor who can easily
explain topics
I will give you that topics and you explain me in depth like I don't know
anything about spring is it clear
I’ll act as an experienced Spring professor and explain everything from absolute basics to deeper
concepts, assuming you don’t know anything about Spring. My explanations will include:
This way you’ll be able to explain confidently in your review meeting, not just memorize.
Great, let’s start with the most important concept in Spring. Almost everything in Spring Core is built
on IoC. If you understand this well, the rest of Spring becomes much easier.
Java
[Link] Page 1 of 54
Spring Core Review Prep 18/03/26, 1:11 PM
class Car {
Engine engine = new Engine();
void drive() {
[Link]();
}
}
Example:
Java
Java
Problems
1. Tight Coupling
2. Hard to test
3. Hard to maintain
4. Hard to replace dependencies
5. Object creation logic scattered everywhere
Instead of objects creating their dependencies themselves, the framework creates and provides
them.
Normally:
With IoC:
Simple Definition
IoC is a design principle where the control of object creation and dependency management is given to
a container/framework.
[Link] Page 2 of 54
Spring Core Review Prep 18/03/26, 1:11 PM
Without IoC:
chair
laptop
desk
internet
access card
With IoC:
Creates objects
Manages object lifecycle
Injects dependencies
Configures beans
Beans
A Bean is simply:
Java
[Link] Page 3 of 54
Spring Core Review Prep 18/03/26, 1:11 PM
class Engine {
void start() {
[Link]("Engine Started");
}
}
class Car {
Engine engine = new Engine();
void drive() {
[Link]();
[Link]("Car Driving");
}
}
Problem
Engine
Java
@Component
class Engine {
void start() {
[Link]("Engine Started");
}
}
Car
Java
@Component
class Car {
@Autowired
Engine engine;
void drive() {
[Link]();
[Link]("Car Driving");
}
}
What happens?
[Link] Page 4 of 54
Spring Core Review Prep 18/03/26, 1:11 PM
Example:
Java
@Component
class Engine {}
IoC Container
Container Description
Configuration Metadata
Spring needs instructions to create beans.
XML configuration
Java configuration
Annotations
Example:
@Component
@Service
@Repository
@Configuration
Start Application
↓
Load Configuration
↓
Create IoC Container
↓
Scan Classes
↓
Create Beans
↓
Resolve Dependencies
↓
Inject Dependencies
↓
Application Ready
[Link] Page 5 of 54
Spring Core Review Prep 18/03/26, 1:11 PM
Easy Testing
Better Maintainability
Modular Development
Different teams can build different modules.
Reusability
Components can be reused easily.
Concept Meaning
IoC is a design principle where the control of object creation and dependency management is transferred
from the application code to the Spring IoC container.
[Link] Page 6 of 54
Spring Core Review Prep 18/03/26, 1:11 PM
BeanFactory
ApplicationContext
Just send the next topic from Module 1 and we’ll continue like a Spring masterclass.
Now we move to the most practical concept in Spring Core. Almost every Spring application uses this
concept everywhere.
[Link] Page 7 of 54
Spring Core Review Prep 18/03/26, 1:11 PM
Example:
Java
class Engine {
void start(){
[Link]("Engine Started");
}
}
Java
class Car {
void drive(){
[Link]();
[Link]("Car Driving");
}
}
Here:
Java
class Car {
Engine engine = new Engine();
}
Issues
Tight Coupling
If tomorrow we change:
Engine → ElectricEngine
Difficult Testing
MockEngine
[Link] Page 8 of 54
Spring Core Review Prep 18/03/26, 1:11 PM
Hard to Maintain
Dependency Injection means providing dependencies to an object instead of the object creating
them itself.
Instead of this:
We do this:
Without DI:
You must:
grow vegetables
bake bread
cook patty
assemble burger
With DI:
[Link] Page 9 of 54
Spring Core Review Prep 18/03/26, 1:11 PM
Flow
Application Starts
↓
Spring Container Starts
↓
Beans Created
↓
Dependencies Identified
↓
Dependencies Injected
↓
Application Runs
Example
Java
@Component
class Engine {
void start(){
[Link]("Engine Started");
}
}
Java
@Component
class Car {
void drive(){
[Link]();
[Link]("Car Driving");
}
}
Car(Engine engine)
[Link] Page 10 of 54
Spring Core Review Prep 18/03/26, 1:11 PM
2. Setter Injection
Dependency is injected through setter method.
Example
Java
@Component
class Car {
@Autowired
public void setEngine(Engine engine){
[Link] = engine;
}
}
How it works
Example:
Java
@Component
class Car {
@Autowired
Engine engine;
}
[Link] Page 11 of 54
Spring Core Review Prep 18/03/26, 1:11 PM
7. What is @Autowired ?
@Autowired tells Spring:
Example:
Java
@Autowired
Engine engine;
@Component
@Service
@Repository
Creates beans
Looks for dependencies
Injects them
Example internally:
Controller
↓
Service
↓
Repository
↓
Database
Example:
Java
[Link] Page 12 of 54
Spring Core Review Prep 18/03/26, 1:11 PM
@RestController
class UserController {
2. Easy Testing
Example:
MockEngine
3. Better Maintainability
4. Reusability
5. Cleaner Code
Business logic is separated from object creation.
Dependency Injection is a design pattern where dependencies of a class are provided by an external
container instead of being created inside the class.
Constructor Injection
Setter Injection
Field Injection
[Link] Page 13 of 54
Spring Core Review Prep 18/03/26, 1:11 PM
Constructor Injection.
Spring Beans
Bean Lifecycle
Bean Scopes
ApplicationContext vs BeanFactory
Annotations ( @Component , @Service , @Repository )
Send the next topic from your mentor list, and I’ll explain it like a Spring architect preparing you for
review.
Spring Container
Now we reach the heart of Spring Core. Everything we studied before (IoC and DI) is actually handled by
the Spring Container.
If you understand this concept clearly, you’ll understand how Spring actually works internally.
It is responsible for:
[Link] Page 14 of 54
Spring Core Review Prep 18/03/26, 1:11 PM
1. Simple Definition
The Spring Container is responsible for creating, configuring, and managing beans in a Spring
application.
Java
In large enterprise systems with hundreds of classes, this becomes impossible to manage.
Internal Flow
Plain text
Application Starts
↓
Spring Container Starts
↓
Configuration is Loaded
↓
Beans are Created
↓
Dependencies Injected
↓
Application Ready
4. What is a Bean?
Inside the Spring container, objects are called Beans.
Definition:
[Link] Page 15 of 54
Spring Core Review Prep 18/03/26, 1:11 PM
A bean is an object that is created, managed, and configured by the Spring Container.
Example:
Java
@Component
class Engine {
}
Employees = Beans
HR Department = Spring Container
HR does:
Hiring employees
Assigning departments
Managing employees
Providing resources
Characteristics
Lightweight
Lazy initialization
Basic dependency injection
Lazy Initialization
Beans are created only when needed.
Example:
Plain text
Example
Java
[Link] Page 16 of 54
Spring Core Review Prep 18/03/26, 1:11 PM
Features of ApplicationContext
Dependency Injection
Event propagation
Internationalization (i18n)
AOP integration
Bean lifecycle management
Automatic bean post processing
Example
Java
ApplicationContext context =
new ClassPathXmlApplicationContext("[Link]");
Java
7. Configuration Metadata
Spring Container needs instructions to create beans.
XML
[Link] Page 17 of 54
Spring Core Review Prep 18/03/26, 1:11 PM
Example:
Java
@Component
class Engine {}
Java
@Component
class Car {
@Autowired
Engine engine;
}
3. Java-Based Configuration
Using **Java Configuration in Spring.
Example:
Java
@Configuration
class AppConfig {
@Bean
Engine engine(){
return new Engine();
}
}
Plain text
@Component
@Service
@Repository
@Controller
Step 1
Step 2
[Link] Page 18 of 54
Spring Core Review Prep 18/03/26, 1:11 PM
Step 3
Beans instantiated
Step 4
Dependencies injected
Step 5
Beans ready for use
Lifecycle Diagram
Plain text
Load Configuration
↓
Create Container
↓
Instantiate Beans
↓
Inject Dependencies
↓
Initialize Beans
↓
Application Ready
Java
@Component
class Engine {
}
Car
Java
@Component
class Car {
@Autowired
Engine engine;
Main Application
Java
[Link] Page 19 of 54
Spring Core Review Prep 18/03/26, 1:11 PM
ApplicationContext context =
new AnnotationConfigApplicationContext([Link]);
Plain text
Object Creation
Dependency Injection
Configuration Management
Reads configuration files.
The Spring Container is the core component of the Spring Framework responsible for creating,
configuring, and managing beans and their dependencies.
BeanFactory
ApplicationContext
[Link] Page 20 of 54
Spring Core Review Prep 18/03/26, 1:11 PM
ApplicationContext
Spring Beans
Bean Scopes
Bean Lifecycle
Autowired Annotation
Component Annotations
If you want, I can also explain “Spring Bean” next, which is directly connected to the container and is
almost always asked in reviews.
[Link] Page 21 of 54
Spring Core Review Prep 18/03/26, 1:11 PM
Bean Scopes
Bean Lifecycle
A Bean is simply a Java object that is created and managed by the Spring Container.
Example:
Java
@Component
class Engine {
}
Here Engine becomes a Spring Bean because the container manages it.
Bean class
Bean name (id)
Scope
Dependencies
Initialization method
Destruction method
Bean Definition
----------------------
Bean Name: engine
Class: Engine
Scope: Singleton
Dependencies: None
Init Method: init()
Destroy Method: destroy()
BeanDefinition
[Link] Page 22 of 54
Spring Core Review Prep 18/03/26, 1:11 PM
Example:
XML
Java
@Component
class Engine {
}
Java
@Component
class Car {
@Autowired
Engine engine;
Common annotations:
@Component
@Service
@Repository
@Controller
3. Java Configuration
Using @Configuration and @Bean .
Example:
Java
@Configuration
class AppConfig {
@Bean
Engine engine(){
return new Engine();
}
}
[Link] Page 23 of 54
Spring Core Review Prep 18/03/26, 1:11 PM
Start Application
↓
Load Configuration
↓
Scan Classes
↓
Create Bean Definitions
↓
Instantiate Beans
↓
Inject Dependencies
↓
Beans Ready to Use
Example:
Java
@Component
class Car {
@Autowired
Engine engine;
2. Bean Scopes
Now we discuss how long a bean lives inside the container.
Definition:
Example:
Java
@Component
@Scope("singleton")
class Engine {
}
[Link] Page 24 of 54
Spring Core Review Prep 18/03/26, 1:11 PM
Behavior:
Container
↓
Single Engine Object
↓
Shared across application
2. Prototype Scope
Definition:
Example:
Java
@Component
@Scope("prototype")
class Engine {
}
Behavior:
Example:
Example:
5. Application Scope
One bean instance for the entire web application.
3. Bean Lifecycle
[Link] Page 25 of 54
Spring Core Review Prep 18/03/26, 1:11 PM
Bean lifecycle describes how a bean is created, initialized, used, and destroyed.
Example:
Example:
Handled by:
BeanPostProcessor
Step 4: Initialization
Example:
Java
[Link] Page 26 of 54
Spring Core Review Prep 18/03/26, 1:11 PM
@PostConstruct
public void init(){
[Link]("Bean initialized");
}
Step 6: Destruction
Example:
Java
@PreDestroy
public void destroy(){
[Link]("Bean destroyed");
}
Java
@Component
class Engine {
@PostConstruct
public void init(){
[Link]("Engine initialized");
}
@PreDestroy
public void destroy(){
[Link]("Engine destroyed");
}
}
Lifecycle flow:
Create Engine
↓
Inject Dependencies
↓
@PostConstruct
↓
Bean Ready
↓
Application Running
↓
@PreDestroy
[Link] Page 27 of 54
Spring Core Review Prep 18/03/26, 1:11 PM
Bean Definition
Bean Scope
Common scopes:
Singleton (default)
Prototype
Request
Session
Application
Bean Lifecycle
Instantiation
Dependency Injection
Initialization
Usage
Destruction
Bean scope defines the lifecycle and number of bean instances in the container.
If you want, I can also explain two extremely important Spring Core topics that reviewers often ask
next:
[Link] Page 28 of 54
Spring Core Review Prep 18/03/26, 1:11 PM
Stereotype annotations
o @Component
o @Service
o @Repository
o @Controller
Your mentors often ask this because it shows whether you understand Spring architecture layers.
When Spring starts, it performs Component Scanning and automatically creates beans for these classes.
@Component
@Service
@Repository
@Controller
Example:
XML
Java
@Service
class UserService {
}
[Link] Page 29 of 54
Spring Core Review Prep 18/03/26, 1:11 PM
Example:
Java
@SpringBootApplication
@ComponentScan("[Link]")
Process:
Application Starts
↓
Spring scans package
↓
Finds @Component, @Service, etc.
↓
Creates Beans
↓
Stores them in IoC container
4. @Component
Definition
@Component is the base stereotype annotation.
It tells Spring:
Example
Java
@Component
class Engine {
When to Use
Example:
utility classes
helper classes
[Link] Page 30 of 54
Spring Core Review Prep 18/03/26, 1:11 PM
configuration helpers
5. @Service
Definition
Example
Java
@Service
class UserService {
Layer Architecture
Typical Spring architecture:
Controller
↓
Service
↓
Repository
↓
Database
It tells developers:
6. @Repository
Definition
Example
[Link] Page 31 of 54
Spring Core Review Prep 18/03/26, 1:11 PM
Java
@Repository
class UserRepository {
Special Feature
Example:
SQLException
↓
DataAccessException
7. @Controller
Definition
@Controller marks a class as a web controller.
Example
Java
@Controller
class UserController {
@Autowired
UserService userService;
Flow:
Client Request
↓
Controller
↓
Service
↓
Repository
↓
Database
[Link] Page 32 of 54
Spring Core Review Prep 18/03/26, 1:11 PM
UserController
UserService
UserRepository
Controller
Java
@Controller
class UserController {
@Autowired
UserService service;
Service
Java
@Service
class UserService {
@Autowired
UserRepository repository;
Repository
Java
@Repository
class UserRepository {
Spring automatically creates three beans and connects them using Dependency Injection.
@Component
↑
|
--------------------------------
| | |
@Service @Repository @Controller
[Link] Page 33 of 54
Spring Core Review Prep 18/03/26, 1:11 PM
3. Cleaner Configuration
No XML configuration required.
4. Easier Maintenance
Clear separation of concerns.
Stereotype annotations are special annotations used to mark classes as Spring-managed components so
they can be automatically detected during component scanning.
@Service
@Repository
@Controller
[Link] Page 34 of 54
Spring Core Review Prep 18/03/26, 1:11 PM
Stereotype annotations mark classes as Spring beans and identify their role in the application
architecture.
Since you finished almost all Spring Core fundamentals, the next topics mentors often ask after
this are:
If you want, I can also give you a “10 most common Spring Core review questions” list that mentors
usually ask trainees.
It will help you prepare answers confidently for your review meeting.
• Configuration types
o XML configuration
o Java configuration
o Annotation configuration
XML Configuration
Java Configuration
Annotation Configuration
Understanding these is important because they explain how beans are registered in the Spring
Container (usually an ApplicationContext).
Spring configuration is the process of defining beans and their dependencies so the Spring
[Link] Page 35 of 54
Spring Core Review Prep 18/03/26, 1:11 PM
XML
<beans>
</beans>
Java Classes
Java
class Engine {
}
Java
class Car {
[Link] Page 36 of 54
Spring Core Review Prep 18/03/26, 1:11 PM
Read [Link]
↓
Create Engine Bean
↓
Create Car Bean
↓
Inject Engine into Car
Java
ApplicationContext context =
new ClassPathXmlApplicationContext("[Link]");
Advantages
Explicit configuration
Easy to visualize bean relationships
Disadvantages
Too much XML
Hard to maintain in large applications
Not type-safe
Because of these issues, XML configuration is rarely used in modern Spring Boot applications.
@Component
@Service
@Repository
@Controller
Example
Engine
Java
@Component
class Engine {
[Link] Page 37 of 54
Spring Core Review Prep 18/03/26, 1:11 PM
Car
Java
@Component
class Car {
@Autowired
Engine engine;
Component Scanning
Spring scans packages to find annotations.
Example:
Java
@ComponentScan("[Link]")
Process:
Application Starts
↓
Spring scans package
↓
Finds annotated classes
↓
Creates Beans
↓
Injects dependencies
Advantages
✔ Less configuration
✔ Cleaner code
✔ Easy dependency injection
✔ Better readability
4. Java Configuration
Java configuration uses Java classes instead of XML to define beans.
It is done using:
@Configuration
@Bean
Example
Configuration Class
Java
[Link] Page 38 of 54
Spring Core Review Prep 18/03/26, 1:11 PM
@Configuration
class AppConfig {
@Bean
public Engine engine(){
return new Engine();
}
@Bean
public Car car(){
return new Car(engine());
}
Java Classes
Java
class Engine {
}
Java
class Car {
Java
ApplicationContext context =
new AnnotationConfigApplicationContext([Link]);
Load AppConfig
↓
Call engine()
↓
Create Engine Bean
↓
Call car()
↓
Inject Engine into Car
[Link] Page 39 of 54
Spring Core Review Prep 18/03/26, 1:11 PM
Annotations
+
Java Configuration
Example:
@SpringBootApplication
@Configuration
@ComponentScan
@EnableAutoConfiguration
Load Configuration
↓
Create Bean Definitions
↓
Initialize Spring Container
↓
Create Beans
↓
Inject Dependencies
↓
Application Ready
1. XML configuration
2. Annotation configuration
3. Java configuration
[Link] Page 40 of 54
Spring Core Review Prep 18/03/26, 1:11 PM
@Configuration indicates that a class contains bean definitions for the Spring container.
@Bean tells Spring that the method returns an object that should be managed as a Spring bean.
9. One-Line Summary
Spring configuration defines how beans are created and wired inside the Spring container using
XML, annotations, or Java classes.
Since you finished almost the entire Spring Core Module, the last thing that will help you in your
review meeting is usually:
If you want, I can also give you a “complete Spring Core review cheat sheet” so you can revise
everything in 5 minutes before your review.
• @Autowired, @Qualifier, @Primary
@Autowired
@Qualifier
@Primary
1. @Autowired
Definition
@Autowired is used to automatically inject dependencies from the Spring container.
Instead of manually creating objects, Spring finds the required bean and injects it.
Example
Engine Class
Java
@Component
class Engine {
}
Car Class
[Link] Page 41 of 54
Spring Core Review Prep 18/03/26, 1:11 PM
Java
@Component
class Car {
@Autowired
Engine engine;
Field Injection
Java
@Autowired
Engine engine;
Java
@Component
class Car {
@Autowired
public Car(Engine engine){
[Link] = engine;
}
Modern Spring can even omit @Autowired if there is only one constructor.
Setter Injection
Java
@Autowired
public void setEngine(Engine engine){
[Link] = engine;
}
[Link] Page 42 of 54
Spring Core Review Prep 18/03/26, 1:11 PM
DieselEngine
Java
@Component
class DieselEngine implements Engine {
}
ElectricEngine
Java
@Component
class ElectricEngine implements Engine {
}
Car Class
Java
@Component
class Car {
@Autowired
Engine engine;
DieselEngine
ElectricEngine
NoUniqueBeanDefinitionException
This means:
3. @Qualifier
Definition
@Qualifier tells Spring exactly which bean should be injected.
[Link] Page 43 of 54
Spring Core Review Prep 18/03/26, 1:11 PM
Example
DieselEngine
Java
@Component("dieselEngine")
class DieselEngine implements Engine {
}
ElectricEngine
Java
@Component("electricEngine")
class ElectricEngine implements Engine {
}
Car Class
Java
@Component
class Car {
@Autowired
@Qualifier("dieselEngine")
Engine engine;
What Happens
DieselEngine
instead of ElectricEngine.
4. @Primary
Definition
@Primary marks a bean as the default bean when multiple beans exist.
Example
DieselEngine
Java
@Component
@Primary
class DieselEngine implements Engine {
}
[Link] Page 44 of 54
Spring Core Review Prep 18/03/26, 1:11 PM
ElectricEngine
Java
@Component
class ElectricEngine implements Engine {
}
Car Class
Java
@Component
class Car {
@Autowired
Engine engine;
What Happens
DieselEngine
@Qualifier
@Primary
Bean Name
Type Matching
Example:
[Link] Page 45 of 54
Spring Core Review Prep 18/03/26, 1:11 PM
Controller
↓
Service
↓
Repository
Example:
Repository
Java
@Repository
class UserRepository {
}
Service
Java
@Service
class UserService {
@Autowired
UserRepository repository;
This is incorrect.
Correct understanding:
When multiple beans of the same type exist and we want to specify which bean should be injected.
[Link] Page 46 of 54
Spring Core Review Prep 18/03/26, 1:11 PM
Since you have now covered almost the entire Spring Core module, if you want I can also give you:
A complete Spring Core architecture diagram
15 tricky questions mentors ask in review meetings
A 5-minute revision sheet for everything you studied
Normal Java:
Spring IoC:
Purpose
Reduce tight coupling
Improve maintainability
Simplify dependency management
Example Concept
[Link] Page 47 of 54
Spring Core Review Prep 18/03/26, 1:11 PM
Dependency Injection means providing dependencies to a class instead of the class creating them
itself.
Types of DI
Type Description
Example:
Java
@Component
class Car {
@Autowired
Engine engine;
3. Spring Container
The Spring Container is responsible for:
Creating beans
Managing dependencies
Handling lifecycle
Configuring application components
ApplicationContext
Types of Containers
Container Description
Container Flow
[Link] Page 48 of 54
Spring Core Review Prep 18/03/26, 1:11 PM
Start Application
↓
Load Configuration
↓
Create Beans
↓
Inject Dependencies
↓
Application Ready
Example:
Java
@Component
class Engine {
}
Bean Definition
Bean definition contains metadata such as:
Class name
Bean id
Scope
Dependencies
Lifecycle methods
5. Bean Scopes
Bean scope defines how many instances of a bean exist in the container.
Common Scopes
[Link] Page 49 of 54
Spring Core Review Prep 18/03/26, 1:11 PM
Scope Description
Example:
Java
@Scope("prototype")
6. Bean Lifecycle
The lifecycle describes how beans are created and destroyed.
Lifecycle Steps
Lifecycle Annotations
7. Stereotype Annotations
These annotations mark classes as Spring-managed components.
@Component
@Service
@Repository
@Controller
Purpose
[Link] Page 50 of 54
Spring Core Review Prep 18/03/26, 1:11 PM
Annotation Layer
8. Configuration Types
Spring supports three configuration styles.
1. XML Configuration
Old style using XML files.
Example:
XML
2. Annotation Configuration
@Component
@Autowired
@Service
3. Java Configuration
Annotations used:
@Configuration
@Bean
Example:
Java
@Configuration
class AppConfig {
@Bean
Engine engine(){
return new Engine();
}
[Link] Page 51 of 54
Spring Core Review Prep 18/03/26, 1:11 PM
@Autowired
Example:
Java
@Autowired
Engine engine;
Problem
DieselEngine
ElectricEngine
@Qualifier
Example:
Java
@Autowired
@Qualifier("dieselEngine")
Engine engine;
@Primary
Example:
Java
@Primary
class DieselEngine
Priority Order
[Link] Page 52 of 54
Spring Core Review Prep 18/03/26, 1:11 PM
Application Starts
↓
Spring Container Starts
↓
Configuration Loaded
↓
Beans Created
↓
Dependencies Injected
↓
Bean Lifecycle Managed
↓
Application Runs
Bean Lifecycle →
Instantiation → Injection → Init → Destroy
Stereotype Annotations →
@Component
@Service
@Repository
@Controller
Configuration Types →
XML
Annotations
Java Configuration
If you can explain these clearly, you’ve mastered Spring Core basics.
If you want, I can also give you “20 Spring Core review questions mentors commonly ask trainees” so
you can practice answering them before your meeting.
[Link] Page 53 of 54
Spring Core Review Prep 18/03/26, 1:11 PM
[Link] Page 54 of 54