资讯动态

Spring 框架常用注解大全

发布时间:2026/8/18 1:19:30 来源:尧图企业网站定制
摘要本文系统整理了 Spring 框架及其相关生态Spring Boot、Spring MVC、JPA 等中的常用注解按照功能分类进行详细说明帮助开发者快速查阅和理解各个注解的作用和用法。一、Bean 声明与注入1.1 声明 Bean 的注解Component通用组件注解没有明确的角色Service在业务逻辑层Service 层使用Repository在数据访问层DAO 层使用Controller在展现层Spring MVC使用1.2 注入 Bean 的注解AutowiredSpring 提供的自动装配注解InjectJSR-330 提供的依赖注入注解ResourceJSR-250 提供的资源注入注解1.3 Bean 生命周期与作用域Bean注解在方法上声明当前方法的返回值为一个 BeanBean(initMethodinit, destroyMethodcleanup)指定初始化和销毁方法Scope定义 Bean 的作用域如 Scope(prototype) 声明为原型模式PostConstruct在构造函数执行完之后执行PreDestroy在 Bean 销毁之前执行二、配置与属性2.1 配置类注解Configuration声明当前类为配置类相当于 Spring 的 XML 配置文件ComponentScan自动扫描指定包下的 Component、Service、Repository、Controller 类并注册为 BeanWiselyConfiguration组合注解可替代 Configuration 和 ComponentScanImport导入其他配置类ImportResource加载 XML 配置文件ContextConfiguration加载配置文件如 ContextConfiguration(classes {TestConfig.class})WebAppConfiguration指定加载的 ApplicationContext 是 WebApplicationContext2.2 属性注入与配置Value属性注入Value(普通字符串)普通字符串注入Value(#{systemProperties[os.name]})注入操作系统属性Value(#{ T(java.lang.Math).random() * 100.0 })注入表达式结果Value(#{demoService.another})注入其他 Bean 属性Value(classpath:com/example/test.txt)注入文件资源Value(http://www.example.com)注入网址资源Value(${book.name})注入配置文件属性使用 $ 符号ConfigurationProperties将 properties 属性和 Bean 关联实现类型安全配置EnableConfigurationProperties开启对 ConfigurationProperties 注解配置 Bean 的支持2.3 环境配置Profile为不同环境使用不同的配置如 Profile(dev)ActiveProfiles声明活动的 profileConditional条件注解根据特定条件创建特定的 Bean三、AOP 与事务3.1 AOP 切面编程Aspect声明这是一个切面After、Before、Around定义切面可直接将拦截规则作为参数PointCut专门定义拦截规则然后在 After、Before、Around 中调用EnableAspectJAutoProxy开启 Spring 对 AspectJ 切面的支持3.2 事务管理Transactional声明式事务处理EnableTransactionManagement开启对注解式事务的支持四、缓存与调度4.1 缓存注解Cacheable数据缓存EnableCaching开启注解式缓存的支持4.2 异步与调度Async声明这是一个异步任务可在类级别和方法级别声明EnableAsync开启异步任务支持多线程Scheduled声明这是一个计划任务支持 cron、fixedDelay、fixedRateEnableScheduling开启对计划任务的支持定时器五、Spring MVC 注解5.1 控制器相关Controller声明类为 Spring MVC 控制器RestControllerController ResponseBody 组合注解ControllerAdvice全局控制器配置ExceptionHandler全局异常处理如 ExceptionHandler(valueException.class)5.2 请求映射与参数处理RequestMapping映射 WEB 请求访问路径和参数ResponseBody将返回值放入 response 体内而不是返回页面RequestBody允许 request 参数在 request 体中PathVariable接收路径参数如 /test/{id}ModelAttribute绑定键值对到 Model 中InitBinder设置 WebDataBinder自动绑定前台请求参数到 Model六、Spring Boot 核心注解SpringBootApplicationSpring Boot 项目的核心注解组合了 Configuration、EnableAutoConfiguration 和 ComponentScanEnableAutoConfiguration让 Spring Boot 根据依赖自动配置 Spring 框架EnableAutoConfiguration(exclude{xxx.class})禁用特定的自动配置七、数据持久化JPA注解7.1 实体映射Entity映射数据库实体类Table指定表名如 Table(name S_PRODUCEINFO)Id声明主键 IDColumn映射数据库字段如 Column(name app_name, unique true, length 50)7.2 字段映射Enumerated枚举类型映射如 Enumerated(EnumType.STRING)Temporal时间格式映射Temporal(TemporalType.TIMESTAMP)完整日期时间Temporal(TemporalType.DATE)年月日Temporal(TemporalType.TIME)时分秒CreationTimestamp创建时间自动填充UpdateTimestamp更新时间自动填充7.3 数据访问EnableJpaRepositories开启对 Spring Data JPA Repository 的支持八、测试相关注解RunWith测试运行器RunWith(JUnit4.class)使用 JUnit4 运行RunWith(SpringJUnit4ClassRunner.class)在 Spring 测试环境中运行RunWith(Suite.class)测试套件Before在测试方法前初始化九、其他实用注解9.1 Enable* 注解集合EnableWebMVC开启对 Web MVC 的配置支持EnableDiscoveryClient让服务发现服务器Spring Cloud 服务发现EnableEurekaServer注册服务器Spring Cloud 服务注册9.2 警告抑制SuppressWarnings(unchecked)忽略 unchecked 警告SuppressWarnings(serial)忽略 serializable 类缺少 serialVersionUID 警告SuppressWarnings(deprecation)忽略使用已弃用方法的警告SuppressWarnings({unchecked, deprecation})同时忽略多种警告十、综合示例Entity Table(name S_PRODUCEINFO) Data NoArgsConstructor AllArgsConstructor public class ProduceInfoEntity { Id Column(name app_name, unique true, length 50) private String name; Column(name status) Enumerated(EnumType.STRING) private ProduceStatus status; Column(name create_time, updatable false) Temporal(TemporalType.TIMESTAMP) CreationTimestamp private Date createTime; Column(name update_time) Temporal(TemporalType.TIMESTAMP) UpdateTimestamp private Date updateTime; }注解说明Entity映射数据库实体类Table(name S_PRODUCEINFO)指定表名Id声明主键 IDColumn对应数据库字段及属性Enumerated(EnumType.STRING)采用枚举值类型和数据库字段交互Temporal时间格式映射获取规定格式的日期CreationTimestamp创建时间自动填充UpdateTimestamp更新时间自动填充

读完文章,也想定制专属网站?

尧图设计师 24 小时内与您沟通定制方案

免费获取报价