0% found this document useful (0 votes)
2 views24 pages

Notes

The document provides an introduction to Spring and Spring Boot, explaining the fundamentals of web application communication through client-server architecture and HTTP protocols. It discusses the role of servlets in handling HTTP requests and the evolution of Java frameworks, particularly Spring, to simplify enterprise application development. Key concepts such as dependency injection, bean management, and the Spring ecosystem are highlighted as essential for building maintainable Java applications.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views24 pages

Notes

The document provides an introduction to Spring and Spring Boot, explaining the fundamentals of web application communication through client-server architecture and HTTP protocols. It discusses the role of servlets in handling HTTP requests and the evolution of Java frameworks, particularly Spring, to simplify enterprise application development. Key concepts such as dependency injection, bean management, and the Spring ecosystem are highlighted as essential for building maintainable Java applications.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

C

od
Introduction to Spring &

er
SpringBoot

Ar
m
1. The Starting Point: How Web Applications

y
Communicate
Before understanding Spring, Spring Boot, Servlet, or any backend framework, we
first need to understand one basic question:
How does a user sitting on one machine communicate with code running on
another machine?
This is the foundation of web development.
When you open a browser and type:

[Link]

your browser is running on your laptop or mobile phone, while Amazon’s


application is running on Amazon’s server somewhere else.
At the most basic level:

Your Browser ---- talks to ---- Amazon Server

This structure is called Client–Server Architecture.

2. Client–Server Architecture
What is a Client?
A client is the side that asks for something.
Examples of clients:

Introduction to Spring & SpringBoot 1


C
od
er
Browser
Mobile app

Ar
Postman
Frontend React app
Android app

m
iOS app

y
When you open a website, your browser becomes the client.
It sends a request like:
“Hey server, give me this webpage.”

What is a Server?
A server is the side that receives requests, processes them, and sends back a
response.
Examples of servers:

Amazon server
YouTube server
Bank server
Your Spring Boot application

A server usually performs tasks like:

Checking login details


Fetching data from a database
Applying business rules
Saving information
Returning a response

The basic idea is simple:

Client asks.
Server responds.

Introduction to Spring & SpringBoot 2


C
od
3. HTTP: The Language of the Web

er
Now the important question is:

Ar
How does the browser know how to ask?

m
How does the server understand what the browser is asking?

y
For communication to happen properly, both sides need a common language.
That common language is called HTTP.

What is HTTP?
HTTP stands for:

HyperText Transfer Protocol

In simple words:
HTTP is the rulebook for communication between a client and a server.
HTTP defines:

How a request should look


How a response should look
Which method is being used
Which URL is being called
What data is being sent
Which status code is returned

So the browser and server do not randomly exchange text. They follow a proper
format.
HTTP is an application-layer protocol that works on top of TCP/IP.

4. Request–Response Cycle
Every web interaction follows the same basic pattern:

Introduction to Spring & SpringBoot 3


C
od
er
Client sends request
Server processes request

Ar
Server sends response
Client displays or uses the response

m
Example:

y
[Link]/courses

The browser may send an HTTP request like:

GET /courses
Host: [Link]

The server receives the request and understands:

The user wants the courses page.


Fetch the courses data.
Prepare the response.
Send it back to the browser.

The server may send back:

Status: 200 OK
Body: course data / HTML page / JSON

The browser receives the response and displays the page.

5. Anatomy of an HTTP Request


An HTTP request usually contains four main parts:

Method
URL or path

Introduction to Spring & SpringBoot 4


C
od
Headers
Body

er
Ar
Example:

m
y
POST /login
Content-Type: application/json

{
"email": "abc@[Link]",
"password": "12345"
}

Meaning:

POST → I am sending data


/login → I want to call the login functionality
Headers → Extra information about the request
Body → Actual data being sent

HTTP Methods
The first line of an HTTP request is called the request line.
It contains the method, path, and HTTP version.
The method tells the server what action the client wants to perform.
Method Meaning Example Use
GET Read data Fetch a list of orders
POST Create data Place a new order
PUT Replace data completely Update an entire user profile
PATCH Update data partially Change only the phone number
DELETE Remove data Cancel an order

HTTP Headers

Introduction to Spring & SpringBoot 5


C
od
Headers are key-value pairs that provide extra information about the request.

er
They tell the server things like:

Ar
m
What format the client can understand
What format the request body is in

y
Who the client is
Which host the client is trying to reach

Common examples:

Accept: application/json
Content-Type: application/json
Authorization: Bearer token
Host: [Link]

Headers are very important in real Spring applications because we frequently


work with authentication, JSON data, API communication, and request metadata.

HTTP Body
The body carries the actual data being sent by the client.
It is commonly used with:

POST
PUT
PATCH

Example body:

{
"name": "Rohit",
"email": "rohit@[Link]"
}

In modern APIs, the request body is usually sent in JSON format.

Introduction to Spring & SpringBoot 6


C
od
GETrequests usually do not have a body because they are mainly used to fetch

er
data.

Ar
6. Anatomy of an HTTP Response

m
y
An HTTP response usually contains:

Status code
Headers
Body

Example:

HTTP/1.1 200 OK
Content-Type: application/json

{
"message": "Login successful"
}

The most important part of the response is the status code.


Status Code Meaning
200 Request successful
404 Resource not found
500 Internal server error
We will study status codes in more detail later.

7. JVM: A Java Program Runs Locally


Now let’s compare this web communication model with a normal Java program.
Example:

public class Main {


public static void main(String[] args) {

Introduction to Spring & SpringBoot 7


C
od
[Link]("Hello World");

er
}
}

Ar
m
When we run this program:

y
java Main

it runs inside the JVM, which means Java Virtual Machine.


Your .java source file is compiled by javac into platform-independent bytecode
stored in .class files.
The JVM loads this bytecode and executes it.
This is the reason Java is known for:

Write once, run anywhere

The same bytecode can run on Windows, macOS, or Linux as long as the JVM is
available.

8. Normal Java Program vs Web Application


A normal Java program usually follows this pattern:

Start
Run instructions
Finish
Exit

But a website or backend application follows a very different pattern:

Start
Keep running
Wait for requests
Process requests

Introduction to Spring & SpringBoot 8


C
od
Send responses
Continue running

er
Ar
A website is not like a program that runs once and exits.

m
A web server is a program that stays alive continuously and keeps listening for
incoming requests.

y
9. Why Core Java Alone Is Not Enough for
Web Applications
Core Java understands concepts like:

Classes
Objects
Inheritance
Collections
Threads
Files

But Core Java does not automatically understand web concepts like:

HTTP requests
URLs
Headers
Cookies
Sessions
REST APIs

These are web-related concepts, not basic Java language concepts.


Even if we keep a Java program running using something like:

while (true) {
// keep program alive
}

it still does not automatically understand a request like:

Introduction to Spring & SpringBoot 9


C
od
er
GET /hello

Ar
Someone has to read that request, understand it, and map it to the correct Java

m
logic.

y
10. Can Java Open Network Connections?
Yes, Java can do networking.
Java has had the [Link] package since Java 1.0.
For example:

ServerSocket server = new ServerSocket(8080);

This allows a Java program to listen on port 8080 .


So technically, Java can communicate over a network.
But there is still a problem.
When a browser or Postman sends an HTTP request, Java receives a raw TCP
connection — basically a stream of bytes.
Example request:

GET /users HTTP/1.1


Host: localhost:8080

To the JVM, this is just data.


It does not automatically understand:

GET means fetch data


/users is an endpoint
Host is a header

Someone has to interpret all of this manually.

Introduction to Spring & SpringBoot 10


C
od
11. What We Would Need to Do Manually in

er
Core Java

Ar
If we tried to build a web application using only Core Java, we would have to

m
handle many things ourselves.

y
We would need to:
1. Open a port using ServerSocket

2. Read raw input streams


3. Parse the HTTP request manually
4. Extract the method, URL, headers, and body
5. Route the request to the correct Java logic
6. Create the HTTP response in the correct format
7. Manage multiple users using threads
8. Handle errors, malformed requests, and connection behavior
Example of manual routing:

if ([Link]("/users")) {
// call user logic
} else if ([Link]("/orders")) {
// call order logic
}

This code has nothing to do with actual business logic.


It is repeated technical work that every Java web application would need.
That is why a standard solution was needed.

12. The Problem Before Servlets


We had this gap:

Introduction to Spring & SpringBoot 11


C
od
er
Browser
|

Ar
HTTP Request
|
v

m
???
|

y
Java Code

The browser sends HTTP requests.


Java contains classes and methods.
But something is needed between HTTP and Java to translate web requests into
Java method calls.
For example, if a browser sends:

GET /hello HTTP/1.1


Host: localhost:8080

how should this request trigger a Java method like:

sayHello();

This translation is handled by:

Servlet Container
Servlet

13. Java Servlets


A Servlet is a Java object that can handle HTTP requests.
In simple words:
A Servlet is a special Java class designed for web applications.

Introduction to Spring & SpringBoot 12


C
od
Conceptually:

er
Ar
HTTP Request
|

m
v
Servlet

y
|
v
Java Code

Servlets were one of the first standard Java technologies created specifically for
building web applications.
A Servlet runs inside a Servlet Container.
Examples of Servlet Containers:

Apache Tomcat
Jetty
Undertow

14. What Does a Servlet Container Do?


A Servlet Container sits between the outside web world and your Java code.
It handles the low-level web work for you.
A Servlet Container is responsible for:

Opening a port such as 8080


Listening for HTTP requests
Reading TCP bytes
Parsing HTTP requests
Creating request and response objects
Managing threads
Calling servlet methods
Sending HTTP responses
Handling connection behavior

Introduction to Spring & SpringBoot 13


C
od
Instead of manually reading bytes and parsing HTTP text, the Servlet Container

er
gives us Java-friendly objects.

Ar
For example, it can call methods like:

m
doGet()

y
doPost()

So the request:

GET /hello

can eventually be handled by Java code inside a servlet.

15. Why Was Spring Needed Then?


Servlets solved an important problem.
They made it possible for Java applications to handle HTTP requests properly.
But building large enterprise applications directly with Servlets became difficult
over time.
Common problems included:

Too much boilerplate code


Too many configurations
Tight coupling between classes
Difficult testing
Difficult maintenance in large applications
Repeated code across projects

As applications became bigger, developers needed a better way to organize code,


manage objects, handle dependencies, and reduce configuration complexity.
This is where the Spring Framework became important.

16. What is Spring Framework?


Introduction to Spring & SpringBoot 14
C
od
Spring Framework is one of the most popular frameworks in the Java world.

er
It was created to make enterprise Java development easier, cleaner, and more

Ar
maintainable.
Spring introduced important concepts like:

m
y
IoC
Dependency Injection
Bean Management
Configuration
Loose Coupling

We will study these concepts in depth later in the series.


For now, remember:
Spring helps us build Java applications in a cleaner and more manageable way.

17. Spring is an Ecosystem


Spring is not just one small library.
Spring is a large ecosystem of projects and frameworks.
Different Spring projects solve different problems.
Some important parts of the Spring ecosystem are:

Spring Core
Spring MVC
Spring Data
Spring Security
Spring AOP
Spring Boot
Spring AI

18. Spring Core


Spring Core is the foundation of the Spring ecosystem.

Introduction to Spring & SpringBoot 15


C
od
It provides the most basic and important features of Spring.

er
Spring Core includes:

Ar
m
IoC
Dependency Injection

y
Bean Management
Configuration
ApplicationContext

Without Spring Core, other Spring projects would not exist.


Spring Core is the base on which many other Spring modules are built.

19. Spring MVC


Spring MVC is used to build web applications and REST APIs.
It is built on top of:

Servlets
+
Spring Core

Spring MVC makes it easier to handle web requests.


Instead of writing Servlet code directly, we can use clean annotations and
controller classes.
For example, later we will write code like:

@GetMapping("/hello")
public String sayHello() {
return "Hello World";
}

Spring MVC internally uses Servlet technology, but it gives developers a cleaner
programming model.

Introduction to Spring & SpringBoot 16


C
od
20. Spring Data

er
Most applications need to store data permanently.

Ar
For that, applications usually need a database.

m
Earlier, Java developers commonly used JDBC.

y
With JDBC, developers had to:

Write SQL manually


Open database connections
Execute queries
Handle result sets
Close resources
Manage repetitive database code

Later, frameworks like Hibernate made database work easier.


Hibernate maps Java objects to database tables.
This concept is called Object-Relational Mapping, or ORM.

JPA and Hibernate


JPA stands for:

Java Persistence API

JPA is a specification.
That means it defines rules and guidelines for how Java objects should be
mapped to database tables.
But JPA itself does not provide the actual working implementation.
Hibernate is one of the most popular implementations of JPA.
In simple words:

JPA tells what should be done.


Hibernate actually does it.

Introduction to Spring & SpringBoot 17


C
od
Spring Data JPA goes one step further and reduces even more boilerplate code.

er
The flow looks like this:

Ar
m
Spring Data JPA → Hibernate → JDBC → Database

y
A simple way to remember:
JDBC crawled so Hibernate could walk, and Hibernate walked so Spring Data
JPA could fly.

21. Spring Security


Spring Security is used for authentication and authorization.
It helps with features like:

Login
JWT
OAuth
Roles
Permissions
Password encoding
CSRF protection
Access control

Without Spring Security, developers would have to manually write a lot of


sensitive and repetitive security code.
Spring Security gives a standard and powerful way to secure Java applications.

22. Spring AOP


AOP stands for:

Aspect-Oriented Programming

Introduction to Spring & SpringBoot 18


C
od
Spring AOP helps us separate cross-cutting concerns from business logic.

er
Examples of cross-cutting concerns:

Ar
m
Logging
Security checks

y
Transaction management
Performance tracking
Exception handling

These are things that may be needed across many parts of an application.
We will study AOP properly later in the series.

23. Spring AI
Spring AI is a newer part of the Spring ecosystem.
It helps Java developers integrate AI features into Spring applications.
It can work with:

OpenAI
Gemini
Anthropic
Vector databases
RAG systems
Embeddings
AI chat models

This is useful when building AI-powered applications using Java and Spring.

24. What is Spring Boot?


Spring Boot is not a replacement for Spring.
Spring Boot is an automation layer on top of Spring.
It helps developers create Spring applications faster by providing:

Introduction to Spring & SpringBoot 19


C
od
er
Auto-configuration
Starter dependencies

Ar
Embedded servers
Sensible defaults
Production-ready features

m
Less manual configuration

y
In simple words:
Spring Boot configures Spring for us so we can start building applications
quickly.

25. Spring Boot is Opinionated


Spring Boot makes many default assumptions.
These assumptions are called opinions.
An opinionated framework provides sensible defaults so developers do not have
to configure everything manually.
For example, if we add a web dependency, Spring Boot assumes that we want to
build a web application.
So it can automatically configure things like:

Embedded Tomcat
Spring MVC setup
Default application structure
JSON support
Basic error handling

This saves a lot of time.


But the real skills are still in understanding:

Spring Core
Spring MVC
Dependency Injection
IoC
Beans

Introduction to Spring & SpringBoot 20


C
od
Servlets
HTTP

er
Database concepts
Security concepts

Ar
m
Spring Boot makes development faster, but Spring fundamentals make you a

y
stronger developer.

26. Spring Boot vs Spring Framework


A common confusion is:
Are Spring and Spring Boot the same?
No.
Spring Framework provides the core features and different modules.
Spring Boot makes Spring easier to use by reducing configuration and setup work.
Simple comparison:
Spring Framework Spring Boot
Provides core features and modules Provides auto-configuration and quick setup
Requires more manual configuration Reduces manual configuration
Gives flexibility Gives sensible defaults
Foundation of the ecosystem Built on top of Spring
So we can say:

Spring Boot uses Spring.


Spring Boot does not replace Spring.

27. Where Do Microservices Fit?


Microservices are not a separate Spring module.
Microservices are an architecture style.

Introduction to Spring & SpringBoot 21


C
od
In a microservices architecture, a large application is divided into smaller

er
independent services.

Ar
For example:

m
User Service

y
Order Service
Payment Service
Notification Service
Product Service

Each service can be developed, deployed, and scaled independently.


Spring Boot is commonly used to build microservices because it makes it easy to
create independent production-ready applications.
So the idea is:

Microservices = Architecture style


Spring Boot = Tool commonly used to build them

28. Complete Flow: From Browser to Spring


Boot
Now we can connect the whole journey.

Browser sends HTTP request


|
v
Servlet Container receives request
|
v
Servlet technology handles web communication
|
v
Spring MVC gives a cleaner web programming model
|
v
Spring Core manages objects and dependencies
|
v

Introduction to Spring & SpringBoot 22


C
od
Spring Boot auto-configures everything
|

er
v
Developer writes business logic

Ar
m
This is the bigger picture behind modern Java backend development.

y
29. Why Understanding This History
Matters
Many beginners start directly with Spring Boot and write code like:

@RestController
@GetMapping("/hello")

The code works, but they do not understand what is happening behind the
scenes.
When we understand the journey from:

Client–Server Architecture
HTTP
JVM
Core Java networking
Servlets
Spring Framework
Spring Boot

Spring Boot no longer feels magical.


It starts making sense.
We understand why each technology came into the picture and what problem it
solved.

30. Final Summary


The complete evolution looks like this:

Introduction to Spring & SpringBoot 23


C
od
er
Client–Server Architecture

Ar
HTTP communication

Core Java limitation for web apps

m

Java networking with sockets

y

Manual HTTP parsing problem

Servlets and Servlet Containers

Spring Framework

Spring ecosystem

Spring Boot

Key takeaways:

Client sends a request.


Server sends a response.
HTTP defines the communication format.
Core Java does not automatically understand HTTP.
Servlets helped Java handle web requests.
Servlet Containers handle low-level web work.
Spring made enterprise Java development cleaner.
Spring Core manages objects and dependencies.
Spring MVC simplifies web development.
Spring Data simplifies database access.
Spring Security handles authentication and authorization.
Spring Boot auto-configures Spring applications.
Microservices are an architecture style, not a Spring module.

The goal of this series is not just to write Spring Boot code.
The goal is to understand how modern Java backend development actually works.
Once this foundation is clear, Spring Boot becomes much easier to learn, debug,
and use in real projects.

Introduction to Spring & SpringBoot 24

You might also like