乐途乐途
主页
  • 计算机基础

    • TCP/IP协议
    • Linux命令
    • HTTP协议
  • 数据库

    • SQL
    • MySQL 5.7
  • 编程语言

    • C语言
    • Python2
    • Python3
  • 数据格式

    • JSON
    • XML
  • 认证与安全

    • JWT
  • 工具

    • Markdown
  • Git

    • GitFlow
  • Quartz

    • Quartz
  • Java

    • MyBatis
    • Spring
    • Spring MVC
    • Maven 入门
    • Maven 进阶
    • Java 设计模式
  • 缓存

    • Redis
联系
阿里云
主页
  • 计算机基础

    • TCP/IP协议
    • Linux命令
    • HTTP协议
  • 数据库

    • SQL
    • MySQL 5.7
  • 编程语言

    • C语言
    • Python2
    • Python3
  • 数据格式

    • JSON
    • XML
  • 认证与安全

    • JWT
  • 工具

    • Markdown
  • Git

    • GitFlow
  • Quartz

    • Quartz
  • Java

    • MyBatis
    • Spring
    • Spring MVC
    • Maven 入门
    • Maven 进阶
    • Java 设计模式
  • 缓存

    • Redis
联系
阿里云
  • 学习路径
  • 第1章 Spring概述与IoC容器

    • Spring概述与IoC容器
    • Spring Framework 概述
    • IoC 与 DI 核心概念
    • @Configuration 详解
    • @Component 详解
    • @ComponentScan 详解
    • @Import 详解
    • @Profile 详解
    • @PropertySource 详解
    • @Service 详解
    • @Repository 详解
  • 第2章 Bean的定义与依赖注入

    • Bean的定义与依赖注入
    • @Bean 详解
    • @Autowired 详解
    • @Qualifier 详解
    • @Primary 详解
    • @Resource 详解
    • @Inject 详解
    • @Named 详解
    • @Value 详解
    • @Scope 详解
    • @Lazy 详解
  • 第3章 Bean生命周期与作用域

    • Bean生命周期与作用域
    • Bean生命周期概述
    • @PostConstruct
    • @PreDestroy
    • InitializingBean
    • DisposableBean
    • BeanPostProcessor
    • BeanFactoryPostProcessor
  • 第4章 AOP面向切面编程

    • AOP面向切面编程
    • AOP核心概念
    • @EnableAspectJAutoProxy
    • @Aspect
    • @Pointcut
    • @Before
    • @After
    • @AfterReturning
    • @AfterThrowing
    • @Around
  • 第5章 数据访问与事务管理

    • 数据访问与事务管理
    • 数据访问概述
    • @EnableTransactionManagement
    • @Transactional
    • @Transactional 的传播行为
    • @Transactional 的隔离级别
    • @Transactional 的回滚规则
    • @Transactional 的超时与只读属性
    • @TransactionalEventListener
  • 第6章 Spring Boot自动配置基础

    • Spring Boot自动配置基础
    • @SpringBootApplication 注解
    • @EnableAutoConfiguration 注解
    • @ConfigurationProperties 注解
    • @ConditionalOnClass 注解
    • @ConditionalOnMissingBean 注解
    • @ConditionalOnProperty 注解
  • 第7章 从容器到Web: Spring MVC导引

    • Spring MVC 导引
  • 第8章 扩展阅读

    • 扩展阅读
    • Spring 事件机制 — ApplicationEvent / ApplicationListener
    • @EventListener
    • SpEL — Spring 表达式语言
    • 校验 Validation — JSR-303 / JSR-380 Bean Validation
    • 类型转换与数据绑定 — Converter / DataBinder
  • 附录

    • Spring Framework 专业术语
    • Spring 核心知识点
    • Spring 面试高频考点
    • Spring 核心注解速查表

Spring 核心注解速查表

定位:本表汇总 Spring Framework 核心注解,按功能域分类,标注使用层级和一句话说明。适合开发时快速查阅、面试前系统复习。


一、IoC 容器注解

注解作用层级
@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 / 字段
@ResourceJSR-250 标准,默认按名称装配字段 / Setter
@InjectJSR-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开启定时任务支持类

四、AOP 注解

注解作用层级
@Aspect声明切面类类
@Pointcut定义可复用的切点表达式方法
@Before前置通知方法
@After后置通知(无论是否异常,类似 finally)方法
@AfterReturning方法成功返回后通知方法
@AfterThrowing方法抛出异常后通知方法
@Around环绕通知,包围目标方法执行方法
@EnableAspectJAutoProxy开启 @AspectJ 风格切面的自动代理类
@DeclareParents为现有类引入新接口(Introduction)字段

五、事务与数据访问

注解作用层级
@Transactional声明式事务,支持传播行为和隔离级别类 / 方法
@EnableTransactionManagement开启注解事务管理类
@Rollback测试方法后是否回滚事务(默认 true)方法
@Commit测试方法后强制提交事务方法
@TransactionalEventListener事务阶段绑定的事件监听方法
@Repository数据访问层(含异常转换)类

六、Bean Validation(JSR-380)

注解作用层级
@Valid级联校验(校验嵌套对象/集合元素)参数 / 字段
@ValidatedSpring 的校验分组支持类 / 参数
@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(用于特定场景)字段 / 参数

七、Spring Security 常用注解

注解作用层级
@PreAuthorize方法执行前权限校验(SpEL 表达式)方法 / 类
@PostAuthorize方法执行后权限校验方法
@Secured基于角色的方法安全(JSR-250 风格)方法 / 类
@RolesAllowedJSR-250 标准角色控制方法 / 类
@PermitAll / @DenyAll允许/拒绝所有访问方法 / 类
@AuthenticationPrincipal绑定当前认证用户到方法参数参数
@CurrentSecurityContext绑定 SecurityContext 到方法参数参数
@EnableWebSecurity开启 Web 安全支持类
@EnableGlobalMethodSecurity开启方法级安全注解(prePostEnabled/securedEnabled)类

八、Spring Cache 常用注解

注解作用层级
@Cacheable方法结果可缓存,命中则直接返回方法
@CachePut更新缓存(始终执行方法)方法
@CacheEvict清除缓存方法
@Caching组合多个缓存操作方法
@CacheConfig类级别的缓存配置(cacheNames/keyGenerator 等)类
@EnableCaching开启缓存注解支持类

九、JPA / Spring Data 常用注解

注解作用层级
@EntityJPA 实体类类
@Table指定数据库表名类
@Id主键字段字段
@GeneratedValue主键生成策略字段
@Column字段映射配置字段
@Transient字段不持久化字段
@Temporal日期类型映射字段
@Enumerated枚举映射策略字段
@OneToOne / @OneToMany / @ManyToOne / @ManyToMany关联映射字段
@JoinColumn / @JoinTable关联外键/中间表配置字段
@QuerySpring 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 原生注解替代。

上一页
Spring 面试高频考点