Annotation | 常用目標 | 用途 |
---|---|---|
@SpringBootApplication | Class | 放置喺 main class,用黎啟動 Spring Boot application,裡面包含 @EnableAutoConfiguration 、@ComponentScan 。 |
@EnableAutoConfiguration | Class | 通常放喺 main class,用黎啟用 Spring 既 auto-configuration 功能。 |
@ComponentScan | Class | 通常放喺 main class,可以用 basePackages attribute 黎令 Spring Boot 掃瞄指定既 packages 去搵 beans 註冊。 |
@Component | Class | 用黎註冊一個 class 做一個 Spring bean。Spring Boot 啟動既時候會自動 instantiate bean object。 |
@Service | Class | 包含左 @Component ,用黎註冊一個 class 做一個 Spring bean,通常會帶有 business logic。對應 domain-driven design 既 service。 |
@Configuration | Class | 包含左 @Component ,用黎註冊一個 class 做一個 Spring bean,通常裡面既 methods 會有 @Bean 。 |
@Bean | Method | 用黎註冊一個 method 既 return value 做一個 Spring bean,通常會寫喺 @Configuration 既 class 裡面。 |
@Profile | Class、method | 用黎表示呢個 Spring bean 只會喺某啲 profiles 生效既情況下被註冊。 |
@ConditionalOnProperty | Class、method | 用黎表示呢個 Spring bean 只會喺某啲 configuration properties match 到特定 value 既情況下被註冊。 |
@ConditionalOnExpression | Class、method | 用黎表示呢個 Spring bean 只會喺個 SpEL expression 係 true 既情況下被註冊。 |
@ConditionalOnBean | Class、method | 用黎表示呢個 Spring bean 只會喺某啲 Spring beans 存在既情況下被註冊。 |
@ConditionalOnMissingBean | Class、method | 用黎表示呢個 Spring bean 只會喺某啲 Spring beans 唔存在既情況下被註冊。 |
Annotation | 常用目標 | 用途 |
---|---|---|
@Value | Field | 用黎讀取一個 Spring configuration property,來源通常係 application[-profile].(yml/properties) 、program arguments、environment variables、Spring Cloud Config Server 等等。其他 Spring beans 必須經過 proxy object 既 getter method 取得。 |
@ConfigurationProperties | Class、method | 用黎讀取某個 prefix 既多個 Spring configuration properties,Spring 會自動 bind 啲 configuration properties 落個 POJO 既所有 fields 度,來源通常係 application[-profile].(yml/properties) 、program arguments、environment variables、Spring Cloud Config Server 等等。 |
@EnableConfigurationProperties | Class | 用黎令一個 Spring component 可以 inject 到 @ConfigurationProperties 但係冇 @Component 既 Spring bean dependency,Spring 會自動註冊呢個 dependency 作為 Spring bean。 |
Annotation | 常用目標 | 用途 |
---|---|---|
@Autowired | Field | 用黎 inject Spring bean object 落個 field 度。 |
@Primary | Class、method | 用黎表示呢個 Spring bean 係首選,當有多過一個相同 type 既 Spring beans 存在既時候先至需要用到。 |
@Qualifier | Field、method parameter | 用黎指定邊個名既 Spring bean 去用。如果有多過一個相同 type 既 Spring beans 存在,而又唔想用 @Primary 既話就需要用到。 |
@Autowired
@Primary
Annotation | 常用目標 | 用途 |
---|---|---|
@PostConstruct | Method | 用喺一個 Spring component 裡面,Spring Boot 喺 instantiate 左個 bean object 之後就會 call。 |
@PreDestroy | Method | 用喺一個 Spring component 裡面,Spring Boot 喺結束個 bean object 之前就會 call。 |
Annotation | 常用目標 | 用途 |
---|---|---|
@RestController | Class | 包含左 @Controller ,用黎註冊一個 REST controller bean,裡面會有帶有各種 mapping annotations 既 methods(例如 @GetMapping ),可以 return DTO object types 或者 ResponseEntity<T> 。 |
@Controller | Class | 包含左 @Component ,係舊式既 controller bean 做法會用既,而家通常都用 @RestController 代替,不過兩者實際上係 interchangeable。 |
@RequestMapping | Class、method | 通常用喺 class 上面去幫所有 request endpoint paths 加添相同既 path prefix,例如 /v1/orders 。如果用喺 method 上面,就係用黎註冊任何 request method 既 HTTP endpoint。 |
@GetMapping | Method | 用黎註冊一個 GET request method 既 HTTP endpoint。 |
@PostMapping | Method | 用黎註冊一個 POST request method 既 HTTP endpoint。 |
@PutMapping | Method | 用黎註冊一個 PUT request method 既 HTTP endpoint。 |
@PatchMapping | Method | 用黎註冊一個 PATCH request method 既 HTTP endpoint。 |
@DeleteMapping | Method | 用黎註冊一個 DELETE request method 既 HTTP endpoint。 |
@RequestBody | Method parameter | 用黎令 request body deserialize 落個 method parameter 度,通常係自定義既 data types,例如 OrderDto 。 |
@RequestParam | Method parameter | 用黎令某個 request parameter(例如 ?foo=bar 既 foo )deserialize 落個 method parameter 度,通常係簡單既 data types,例如 Integer 、String 、Boolean 。 |
@RequestHeader | Method parameter | 用黎令某個 request header(例如 foo: bar 既 foo )deserialize 落個 method parameter 度,通常係簡單既 data types,例如 Integer 、String 、Boolean 。 |
@PathVariable | Method parameter | 用黎令某個 path variable(例如 /{foo} 既 foo )deserialize 落個 method parameter 度,,通常係簡單既 data types,例如 Integer 、String 。 |
@ControllerAdvice | Class | 包含左 @Component ,用黎註冊一個 class 做一個 Spring bean,通常裡面既 methods 會有 @ExceptionHandler ,用黎 handle 自定義 Spring controller request mappings 既 exceptions。 |
@ExceptionHandler | Method | 用黎註冊一個 method 去 handle Spring controller request mappings 既某類 exception,通常會寫喺 @Configuration 既 class 裡面。 |
Annotation | 常用目標 | 用途 |
---|---|---|
@EnableJpaRepositories | Class | 通常放喺 main class,可以用 basePackages attribute 黎令 Spring Boot 掃瞄指定既 packages 去搵 JPA repository beans 註冊。 |
@EntityScan | Class | 通常放喺 main class,可以用 basePackages attribute 黎令 Spring Boot 掃瞄指定既 packages 去搵 JPA entity classes。 |
@Repository | Class/interface | 包含左 @Component ,用黎註冊一個 class 或者 interface(extends JpaRepository<T, ID> )做一個 Spring bean。通常會存取數據。對應 domain-driven design 既 repository。 |
@Entity | Class | 用黎表示呢個 class 係一個 JPA entity。 |
@Table | Class | 用黎表示呢個 class 係一個 database table。 |
@Column | Field | 用黎表示呢個 field 係一個 table column。 |
@Id | Field | 用黎表示呢個 field 係 primary key。 |
@GeneratedValue | Field | 用黎表示呢個 field 用咩方式黎生成 value,通常用喺帶有 @Id 既 fields 上面。 |
@Index | Annotation attribute | 通常用喺 @Table 既 indexes attribute。用 columnList attribute 黎表示需要落 index 既 columns。可以用 unique attribute 黎表示 unique index。 |
@UniqueConstraint | Annotation attribute | 通常用喺 @Table 既 uniqueConstraints attribute。用 columnNames attribute 黎表示需要落 unique constraint 既 columns。 |
@OneToOne | Field | 用黎表示所屬既 class 同呢個 field 係 1-to-1 既關係。 |
@OneToMany | Field | 用黎表示所屬既 class 同呢個 field 係 1-to-M 既關係。個 field 通常係 Collection type。 |
@ManyToOne | Field | 用黎表示所屬既 class 同呢個 field 係 M-to-1 既關係。 |
@ManyToMany | Field | 用黎表示所屬既 class 同呢個 field 係 M-to-M 既關係。 |
@JoinTable | Field | 通常用喺 @ManyToMany 既 field 上面。可以用 joinColumns 黎表示所屬既 class 喺個 join table 對應既 foreign key,亦可以用 inverseJoinColumns 黎表示呢個 field 既 entity class 喺個 join table 對應既 foreign key。 |
@Transactional | Class、method | 用黎表示所屬既 class 既所有 methods 或者呢個 method 需要用 database transaction。Spring 版會比 JTA 既 jakarta.transaction.Transactional 多啲 attributes。 |
@Query | Method | 通常用喺一啲需要自定義 JPA query 或者 native SQL 既 JPA repository methods 上面。 |
@Modifying | Method | 用喺帶有 @Query 既 JPA repository method 上面,用黎表示佢會修改數據。 |
@Transactional
Annotation | 常用目標 | 用途 |
---|---|---|
@EnableCaching | Class | 用黎啟用 Spring 既 cache annotations。 |
@CacheConfig | Class | 用黎幫個 class 裡面所有 cache annotations 應用一啲通用既配置。 |
@Cacheable | Method | 用黎指示 Spring 呢個 method 會用 cache-aside pattern 去 cache 個 return value。 |
@CachePut | Method | 用黎指示 Spring 呢個 method 去更新 cache。 |
@CacheEvict | Method | 用黎指示 Spring 呢個 method 去清除 cache。 |
@Caching | Method | 用黎 annotate 多個 cache annotations。 |
Annotation | 常用目標 | 用途 |
---|---|---|
@Valid | Method parameter | 用黎指示 Spring 去 validate 呢個 method parameter 既 object 裡面帶有 validation annotations 既 fields。 |
@Validated | Class | 用黎指示 Spring 去應用 validation 喺呢個 class 上面。 |
@Constraint | Class | 用喺自定義既 validation annotation 上面,佢既 validatedBy attribute 既 class 需要 implements ConstraintValidator 。 |
@NotNull | Field | 用黎表示呢個 field 唔可以係 null ,適用於任何 types。 |
@NotEmpty | Field | 用黎表示呢個 field 唔可以係 null 或者 empty(或者 size 係 0 ),適用於 String 、Collection 、Map 、array。 |
@NotBlank | Field | 用黎表示呢個 field 唔可以係 null 、empty 或者只有 whitespace characters,適用於 String 。 |
@Min | Field | 用黎表示呢個 field 唔可以少過某個 minimum value。 |
@Max | Field | 用黎表示呢個 field 唔可以多過某個 maximum value。 |
@Pattern | Field | 用黎表示呢個 field 一定要符合某個 regular expression pattern。 |
Annotation | 常用目標 | 用途 |
---|---|---|
@EnableFeignClients | Class | 用黎啟用 OpenFeign 既功能。可以用 basePackages attribute 黎令 OpenFeign 掃瞄指定既 packages 去搵 OpenFeign clients 註冊。 |
@FeignClient | Interface | 用黎註冊個 interface 做 OpenFeign client,裡面會有帶有各種 mapping annotations 既 methods(例如 @GetMapping )。 |
Annotation | 常用目標 | 用途 |
---|---|---|
@EnableScheduling | Class | 用黎啟用 Spring 既 scheduler 功能。 |
@Scheduled | Method | 用黎令一個 method 根據自定義既固定時間段或者 cron expression 而持續咁自動執行。可以用 cron attribute 黎表達類似 Unix cron expression 既 job scheduling 表達式,亦可以用 fixedDelay 或者 fixedRate attributes 黎指定固定時間段(前者會喺執行完成先至開始計時)。 |
Annotation | 常用目標 | 用途 |
---|---|---|
@EnableAsync | Class | 用黎啟用 Spring async 咁執行 method 既功能。 |
@Async | Method | 用黎令一個 method 可以 async 咁執行。通常用喺帶有 @Scheduled 既 methods 上面,而我地想用 async(multithreaded)既執行方式黎確保每次既 schedule 都可以準時執行,並唔會因為上次執行時間過長而延遲左後續既 schedules。 |
Annotation | 常用目標 | 用途 |
---|---|---|
@EnableRetry | Class | 用黎啟用 Spring 既 retry annotations。 |
@Retryable | Class、method | 用黎令一個 method 可以喺出現 exception 之後重試執行。可以用 maxAttempts attribute 黎表示執行失敗次數上限(包括重試之前既第一次執行),亦可以用 backoff attribute 黎表示下一次重試既延遲 behavior,亦可以用 recover 黎指定 fallback 既 method 名。 |
@Backoff | Annotation attribute | 用黎表示下一次重試既延遲 behavior。可以用 delay attribute 黎表示下一次重試既基本延遲,亦可以用 multiplier attribute 黎表示下一次重試既延遲時間倍數(重試得越多,間距越長),亦可以用 maxDelay attribute 黎表示下一次重試既延遲時間上限。 |
@Recover | Method | 用黎表示個 method 會用作可重試既 methods 既 fallback method,當可重試既 methods 達到失敗次數上限,就會返回呢個 method 既 return value 作為 fallback。 |
Annotation | 常用目標 | 用途 |
---|---|---|
@Aspect | Class | 用喺一個 Spring component 上面,用黎令裡面既 AspectJ AOP annotations 生效。 |
@Pointcut | Method | 用黎表達邊啲 class(s)、method(s)、annotation(s) 等等既 aspects 適用,亦即係一個 aspect selector。都可以直接寫個 aspect selector expression 喺啲 advice annotations 度。用既係 AspectJ pointcut 表達式。 |
@Around | Method | 係其中一個 advice annotation,亦係最勁果個,用黎包住成個 method。佢可以做乜都得,甚至可以唔執行個 intercepted method。 |
@Before | Method | 係其中一個 advice annotation,會喺執行個 intercepted method 之前執行。 |
@AfterReturning | Method | 係其中一個 advice annotation,會喺執行個 intercepted method 之後,喺結果冇 exception 既情況下執行。 |
@AfterThrowing | Method | 係其中一個 advice annotation,會喺執行個 intercepted method 之後,喺結果有 exception 既情況下執行。 |
@After | Method | 係其中一個 advice annotation,會喺執行個 intercepted method 之後,無論有冇 exception 既情況,都會最後執行(類似 try-catch-finally 既 finally block)。 |
Annotation | 常用目標 | 用途 |
---|---|---|
@EnableConfigServer | Class | 用黎啟用 Spring Cloud Config Server 功能。 |
@RefreshScope | Class、method | 用喺 Spring bean 上面,喺 configuration refresh 發生之後,當下一次個 proxy object 有 method call 既時候,Spring 就會重新 instantiate 個 Spring bean object,然後應用最新既配置。 |