博客
关于我
SpringBoot中常用注解
阅读量:362 次
发布时间:2019-03-04

本文共 1168 字,大约阅读时间需要 3 分钟。

Spring Boot 常用注解与组件注册指南

Spring Boot 是 Spring 的一个简化版,专注于快速搭建微服务项目,省去了繁琐的 XML 配置,让开发者更专注于业务逻辑的实现。以下是 Spring Boot 中常用的注解和组件注册说明。


1. Spring Boot 启动类注解

@SpringBootApplication 是一个注解组合,包含以下三个重要注解:

  • @SpringBootConfiguration:声明当前类是 Spring Boot 的配置类,通常用于主启动类。
  • @EnableAutoConfiguration:开启 Spring Boot 的自动配置功能,会自动加载符合条件的 bean。
  • @ComponentScan:启用组件扫描,自动注册包内的 @Component 组件。

2. 组件注册

Spring Boot 组件注册主要通过以下注解实现:

  • @ComponentScan:启用组件扫描,将包及其子包内的 @Component@Service@Controller 等注解标记的类注册到 Spring 容器中。
  • @Controller:标注控制层组件,常用于处理 HTTP 请求。
  • @RestController@Controller@ResponseBody 的合集,用于标注 RESTful 控制器,返回 JSON 数据。
  • @Repository:标注数据访问组件,通常用于 DAO 层。
  • @Service:标注业务逻辑组件,通常用于 Service 层。
  • @Bean:在方法上使用,表示将返回值注册为 Spring 容器中的 bean。
  • @Configuration:标注配置类,类似于 Spring 的 @Configuration,用于定义 bean。
  • @Autowired:注入 Spring 容器中的 bean,常用于类属性或方法参数。
  • @Resource:与 @Autowired 类似,但通过资源名称注入 bean。
  • @RequestMapping:用于映射 HTTP 请求地址,常用于 @Controller 类上或方法上。
  • @GetMapping@PostMapping@RequestMapping 的更具体版本,用于方法级请求映射。

3. 请求参数处理

  • @RequestBody:接收 HTTP 请求体中的数据,直接将其封装为实体类。
  • @RequestParam:在方法参数上使用,相当于 request.getParameter(), 用于获取请求参数。
  • @PathVariable:获取请求地址栏中的占位符值,常用于 URL 参数。

以上注解和组件注册方式可以帮助开发者快速搭建 Spring Boot 项目,简化配置管理,专注于业务逻辑实现。

转载地址:http://vfzg.baihongyu.com/

你可能感兴趣的文章
Prometheus 采集器使用详解
查看>>
Prometheus 黑盒监控实战
查看>>
prometheus+alertmanager+grafana监控部署教程
查看>>
Prometheus+Grafana构建智能化Kubernetes监控系统实战
查看>>
Prometheus+SpringBoot应用监控全过程详解
查看>>
Prometheus+SpringBoot应用监控全过程详解
查看>>
prometheus安装
查看>>
Prometheus实战教程:监控Kafka消息
查看>>
Prometheus实战教程:监控mysql数据库
查看>>
Prometheus实战教程:监控Nginx状态
查看>>
prometheus常用exporter下载地址大全
查看>>
Prometheus快速搭建与监控Linux系统实战
查看>>
prometheus报警与恢复告警的格式
查看>>
Pytorch中安装 torch_geometric 详细图文操作(全)
查看>>
prometheus监控docker容器实战
查看>>
Prometheus监控k8s集群使用邮箱和微信告警!
查看>>
Prometheus监控mysq数据库实战
查看>>
prometheus监控nginx实战
查看>>
Prometheus监控redis数据库实战
查看>>
Prometheus监控教程:使用Grafana展示主机基本信息
查看>>