๐Ÿ“˜ 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:

@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 ๐Ÿ’ช


This site uses Just the Docs, a documentation theme for Jekyll.