Spring Bean (註解 Annotation)

由 Spring IoC 容器管理的對象稱為 Bean 。Bean 是由 Spring IoC 容器實例化、組裝和管理的對象。應用 Annotation 可以將一個物件註冊成為 Bean。

使用 Annotation 的好處:

  • 只要在任意的 class 開頭定義好,Spring 啟動後就會自動去找,不需要一個一個匯入
  • 名稱就可以對應該 Class 要做甚麼或是被引入甚麼套件
  • Spring 會自動根據 Annotation 去將該 class 設定成特定的用途或是引入特定套件。

依照不同應用層面分類:

1. 配置與啟動

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
  • @SpringBootApplication:
    • 結合了 @Configuration、@EnableAutoConfiguration 和 @ComponentScan,是啟動 Spring Boot 應用的核心註解。
  • @Configuration:
    • 標記一個類為配置類,通常用來定義 @Bean 方法。
  • @ComponentScan:
    • 自動掃描並註冊被 @Component、@Service、@Repository 等註解的類別,通常在 @SpringBootApplication 中已隱式配置。
  • @EnableAutoConfiguration:
    • 讓 Spring Boot 根據加入的依賴自動配置 Spring 應用程式,通常已被包含在 @SpringBootApplication 中。

2. 依賴注入 (DI)

  • @Component:
    • 標記一個類別為 Spring 管理的組件(Bean),會自動加入到 Spring 容器中。
  • @Service:
    • @Component的一種,表示 Service 業務邏輯層的類別。
  • @Repository:
    • @Component的一種,表示 Dao 資料存取層的類別,並且會自動處理資料庫例外。
  • @Controller:
    • @Component的一種, 表示 Controller 端口控制層的類別,包含資料取得或是配合前端模板 Thymeleaf 等前端頁面呈現。
  • @Autowired:
    • 自動注入 Bean,各類別內部要使用已經註冊的 bean 就可以注入使用
  • @Bean
    • 針對方法類進行 bean 注冊
  • @Value:
    • 用來注入配置檔中的值,通常從 application.properties 或 application.yml 中獲取。

3. Web 與 RESTful API

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
  • @RestController:
    • 結合了 @Controller 和 @ResponseBody,表示這個類別是 RESTful Controller,方法返回的資料會直接轉換成 JSON 或 XML,用於純資料取得的 API 端口。
  • @RequestMapping:
    • 定義 Controller 中的 URL 規則,可以用於類別或方法上。也可以用更具體的註解如 @GetMapping, @PostMapping, @PutMapping, @DeleteMapping 來指定 HTTP 方法。
  • @PathVariable:
    • 用來擷取 URL 路徑中的參數,通常用在動態路徑中。
  • @RequestParam:
    • 用來擷取 URL 中的查詢參數或表單資料。
  • @RequestBody:
    • 用來將 HTTP 請求的 JSON 或 XML 資料直接轉換為 Java 物件,通常用於 POST/PUT 請求。
  • @ResponseBody:
    • 將方法的返回值直接轉換為 JSON 或 XML 並返回給前端,適合 RESTful API。

4. ORM, 資料庫操作

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
  • @Entity:
    • 標記一個類別為 JPA 實體,代表資料庫中的一個表格。
  • @Table:
    • 用來指定實體對應的資料表名稱,若不指定,預設會使用類名作為表名。
  • @Id:
    • 指定實體的主鍵欄位。
  • @GeneratedValue:
    • 指定主鍵的生成策略,常見的有 IDENTITY、SEQUENCE 等。
  • @Column:
    • 用來定義實體中屬性對應的資料庫欄位,包含名稱、長度等設定。
  • @Repository:
    • 用於標記資料存取層,搭配 Spring Data JPA 時,可以自動生成 CRUD 操作。
  • @Query:
    • 自定義查詢語句,適合需要複雜查詢的情境。

5. AOP 與異常處理

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
  • @Aspect:
    • 定義切面類別,用來實現橫切關注點。
  • @Before、@After、@Around:
    • 用來在方法執行的前、後或周圍插入額外的邏輯。
  • @ExceptionHandler:
    • 用於定義控制器中的異常處理方法。

6. 其他常用註解

  • @Valid, @Validated:
    1
    2
    3
    4
    5
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-validation</artifactId>
    </dependency>

    • 用於啟動 Bean 驗證,可限制參數或內部屬性輸入長度或是格式等等,通常和 @RequestBody 或 @RequestParam 搭配使用。

Ref: