➜ Old React website
Chung Cheuk Hang MichaelJava Web Developer
Azure Workload Identity用 Vite 建立 React 網站

80 個 Spring Boot annotations

Table of contents

1 Spring Boot 常用 annotations

1.1 啟動、bean 註冊

Annotation常用目標用途
@SpringBootApplicationClass放置喺 main class,用黎啟動 Spring Boot application,裡面包含 @EnableAutoConfiguration@ComponentScan
@EnableAutoConfigurationClass通常放喺 main class,用黎啟用 Spring 既 auto-configuration 功能。
@ComponentScanClass通常放喺 main class,可以用 basePackages attribute 黎令 Spring Boot 掃瞄指定既 packages 去搵 beans 註冊。
@ComponentClass用黎註冊一個 class 做一個 Spring bean。Spring Boot 啟動既時候會自動 instantiate bean object。
@ServiceClass包含左 @Component,用黎註冊一個 class 做一個 Spring bean,通常會帶有 business logic。對應 domain-driven design 既 service。
@ConfigurationClass包含左 @Component,用黎註冊一個 class 做一個 Spring bean,通常裡面既 methods 會有 @Bean
@BeanMethod用黎註冊一個 method 既 return value 做一個 Spring bean,通常會寫喺 @Configuration 既 class 裡面。
@ProfileClass、method用黎表示呢個 Spring bean 只會喺某啲 profiles 生效既情況下被註冊。
@ConditionalOnPropertyClass、method用黎表示呢個 Spring bean 只會喺某啲 configuration properties match 到特定 value 既情況下被註冊。
@ConditionalOnExpressionClass、method用黎表示呢個 Spring bean 只會喺個 SpEL expression 係 true 既情況下被註冊。
@ConditionalOnBeanClass、method用黎表示呢個 Spring bean 只會喺某啲 Spring beans 存在既情況下被註冊。
@ConditionalOnMissingBeanClass、method用黎表示呢個 Spring bean 只會喺某啲 Spring beans 唔存在既情況下被註冊。
參考資料:

1.2 配置

Annotation常用目標用途
@ValueField用黎讀取一個 Spring configuration property,來源通常係 application[-profile].(yml/properties)、program arguments、environment variables、Spring Cloud Config Server 等等。其他 Spring beans 必須經過 proxy object 既 getter method 取得。
@ConfigurationPropertiesClass、method用黎讀取某個 prefix 既多個 Spring configuration properties,Spring 會自動 bind 啲 configuration properties 落個 POJO 既所有 fields 度,來源通常係 application[-profile].(yml/properties)、program arguments、environment variables、Spring Cloud Config Server 等等。
@EnableConfigurationPropertiesClass用黎令一個 Spring component 可以 inject 到 @ConfigurationProperties 但係冇 @Component 既 Spring bean dependency,Spring 會自動註冊呢個 dependency 作為 Spring bean。
參考資料:

1.3 Bean injection

Annotation常用目標用途
@AutowiredField用黎 inject Spring bean object 落個 field 度。
@PrimaryClass、method用黎表示呢個 Spring bean 係首選,當有多過一個相同 type 既 Spring beans 存在既時候先至需要用到。
@QualifierField、method parameter用黎指定邊個名既 Spring bean 去用。如果有多過一個相同 type 既 Spring beans 存在,而又唔想用 @Primary 既話就需要用到。
參考資料:

1.4 Bean lifecycle

Annotation常用目標用途
@PostConstructMethod用喺一個 Spring component 裡面,Spring Boot 喺 instantiate 左個 bean object 之後就會 call。
@PreDestroyMethod用喺一個 Spring component 裡面,Spring Boot 喺結束個 bean object 之前就會 call。
參考資料:

1.5 Web

Annotation常用目標用途
@RestControllerClass包含左 @Controller,用黎註冊一個 REST controller bean,裡面會有帶有各種 mapping annotations 既 methods(例如 @GetMapping),可以 return DTO object types 或者 ResponseEntity<T>
@ControllerClass包含左 @Component,係舊式既 controller bean 做法會用既,而家通常都用 @RestController 代替,不過兩者實際上係 interchangeable。
@RequestMappingClass、method通常用喺 class 上面去幫所有 request endpoint paths 加添相同既 path prefix,例如 /v1/orders。如果用喺 method 上面,就係用黎註冊任何 request method 既 HTTP endpoint。
@GetMappingMethod用黎註冊一個 GET request method 既 HTTP endpoint。
@PostMappingMethod用黎註冊一個 POST request method 既 HTTP endpoint。
@PutMappingMethod用黎註冊一個 PUT request method 既 HTTP endpoint。
@PatchMappingMethod用黎註冊一個 PATCH request method 既 HTTP endpoint。
@DeleteMappingMethod用黎註冊一個 DELETE request method 既 HTTP endpoint。
@RequestBodyMethod parameter用黎令 request body deserialize 落個 method parameter 度,通常係自定義既 data types,例如 OrderDto
@RequestParamMethod parameter用黎令某個 request parameter(例如 ?foo=barfoo)deserialize 落個 method parameter 度,通常係簡單既 data types,例如 IntegerStringBoolean
@RequestHeaderMethod parameter用黎令某個 request header(例如 foo: barfoo)deserialize 落個 method parameter 度,通常係簡單既 data types,例如 IntegerStringBoolean
@PathVariableMethod parameter用黎令某個 path variable(例如 /{foo}foo)deserialize 落個 method parameter 度,,通常係簡單既 data types,例如 IntegerString
@ControllerAdviceClass包含左 @Component,用黎註冊一個 class 做一個 Spring bean,通常裡面既 methods 會有 @ExceptionHandler,用黎 handle 自定義 Spring controller request mappings 既 exceptions。
@ExceptionHandlerMethod用黎註冊一個 method 去 handle Spring controller request mappings 既某類 exception,通常會寫喺 @Configuration 既 class 裡面。
參考資料:

2 其他 Spring modules 既 annotations

2.1 JPA/Hibernate

Annotation常用目標用途
@EnableJpaRepositoriesClass通常放喺 main class,可以用 basePackages attribute 黎令 Spring Boot 掃瞄指定既 packages 去搵 JPA repository beans 註冊。
@EntityScanClass通常放喺 main class,可以用 basePackages attribute 黎令 Spring Boot 掃瞄指定既 packages 去搵 JPA entity classes。
@RepositoryClass/interface包含左 @Component,用黎註冊一個 class 或者 interface(extends JpaRepository<T, ID>)做一個 Spring bean。通常會存取數據。對應 domain-driven design 既 repository。
@EntityClass用黎表示呢個 class 係一個 JPA entity。
@TableClass用黎表示呢個 class 係一個 database table。
@ColumnField用黎表示呢個 field 係一個 table column。
@IdField用黎表示呢個 field 係 primary key。
@GeneratedValueField用黎表示呢個 field 用咩方式黎生成 value,通常用喺帶有 @Id 既 fields 上面。
@IndexAnnotation attribute通常用喺 @Tableindexes attribute。用 columnList attribute 黎表示需要落 index 既 columns。可以用 unique attribute 黎表示 unique index。
@UniqueConstraintAnnotation attribute通常用喺 @TableuniqueConstraints attribute。用 columnNames attribute 黎表示需要落 unique constraint 既 columns。
@OneToOneField用黎表示所屬既 class 同呢個 field 係 1-to-1 既關係。
@OneToManyField用黎表示所屬既 class 同呢個 field 係 1-to-M 既關係。個 field 通常係 Collection type。
@ManyToOneField用黎表示所屬既 class 同呢個 field 係 M-to-1 既關係。
@ManyToManyField用黎表示所屬既 class 同呢個 field 係 M-to-M 既關係。
@JoinTableField通常用喺 @ManyToMany 既 field 上面。可以用 joinColumns 黎表示所屬既 class 喺個 join table 對應既 foreign key,亦可以用 inverseJoinColumns 黎表示呢個 field 既 entity class 喺個 join table 對應既 foreign key。
@TransactionalClass、method用黎表示所屬既 class 既所有 methods 或者呢個 method 需要用 database transaction。Spring 版會比 JTA 既 jakarta.transaction.Transactional 多啲 attributes。
@QueryMethod通常用喺一啲需要自定義 JPA query 或者 native SQL 既 JPA repository methods 上面。
@ModifyingMethod用喺帶有 @Query 既 JPA repository method 上面,用黎表示佢會修改數據。
參考資料:

2.2 Caching

Annotation常用目標用途
@EnableCachingClass用黎啟用 Spring 既 cache annotations。
@CacheConfigClass用黎幫個 class 裡面所有 cache annotations 應用一啲通用既配置。
@CacheableMethod用黎指示 Spring 呢個 method 會用 cache-aside pattern 去 cache 個 return value。
@CachePutMethod用黎指示 Spring 呢個 method 去更新 cache。
@CacheEvictMethod用黎指示 Spring 呢個 method 去清除 cache。
@CachingMethod用黎 annotate 多個 cache annotations。
參考資料:

2.3 Bean validation

Annotation常用目標用途
@ValidMethod parameter用黎指示 Spring 去 validate 呢個 method parameter 既 object 裡面帶有 validation annotations 既 fields。
@ValidatedClass用黎指示 Spring 去應用 validation 喺呢個 class 上面。
@ConstraintClass用喺自定義既 validation annotation 上面,佢既 validatedBy attribute 既 class 需要 implements ConstraintValidator
@NotNullField用黎表示呢個 field 唔可以係 null,適用於任何 types。
@NotEmptyField用黎表示呢個 field 唔可以係 null 或者 empty(或者 size 係 0),適用於 StringCollectionMap、array。
@NotBlankField用黎表示呢個 field 唔可以係 null、empty 或者只有 whitespace characters,適用於 String
@MinField用黎表示呢個 field 唔可以少過某個 minimum value。
@MaxField用黎表示呢個 field 唔可以多過某個 maximum value。
@PatternField用黎表示呢個 field 一定要符合某個 regular expression pattern。
參考資料:

2.4 OpenFeign

Annotation常用目標用途
@EnableFeignClientsClass用黎啟用 OpenFeign 既功能。可以用 basePackages attribute 黎令 OpenFeign 掃瞄指定既 packages 去搵 OpenFeign clients 註冊。
@FeignClientInterface用黎註冊個 interface 做 OpenFeign client,裡面會有帶有各種 mapping annotations 既 methods(例如 @GetMapping)。

2.5 Scheduler

Annotation常用目標用途
@EnableSchedulingClass用黎啟用 Spring 既 scheduler 功能。
@ScheduledMethod用黎令一個 method 根據自定義既固定時間段或者 cron expression 而持續咁自動執行。可以用 cron attribute 黎表達類似 Unix cron expression 既 job scheduling 表達式,亦可以用 fixedDelay 或者 fixedRate attributes 黎指定固定時間段(前者會喺執行完成先至開始計時)。

2.6 Async

Annotation常用目標用途
@EnableAsyncClass用黎啟用 Spring async 咁執行 method 既功能。
@AsyncMethod用黎令一個 method 可以 async 咁執行。通常用喺帶有 @Scheduled 既 methods 上面,而我地想用 async(multithreaded)既執行方式黎確保每次既 schedule 都可以準時執行,並唔會因為上次執行時間過長而延遲左後續既 schedules。

2.7 Retry

Annotation常用目標用途
@EnableRetryClass用黎啟用 Spring 既 retry annotations。
@RetryableClass、method用黎令一個 method 可以喺出現 exception 之後重試執行。可以用 maxAttempts attribute 黎表示執行失敗次數上限(包括重試之前既第一次執行),亦可以用 backoff attribute 黎表示下一次重試既延遲 behavior,亦可以用 recover 黎指定 fallback 既 method 名。
@BackoffAnnotation attribute用黎表示下一次重試既延遲 behavior。可以用 delay attribute 黎表示下一次重試既基本延遲,亦可以用 multiplier attribute 黎表示下一次重試既延遲時間倍數(重試得越多,間距越長),亦可以用 maxDelay attribute 黎表示下一次重試既延遲時間上限。
@RecoverMethod用黎表示個 method 會用作可重試既 methods 既 fallback method,當可重試既 methods 達到失敗次數上限,就會返回呢個 method 既 return value 作為 fallback。
參考資料:

2.8 AOP

Annotation常用目標用途
@AspectClass用喺一個 Spring component 上面,用黎令裡面既 AspectJ AOP annotations 生效。
@PointcutMethod用黎表達邊啲 class(s)、method(s)、annotation(s) 等等既 aspects 適用,亦即係一個 aspect selector。都可以直接寫個 aspect selector expression 喺啲 advice annotations 度。用既係 AspectJ pointcut 表達式。
@AroundMethod係其中一個 advice annotation,亦係最勁果個,用黎包住成個 method。佢可以做乜都得,甚至可以唔執行個 intercepted method。
@BeforeMethod係其中一個 advice annotation,會喺執行個 intercepted method 之前執行。
@AfterReturningMethod係其中一個 advice annotation,會喺執行個 intercepted method 之後,喺結果冇 exception 既情況下執行。
@AfterThrowingMethod係其中一個 advice annotation,會喺執行個 intercepted method 之後,喺結果有 exception 既情況下執行。
@AfterMethod係其中一個 advice annotation,會喺執行個 intercepted method 之後,無論有冇 exception 既情況,都會最後執行(類似 try-catch-finally 既 finally block)。
參考資料:

2.9 Spring Cloud Config

Annotation常用目標用途
@EnableConfigServerClass用黎啟用 Spring Cloud Config Server 功能。
@RefreshScopeClass、method用喺 Spring bean 上面,喺 configuration refresh 發生之後,當下一次個 proxy object 有 method call 既時候,Spring 就會重新 instantiate 個 Spring bean object,然後應用最新既配置。
參考資料: