定位:本表汇总 Spring Framework 核心注解,按功能域分类,标注使用层级和一句话说明。适合开发时快速查阅、面试前系统复习。
| 注解 | 作用 | 层级 |
|---|
@Component | 通用组件,标记为 Spring 管理的 Bean | 类 |
@Service | 业务逻辑层,语义化 @Component | 类 |
@Repository | 数据访问层,自动转换持久层异常 | 类 |
@Configuration | 声明配置类,内部 @Bean 方法定义 Bean | 类 |
@Bean | 在 @Configuration 类中显式定义 Bean | 方法 |
@ComponentScan | 开启组件扫描,指定扫描包路径 | 类 |
@Import | 导入其他配置类或 ImportSelector | 类 |
@Profile | 指定 Bean 生效的环境 profile | 类 / 方法 |
@Scope | 指定 Bean 作用域(singleton/prototype/request/session/application/websocket) | 类 / 方法 |
@Lazy | 延迟初始化 | 类 / 方法 / 参数 |
@Primary | 同类型 Bean 冲突时优先注入 | 类 / 方法 |
@Qualifier | 按名称精确指定注入的 Bean | 参数 / 字段 / 方法 |
@Autowired | 按类型自动装配(Spring 专有) | 构造器 / Setter / 字段 |
@Resource | JSR-250 标准,默认按名称装配 | 字段 / Setter |
@Inject | JSR-330 标准,等价于 @Autowired | 构造器 / Setter / 字段 |
@Value | 注入配置属性(${...})或 SpEL 表达式(#{...}) | 字段 / 参数 |
@PropertySource | 指定额外的属性文件路径 | 类 |
@ConfigurationProperties | 批量绑定结构化外部配置到 Java 对象 | 类 |
@ConstructorBinding | 通过构造器绑定配置属性(不可变对象) | 类 |
@EnableConfigurationProperties | 启用 @ConfigurationProperties 配置类 | 类 |
| 注解 | 作用 | 层级 |
|---|
@Lookup | 抽象方法上,让 Spring 实现方法注入 prototype Bean | 方法 |
@DependsOn | 指定 Bean 初始化前的依赖顺序 | 类 / 方法 |
@Order | 指定 Bean 或 AOP Advice 的执行顺序(数字越小越先) | 类 / 方法 |
@Conditional | 条件化注册 Bean(Spring Boot 扩展了大量子注解) | 类 / 方法 |
@Description | 为 Bean 添加描述信息 | 类 / 方法 / 字段 |
| 注解 | 作用 | 层级 |
|---|
@PostConstruct | 依赖注入完成后执行初始化(JSR-250) | 方法 |
@PreDestroy | 容器销毁 Bean 前执行清理(JSR-250) | 方法 |
@EventListener | 将方法注册为事件监听器 | 方法 |
@TransactionalEventListener | 事务绑定的事件监听(AFTER_COMMIT 等) | 方法 |
@Async | 异步执行方法 | 方法 / 类 |
@Scheduled | 定时任务(cron/fixedRate/fixedDelay) | 方法 |
@EnableAsync | 开启异步方法支持 | 类 |
@EnableScheduling | 开启定时任务支持 | 类 |
| 注解 | 作用 | 层级 |
|---|
@Aspect | 声明切面类 | 类 |
@Pointcut | 定义可复用的切点表达式 | 方法 |
@Before | 前置通知 | 方法 |
@After | 后置通知(无论是否异常,类似 finally) | 方法 |
@AfterReturning | 方法成功返回后通知 | 方法 |
@AfterThrowing | 方法抛出异常后通知 | 方法 |
@Around | 环绕通知,包围目标方法执行 | 方法 |
@EnableAspectJAutoProxy | 开启 @AspectJ 风格切面的自动代理 | 类 |
@DeclareParents | 为现有类引入新接口(Introduction) | 字段 |
| 注解 | 作用 | 层级 |
|---|
@Transactional | 声明式事务,支持传播行为和隔离级别 | 类 / 方法 |
@EnableTransactionManagement | 开启注解事务管理 | 类 |
@Rollback | 测试方法后是否回滚事务(默认 true) | 方法 |
@Commit | 测试方法后强制提交事务 | 方法 |
@TransactionalEventListener | 事务阶段绑定的事件监听 | 方法 |
@Repository | 数据访问层(含异常转换) | 类 |
| 注解 | 作用 | 层级 |
|---|
@Valid | 级联校验(校验嵌套对象/集合元素) | 参数 / 字段 |
@Validated | Spring 的校验分组支持 | 类 / 参数 |
@NotNull | 值不能为 null | 字段 / 参数 |
@NotEmpty | 不能为 null 且长度/大小 > 0 | 字段 / 参数 |
@NotBlank | 不能为 null 且 trim 后长度 > 0(仅 String) | 字段 / 参数 |
@Size(min, max) | 长度/大小范围 | 字段 / 参数 |
@Min / @Max | 数值最小/最大值 | 字段 / 参数 |
@Positive / @PositiveOrZero | 正数 / 非负数 | 字段 / 参数 |
@Negative / @NegativeOrZero | 负数 / 非正数 | 字段 / 参数 |
@DecimalMin / @DecimalMax | 小数范围 | 字段 / 参数 |
@Digits | 整数位和小数位限制 | 字段 / 参数 |
@Email | 邮箱格式校验 | 字段 / 参数 |
@Pattern(regexp) | 正则匹配 | 字段 / 参数 |
@Past / @PastOrPresent | 过去的时间 | 字段 / 参数 |
@Future / @FutureOrPresent | 将来的时间 | 字段 / 参数 |
@AssertTrue / @AssertFalse | 必须为 true / false | 字段 / 参数 |
@Null | 值必须为 null(用于特定场景) | 字段 / 参数 |
| 注解 | 作用 | 层级 |
|---|
@PreAuthorize | 方法执行前权限校验(SpEL 表达式) | 方法 / 类 |
@PostAuthorize | 方法执行后权限校验 | 方法 |
@Secured | 基于角色的方法安全(JSR-250 风格) | 方法 / 类 |
@RolesAllowed | JSR-250 标准角色控制 | 方法 / 类 |
@PermitAll / @DenyAll | 允许/拒绝所有访问 | 方法 / 类 |
@AuthenticationPrincipal | 绑定当前认证用户到方法参数 | 参数 |
@CurrentSecurityContext | 绑定 SecurityContext 到方法参数 | 参数 |
@EnableWebSecurity | 开启 Web 安全支持 | 类 |
@EnableGlobalMethodSecurity | 开启方法级安全注解(prePostEnabled/securedEnabled) | 类 |
| 注解 | 作用 | 层级 |
|---|
@Cacheable | 方法结果可缓存,命中则直接返回 | 方法 |
@CachePut | 更新缓存(始终执行方法) | 方法 |
@CacheEvict | 清除缓存 | 方法 |
@Caching | 组合多个缓存操作 | 方法 |
@CacheConfig | 类级别的缓存配置(cacheNames/keyGenerator 等) | 类 |
@EnableCaching | 开启缓存注解支持 | 类 |
| 注解 | 作用 | 层级 |
|---|
@Entity | JPA 实体类 | 类 |
@Table | 指定数据库表名 | 类 |
@Id | 主键字段 | 字段 |
@GeneratedValue | 主键生成策略 | 字段 |
@Column | 字段映射配置 | 字段 |
@Transient | 字段不持久化 | 字段 |
@Temporal | 日期类型映射 | 字段 |
@Enumerated | 枚举映射策略 | 字段 |
@OneToOne / @OneToMany / @ManyToOne / @ManyToMany | 关联映射 | 字段 |
@JoinColumn / @JoinTable | 关联外键/中间表配置 | 字段 |
@Query | Spring Data 自定义查询 | 方法 |
@Param | 为 JPQL/原生 SQL 参数命名 | 参数 |
@Modifying | 标识修改操作(UPDATE/DELETE) | 方法 |
@Transactional | 事务控制(Spring Data 查询默认只读) | 方法 / 类 |
@EnableJpaRepositories | 开启 JPA Repository 扫描 | 类 |
@EnableTransactionManagement | 开启声明式事务 | 类 |
| 场景 | 关键注解 |
|---|
| 定义 Bean | @Component, @Service, @Repository, @Configuration, @Bean |
| 注入依赖 | @Autowired, @Resource, @Inject, @Value, @Qualifier, @Primary |
| 配置属性 | @Value, @ConfigurationProperties, @PropertySource, @ConstructorBinding |
| 生命周期 | @PostConstruct, @PreDestroy, @EventListener, @TransactionalEventListener |
| AOP 切面 | @Aspect, @Pointcut, @Before, @After, @AfterReturning, @AfterThrowing, @Around |
| 事务控制 | @Transactional, @EnableTransactionManagement |
| 数据校验 | @Valid, @Validated, @NotNull, @NotBlank, @Size, @Email, @Pattern |
| 安全 | @PreAuthorize, @Secured, @RolesAllowed, @AuthenticationPrincipal |
| 缓存 | @Cacheable, @CachePut, @CacheEvict, @EnableCaching |
| 异步/定时 | @Async, @Scheduled, @EnableAsync, @EnableScheduling |
版本说明:本速查表基于 Spring Framework 6.x / Spring Boot 3.x 编写,使用 jakarta 命名空间(Jakarta EE 9+)。部分注解(如 @MockBean)在 Spring 6.2+ 已被 Framework 原生注解替代。