常用注解速查
Spring Boot 核心
| 注解 | 作用 | 说明 |
|---|---|---|
@SpringBootApplication | 入口注解,组合了 @Configuration + @EnableAutoConfiguration + @ComponentScan | 放在 main() 方法所在类上 |
@Configuration | 标记一个类为配置类(类比 XML 配置) | 用在定义 @Bean 的类上 |
@EnableAutoConfiguration | 自动配置 | Spring Boot 的核心,自动推断你需要什么配置 |
@ComponentScan | 扫描指定包下的组件 | 默认扫描当前包及其子包 |
组件注册
| 注解 | 作用 | 类比 |
|---|---|---|
@Component | 通用组件 | 任意类注册为 Bean |
@Service | 业务逻辑层 | 类似 @Component,语义明确 |
@Repository | 数据访问层 | 类似 @Component,Spring 会转换数据访问异常 |
@RestController | 控制器 + 响应体 | @Controller + @ResponseBody |
@Controller | 控制器(返回视图) | 对应 MVC 的 Controller |
@Bean | 方法级别,声明返回值为一个 Bean | 用在 @Configuration 类中 |
依赖注入
| 注解 | 作用 | 类比 |
|---|---|---|
@Autowired | 按类型注入 | Laravel 的构造器自动解析 |
@Qualifier | 指定 Bean 名称注入 | 当有多个同类型 Bean 时使用 |
@Value | 注入配置值 | config('app.name') |
@Scope | 指定作用域 | singleton(默认)/ prototype / request |
@Lazy | 延迟初始化 | 第一次使用时才创建 |
@Primary | 多个同类型 Bean 时优先 | 自动装配的首选 |
HTTP 路由
| 注解 | 作用 | 类比 |
|---|---|---|
@RequestMapping | 通用请求映射 | Route::match(['GET', 'POST'], '/uri') |
@GetMapping | GET 请求 | Route::get('/uri') |
@PostMapping | POST 请求 | Route::post('/uri') |
@PutMapping | PUT 请求 | Route::put('/uri') |
@DeleteMapping | DELETE 请求 | Route::delete('/uri') |
@PatchMapping | PATCH 请求 | Route::patch('/uri') |
@PathVariable | 路径参数 | Route::get('/users/{id}') |
@RequestParam | 查询参数 | $request->input('name') |
@RequestBody | 请求体 JSON | $request->all() |
@RequestHeader | 请求头 | $request->header('User-Agent') |
@ResponseBody | 返回值序列化 | response()->json() |
@ResponseStatus | 指定响应状态码 | response()->json([], 201) |
数据校验
| 注解 | 作用 |
|---|---|
@Valid | 触发 JSR 校验 |
@Validated | Spring 的校验(支持分组) |
@NotBlank | 字符串不为 null 且去掉空格后不为空 |
@NotEmpty | 集合/字符串不为空 |
@NotNull | 不为 null |
@Size(min, max) | 字符串长度/集合大小范围 |
@Min / @Max | 数字范围 |
@Email | 邮箱格式 |
@Pattern | 正则匹配 |
@Positive / @PositiveOrZero | 正数 / 非负数 |
数据访问
| 注解 | 作用 | 类比 |
|---|---|---|
@Mapper | 标记 MyBatis Mapper | @Repository 的特化 |
@TableName | 指定表名 | protected $table = 'users' |
@TableId | 指定主键 | $primaryKey = 'id' |
@TableField | 指定字段名 | protected $fillable |
@Transactional | 声明式事务 | DB::transaction() |
@Select | 直接写 SQL 查询(MyBatis) | DB::select('...') |
@Insert | 直接写 SQL 插入(MyBatis) | - |
声明式功能
| 注解 | 作用 | 类比/说明 |
|---|---|---|
@Transactional | 事务管理 | 方法执行前后自动开启/提交/回滚事务 |
@Cacheable | 缓存结果 | Cache::remember() |
@CacheEvict | 清除缓存 | Cache::forget() |
@CachePut | 更新缓存 | Cache::put() |
@Scheduled | 定时任务 | php artisan schedule |
@Async | 异步执行 | 另起线程执行 |
@EventListener | 事件监听 | Event::listen() |
@TransactionalEventListener | 事务事件监听 | 事务提交后触发 |
AOP
| 注解 | 作用 | 说明 |
|---|---|---|
@Aspect | 声明切面类 | 需要 @Component 配合 |
@Before | 方法执行前 | 前置通知 |
@After | 方法执行后 | 最终通知 |
@AfterReturning | 方法成功返回后 | 后置通知 |
@AfterThrowing | 方法抛出异常后 | 异常通知 |
@Around | 包裹整个方法 | 最强大的通知,可控制方法执行 |
Spring Security
| 注解 | 作用 | 类比 |
|---|---|---|
@EnableWebSecurity | 启用 Security 配置 | - |
@EnableMethodSecurity | 启用方法安全 | - |
@PreAuthorize | 方法执行前权限检查 | $this->authorize() |
@PostAuthorize | 方法执行后权限检查 | - |
@Secured | 角色检查(旧版) | @PreAuthorize 的简化版 |
@AuthenticationPrincipal | 注入当前用户 | auth()->user() |
测试
| 注解 | 作用 | 说明 |
|---|---|---|
@SpringBootTest | 启动完整测试上下文 | 类比 RefreshDatabase + 完整应用 |
@WebMvcTest | 只测试 Web 层 | 只启动 Controller 相关组件 |
@DataJpaTest | 只测试 JPA/MyBatis | 只启动数据访问组件 |
@MockBean | 替换为 Mock | 在测试中模拟某个 Bean |
@Test | 标记测试方法 | JUnit 5 |
@BeforeEach | 每个测试前执行 | setUp() |
@AfterEach | 每个测试后执行 | tearDown() |
Lombok(省代码)
| 注解 | 作用 |
|---|---|
@Data | @Getter + @Setter + @ToString + @EqualsAndHashCode + @RequiredArgsConstructor |
@Getter / @Setter | 自动生成 getter/setter |
@ToString | 自动生成 toString() |
@EqualsAndHashCode | 自动生成 equals() 和 hashCode() |
@NoArgsConstructor | 生成无参构造器 |
@AllArgsConstructor | 生成全参构造器 |
@Builder | 生成 Builder 模式 |
@Slf4j | 生成 log 字段 |
@RequiredArgsConstructor | 生成 final 字段的构造器 |