文档站点
Skip to content

常用注解速查表 ​

集中整理 Spring Boot 高频注解,按用途分类。每个注解都带「类比 Laravel」,帮助记忆。

组件与依赖注入 ​

注解作用类比 Laravel
@SpringBootApplication入口 = 配置 + 扫描 + 自动配置public/index.php 启动引导
@Component注册为容器 Bean被容器管理的类
@Service业务层组件(语义版 @Component)app/Services 下的类
@Repository数据访问层组件app/Models / Repository
@Controller控制器(返回视图)控制器
@RestController控制器(返回值即 JSON)控制器 + return $json
@Configuration配置类config/ 文件 / ServiceProvider
@Bean在配置类里手动装配一个对象ServiceProvider 里 bind()
@Autowired自动注入依赖app(SomeClass::class)
@Qualifier("name")指定注入哪个实现容器里绑定具体实现
@Value("${key}")读取配置项config('xxx') / env('xxx')
@ConfigurationProperties一组配置绑定到类类型安全的 config

请求映射(路由) ​

注解作用类比 Laravel
@RequestMapping(path)类/方法级路由 + 方法约束Route::match
@GetMappingGET 路由Route::get
@PostMappingPOST 路由Route::post
@PutMappingPUT 路由Route::put
@DeleteMappingDELETE 路由Route::delete
@PatchMappingPATCH 路由Route::patch

参数绑定(请求) ​

注解作用类比 Laravel
@PathVariable路径参数 {id}$request->route('id')
@RequestParam查询参数$request->query('page')
@RequestBodyJSON 请求体 → 对象$request->all() + 反序列化
@RequestHeader请求头$request->header()
@CookieValueCookie$request->cookie()
@RequestPart上传文件$request->file()
@SessionAttribute读 Sessionsession('key')
@RequestAttribute读 request 属性(拦截器写入)中间件塞进 request 的数据

响应 ​

注解作用类比 Laravel
@ResponseStatus(code)固定状态码response()->status()
@ResponseBody返回值序列化为 JSONresponse()->json()
@JsonIgnore序列化时忽略字段$hidden
@JsonProperty("name")输出字段改名Resource 字段重命名

校验(Validation) ​

注解作用类比 Laravel 规则
@Valid / @Validated触发校验$request->validate()
@NotBlank非空字符串required
@NotNull非 nullrequired
@Size(min,max)长度min:|max:
@Min / @Max数值范围min / max
@Email邮箱email
@Pattern(regexp)正则regex:
@Positive正整数integer|min:1
@Past / @Future时间先后before: / after:

事务 / 缓存 / 异步 / 调度 ​

注解作用类比 Laravel
@Transactional方法级事务DB::transaction()
@Cacheable读缓存,未命中执行并缓存Cache::remember
@CachePut总是执行并更新缓存Cache::put
@CacheEvict删除缓存Cache::forget
@EnableCaching开启缓存config/cache.php
@EnableAsync开启异步队列 worker
@Async异步执行dispatch() / Queue::push
@EnableScheduling开启定时任务Kernel schedule
@Scheduled(cron)定时任务(六段式 cron)->dailyAt()
@PostConstruct初始化后执行一次ServiceProvider boot()
@PreDestroy销毁前执行—

事件(Events) ​

注解作用类比 Laravel
@EventListener监听事件Event 的 Listener
@TransactionalEventListener(AFTER_COMMIT)事务提交后监听afterCommit 事件

安全(Spring Security) ​

注解作用类比 Laravel
@EnableWebSecurity开启 Security 配置中间件配置
@EnableMethodSecurity开启方法级授权Gate / Policy
@PreAuthorize("hasRole('ADMIN')")方法前鉴权Gate::authorize
@Secured老式角色鉴权middleware('role')

数据访问(JPA) ​

注解作用类比 Eloquent
@Entity声明实体继承 Model
@Table(name)映射表$table
@Id主键—
@GeneratedValue(IDENTITY)自增主键$incrementing
@Column字段映射/约束migration 列定义
@OneToMany(mappedBy)一对多hasMany
@ManyToOne多对一belongsTo
@ManyToMany多对多belongsToMany
@JoinColumn / @JoinTable外键 / 中间表migration 外键 / 中间表
@Query自定义 JPQL/原生 SQLDB::select
@Modifying修改类 @QueryDB::update
@EntityGraph预加载防 N+1with()

配置类里常用的条件注解 ​

注解作用
@ConditionalOnClassclasspath 有某类才生效
@ConditionalOnMissingBean没有某 Bean 才生效
@ConditionalOnProperty配置项满足才生效
@ConditionalOnWebApplication是 Web 应用才生效

记忆技巧

  • @XxxMapping = 路由注解,@RequestXxx = 参数绑定,@ResponseStatus = 响应控制
  • @EnableXxx = 开一个功能的总开关
  • 带 @ConfigurationProperties 前缀的类 = 「类型安全的配置中心」
  • 大部分注解背后都是 AOP 代理 或反射,理解「谁在读注解」比背注解重要

面向 PHP / Laravel 开发者的 Spring Boot 中文文档