博客
关于我
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/

你可能感兴趣的文章
Project Euler Problem 12: Highly divisible triangular number
查看>>
ProjectEuler 2
查看>>
projection介绍及EPSG:4326和EPSG:3857的投射转换
查看>>
project打开文件时,显示无法识别此文件格式?
查看>>
Prometheus + Grafana on Kubernetes部署
查看>>
prometheus + grafana进行服务器资源监控
查看>>
Prometheus Alertmanager 告警配置详解
查看>>
Prometheus Grafana 展示平台
查看>>
Prometheus pushgateway使用详解
查看>>
Prometheus 云原生 - Prometheus 数据模型、Metrics 指标类型、Exporter 相关
查看>>
Prometheus 云原生 - 基于 file_sd、http_sd 实现 Service Discovery
查看>>
Prometheus 云原生 - 微服务监控报警系统 (Promethus、Grafana、Node_Exporter)部署、简单使用
查看>>
Prometheus 云原生 - 监控 Linux、MySQL、Redis、RabbitMQ、Docker、SpringBoot 3.x
查看>>
Prometheus 介绍
查看>>
Prometheus 安全配置详解
查看>>
prometheus 安装node_exporter, node_exporter 安装最新版 普罗米修思安装监控服务器client
查看>>
Prometheus 安装部署实战
查看>>
prometheus 持久化存储方案
查看>>
Prometheus 监控系统企业级实战
查看>>
Prometheus 监控系统简介
查看>>