Spring

Framework หลักของ Java สำหรับสร้าง backend application ใช้ Dependency Injection ผ่าน IoC Container เป็นหัวใจหลัก

จุดเด่น

  • เป็นรากฐานของ Spring ecosystem ทั้งหมด — Spring Boot ต่อยอดมาจากนี้
  • เสาหลัก 3 อย่าง: IoC/DI, AOP, MVC
  • ต้อง config เองเยอะกว่า Spring Boot (XML / Java Config)

หัวใจหลัก

1. IoC / Dependency Injection

Spring จัดการสร้าง object (Bean) และ inject ให้อัตโนมัติ ไม่ต้อง new เอง

@Service
public class OrderService {
    @Autowired  // Spring หา PaymentService มา inject ให้
    private PaymentService payment;
}

2. AOP (Aspect-Oriented Programming)

ตัดเรื่องที่ซ้ำ ๆ ออกมาจัดการที่เดียว เช่น logging, security, transaction

3. MVC (Model-View-Controller)

โครงสร้างสำหรับสร้าง web application / REST API

Spring vs Spring Boot

ด้านSpringSpring Boot
ตั้งค่าconfig เองเยอะ (XML / Java Config)auto-configuration
เริ่มต้นยุ่งยาก ต้องตั้งค่าเยอะง่าย — Spring Initializr
เปรียบเทียบซื้อชิ้นส่วนคอมมาประกอบเองซื้อคอมประกอบสำเร็จพร้อมใช้

ปัจจุบันส่วนใหญ่ใช้ Spring Boot เพราะสะดวกกว่ามาก แต่ข้างในก็คือ Spring

Ecosystem

  • Spring CoreIoC Container, Dependency Injection
  • Spring Boot — ตั้งค่าอัตโนมัติ เริ่มต้นเร็ว
  • Spring Data — จัดการ database ง่าย ๆ ด้วย JPA
  • Spring Security — authentication, authorization
  • Spring Cloud — สำหรับ microservices
  • Java — ภาษาที่ Spring ใช้
  • Spring Boot — เวอร์ชันใช้งานง่าย
  • IoC Container — หัวใจของ Spring
  • Dependency Injection — pattern หลัก
  • Framework — Spring เป็น framework ตัวอย่างที่ดี
  • OSIV — พฤติกรรม default ของ Spring Boot