Java Frameworks: Spring Framework & Spring Boot 🚀

Java Frameworks: Spring Framework & Spring Boot 🚀
Spring is a powerful, lightweight framework for building enterprise-grade Java applications. It provides features like dependency injection, web development, and microservices support through Spring Boot.
1️⃣ Spring Framework Basics
✅ What is the Spring Framework?
Spring is a modular Java framework that provides:
✔ Dependency Injection (DI) – Simplifies object management.
✔ Spring MVC – For building web applications.
✔ Spring Boot – Simplifies Spring app development.
✔ Spring Data – Simplifies database interactions.
✔ Spring Security – Handles authentication & authorization.
2️⃣ Dependency Injection (IoC - Inversion of Control)
✅ What is Dependency Injection?
Instead of creating objects manually, Spring injects them automatically.
Example: Without Spring DI
public class Car {
private Engine engine = new Engine(); // Tight coupling
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
class Engine {
public void start() {
System.out.println("Engine Started!");
}
}
@Component
class Car {
private final Engine engine;
@Autowired // Spring automatically injects Engine
public Car(Engine engine) {
this.engine = engine;
}
public void drive() {
engine.start();
System.out.println("Car is driving!");
}
}
import org.springframework.web.bind.annotation.*;
@RestController
public class HelloController {
@GetMapping("/hello")
public String sayHello() {
return "Hello, Spring MVC!";
}
}
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MySpringBootApp {
public static void main(String[] args) {
SpringApplication.run(MySpringBootApp.class, args);
}
}
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/api")
public class ApiController {
@GetMapping("/greet")
public String greet() {
return "Hello, Spring Boot!";
}
}
org.springframework.boot
spring-boot-starter-data-jpa
mysql
mysql-connector-java
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.hibernate.ddl-auto=update
import jakarta.persistence.*;
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
// Getters and Setters
}
import org.springframework.data.jpa.repository.JpaRepository;
public interface UserRepository extends JpaRepository{
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/users")
public class UserController {
@Autowired
private UserRepository userRepository;
@GetMapping
public ListgetUsers() {
return userRepository.findAll();
}
@PostMapping
public User createUser(@RequestBody User user) {
return userRepository.save(user);
}
}
Date: 2025-03-28 00:00:00.000000