๐ Study Material โ Week 5: Spring Boot
๐ 1. Introduction to Spring & Spring Boot
๐น What is Spring?
Spring is a Java framework used to build enterprise applications.
It provides:
- Dependency management
- Modular architecture
- Integration with databases, messaging systems, etc.
๐น What is Spring Boot?
Spring Boot is an extension of Spring that:
- Removes boilerplate configuration
- Provides auto-configuration
- Helps you start applications quickly
๐ In simple terms:
Spring = Powerful but complex Spring Boot = Spring made easy
๐ง 2. Core Concept: Inversion of Control (IoC)
๐น Traditional Approach
class OrderService {
PaymentService paymentService = new PaymentService();
}
Problem:
- Tight coupling
- Hard to test
- Hard to replace dependencies
๐น Spring Approach (IoC)
Spring creates and manages objects for you.
@Service
class OrderService {
private final PaymentService paymentService;
public OrderService(PaymentService paymentService) {
this.paymentService = paymentService;
}
}
๐ Key Idea
Instead of creating objects, you receive them from the framework
This is called Inversion of Control (IoC).
๐ 3. Dependency Injection (DI)
Dependency Injection is how IoC is implemented.
Types:
1๏ธโฃ Constructor Injection (Recommended)
@Service
class OrderService {
private final PaymentService paymentService;
public OrderService(PaymentService paymentService) {
this.paymentService = paymentService;
}
}
โ Best for:
- Immutability
- Testability
- Clean design
2๏ธโฃ Field Injection (Avoid in production)
@Autowired
private PaymentService paymentService;
โ Hard to test โ Not recommended
๐ 4. Spring Boot Application Structure
A standard Spring Boot application follows layered architecture:
Controller โ Service โ Repository
๐น Controller Layer (API Layer)
Handles HTTP requests.
@RestController
@RequestMapping("/expenses")
class ExpenseController {
@GetMapping
public String getAllExpenses() {
return "List of expenses";
}
}
๐น Service Layer (Business Logic)
@Service
class ExpenseService {
public String getExpenses() {
return "Business logic here";
}
}
๐น Repository Layer
(We will fully implement this in Week 6)
@Repository
class ExpenseRepository {
}
๐ 5. Building REST APIs
REST = Representational State Transfer
You expose endpoints that clients (UI, mobile apps) can call.
๐น Common HTTP Methods
| Method | Purpose |
|---|---|
| GET | Fetch data |
| POST | Create data |
| PUT | Update data |
| DELETE | Delete data |
๐น Example REST Controller
@RestController
@RequestMapping("/expenses")
class ExpenseController {
@GetMapping
public List<String> getAllExpenses() {
return List.of("Food", "Travel");
}
@PostMapping
public String createExpense() {
return "Expense created";
}
}
๐ฅ 6. Handling Request Data
๐น Query Params
GET /expenses?id=1
@GetMapping
public String getExpense(@RequestParam int id) {
return "Expense " + id;
}
๐น Path Variables
GET /expenses/1
@GetMapping("/{id}")
public String getExpense(@PathVariable int id) {
return "Expense " + id;
}
๐น Request Body (POST)
@PostMapping
public String createExpense(@RequestBody String expense) {
return "Created: " + expense;
}
๐ค 7. Response Handling
Spring automatically converts objects to JSON.
@GetMapping
public Map<String, String> getExpense() {
return Map.of("name", "Food", "amount", "100");
}
โ๏ธ 8. application.properties
Used for configuration.
Example:
server.port=8081
spring.application.name=expense-tracker
๐งช 9. Testing APIs (Important)
Use tools like:
- Postman
- curl
Example curl command:
curl -X GET http://localhost:8080/expenses
What to Validate:
- Correct response
- HTTP status codes
-
Edge cases:
- Invalid ID
- Missing data
- Empty response
๐งฑ 10. Project Setup (Quick Start)
Use Spring Initializr:
- Project: Maven
- Language: Java
-
Dependencies:
- Spring Web
Basic Application Class
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
โ ๏ธ 11. Common Mistakes to Avoid
โ Writing all logic in Controller โ Not using Service layer โ Field injection โ No error handling โ Large methods
๐ง 12. Best Practices
โ Use constructor injection โ Keep layers separate โ Use meaningful names โ Keep methods small โ Return proper responses
๐ 13. How This Connects to Next Week
Right now:
- Your APIs return dummy data
Next week:
- You will connect to a database
- Use JPA & Hibernate
- Store real data
๐ Summary
This week, you learned:
- What Spring Boot is
- IoC and Dependency Injection
- REST API basics
- Layered architecture
- Request & response handling
๐ Final Thought
You are no longer just writing Java programs.
You are now building real backend systems.
Focus on:
- Clean code
- Clear structure
- Understanding concepts deeply
Consistency is the key ๐ช