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

你可能感兴趣的文章
Oracle面试题:Oracle中truncate和delete的区别
查看>>
ThreadLocal线程内部存储类
查看>>
thinkphp 常用SQL执行语句总结
查看>>
Oracle:ORA-00911: 无效字符
查看>>
Text-to-Image with Diffusion models的巅峰之作:深入解读 DALL·E 2
查看>>
TCP基本入门-简单认识一下什么是TCP
查看>>
tableviewcell 中使用autolayout自适应高度
查看>>
Orcale表被锁
查看>>
svn访问报错500
查看>>
org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned
查看>>
org.apache.ibatis.type.TypeException: Could not resolve type alias 'xxxx'异常
查看>>
org.apache.poi.hssf.util.Region
查看>>
org.apache.xmlbeans.XmlOptions.setEntityExpansionLimit(I)Lorg/apache/xmlbeans/XmlOptions;
查看>>
org.apache.zookeeper.KeeperException$ConnectionLossException: KeeperErrorCode = ConnectionLoss for /
查看>>
org.hibernate.HibernateException: Unable to get the default Bean Validation factory
查看>>
org.hibernate.ObjectNotFoundException: No row with the given identifier exists:
查看>>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
查看>>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
查看>>
org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size exceeded
查看>>
org.tinygroup.serviceprocessor-服务处理器
查看>>