SpringBoot中必须掌握的45个注解

沙海 2021年5月26日04:57:35Java评论16字数 4258阅读14分11秒阅读模式
摘要

速读摘要

速读摘要文章源自JAVA秀-https://www.javaxiu.com/25955.html

指定request中必须包含某些参数值是,才让该方法处理。指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回。对于SpringCloud来说,这是每一微服务必须应有的三个注解,所以才推出了@SpringCloudApplication这一注解集合。MyBatis、Redis、MongoDB、ES、分库分表、读写分离、SpringMVC、Webflux、权限、WebSocket、Dubbo、RabbitMQ、RocketMQ、Kafka、性能测试等等内容。文章源自JAVA秀-https://www.javaxiu.com/25955.html

原文约 5932 | 图片 6 | 建议阅读 12 分钟 | 评价反馈文章源自JAVA秀-https://www.javaxiu.com/25955.html

SpringBoot中必须掌握的45个注解

点击关注 ? 芋道源码 文章源自JAVA秀-https://www.javaxiu.com/25955.html

收录于话题文章源自JAVA秀-https://www.javaxiu.com/25955.html

#芋道源码文章源自JAVA秀-https://www.javaxiu.com/25955.html

87个文章源自JAVA秀-https://www.javaxiu.com/25955.html

点击上方“芋道源码”,选择“设为星标文章源自JAVA秀-https://www.javaxiu.com/25955.html

管她前浪,还是后浪?文章源自JAVA秀-https://www.javaxiu.com/25955.html

能浪的浪,才是好浪!文章源自JAVA秀-https://www.javaxiu.com/25955.html

每天 8:55 更新文章,每天掉亿点点头发...文章源自JAVA秀-https://www.javaxiu.com/25955.html

文章源自JAVA秀-https://www.javaxiu.com/25955.html

源码精品专栏文章源自JAVA秀-https://www.javaxiu.com/25955.html

 文章源自JAVA秀-https://www.javaxiu.com/25955.html

文章源自JAVA秀-https://www.javaxiu.com/25955.html

来源:网络文章源自JAVA秀-https://www.javaxiu.com/25955.html

1.SpringBoot/spring

@SpringBootApplication:

包含@Configuration、@EnableAutoConfiguration、@ComponentScan通常用在主类上;文章源自JAVA秀-https://www.javaxiu.com/25955.html

@Repository:

用于标注数据访问组件,即DAO组件;文章源自JAVA秀-https://www.javaxiu.com/25955.html

@Service:

用于标注业务层组件;文章源自JAVA秀-https://www.javaxiu.com/25955.html

@RestController:

用于标注控制层组件(如struts中的action),包含@Controller和@ResponseBody;文章源自JAVA秀-https://www.javaxiu.com/25955.html

@Controller:

用于标注是控制层组件,需要返回页面时请用@Controller而不是@RestController;文章源自JAVA秀-https://www.javaxiu.com/25955.html

@Component:

泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注;文章源自JAVA秀-https://www.javaxiu.com/25955.html

@ResponseBody:

表示该方法的返回结果直接写入HTTP response body中,一般在异步获取数据时使用,在使用@RequestMapping后,返回值通常解析为跳转路径,文章源自JAVA秀-https://www.javaxiu.com/25955.html

加上@responsebody后返回结果不会被解析为跳转路径,而是直接写入HTTP response body中;比如异步获取json数据,加上@responsebody后,会直接返回json数据;文章源自JAVA秀-https://www.javaxiu.com/25955.html

@RequestBody:

参数前加上这个注解之后,认为该参数必填。表示接受json字符串转为对象 List等;文章源自JAVA秀-https://www.javaxiu.com/25955.html

@ComponentScan:

组件扫描。个人理解相当于,如果扫描到有@Component @Controller @Service等这些注解的类,则把这些类注册为bean*;文章源自JAVA秀-https://www.javaxiu.com/25955.html

@Configuration:

指出该类是 Bean 配置的信息源,相当于XML中的,一般加在主类上;文章源自JAVA秀-https://www.javaxiu.com/25955.html

@Bean:

相当于XML中的,放在方法的上面,而不是类,意思是产生一个bean,并交给spring管理;文章源自JAVA秀-https://www.javaxiu.com/25955.html

@EnableAutoConfiguration:

让 Spring Boot 根据应用所声明的依赖来对 Spring 框架进行自动配置,一般加在主类上;文章源自JAVA秀-https://www.javaxiu.com/25955.html

@AutoWired:

byType方式。把配置好的Bean拿来用,完成属性、方法的组装,它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作;文章源自JAVA秀-https://www.javaxiu.com/25955.html

当加上(required=false)时,就算找不到bean也不报错;文章源自JAVA秀-https://www.javaxiu.com/25955.html

@Qualifier:

当有多个同一类型的Bean时,可以用@Qualifier(“name”)来指定。与@Autowired配合使用;文章源自JAVA秀-https://www.javaxiu.com/25955.html

@Resource(name=”name”,type=”type”):

没有括号内内容的话,默认byName。与@Autowired干类似的事;文章源自JAVA秀-https://www.javaxiu.com/25955.html

@RequestMapping:

RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上。用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径;文章源自JAVA秀-https://www.javaxiu.com/25955.html

该注解有六个属性:文章源自JAVA秀-https://www.javaxiu.com/25955.html

params:指定request中必须包含某些参数值是,才让该方法处理。文章源自JAVA秀-https://www.javaxiu.com/25955.html

headers:指定request中必须包含某些指定的header值,才能让该方法处理请求。文章源自JAVA秀-https://www.javaxiu.com/25955.html

value:指定请求的实际地址,指定的地址可以是URI Template 模式文章源自JAVA秀-https://www.javaxiu.com/25955.html

method:指定请求的method类型, GET、POST、PUT、DELETE等文章源自JAVA秀-https://www.javaxiu.com/25955.html

consumes:指定处理请求的提交内容类型(Content-Type),如application/json,text/html;文章源自JAVA秀-https://www.javaxiu.com/25955.html

produces:指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回。文章源自JAVA秀-https://www.javaxiu.com/25955.html

@GetMapping、@PostMapping等:

相当于@RequestMapping(value=”/”,method=RequestMethod.Get\Post\Put\Delete等) 。是个组合注解;文章源自JAVA秀-https://www.javaxiu.com/25955.html

@RequestParam:

用在方法的参数前面。相当于 request.getParameter();文章源自JAVA秀-https://www.javaxiu.com/25955.html

@PathVariable:

路径变量。如 RequestMapping(“user/get/mac/{macAddress}”) ;文章源自JAVA秀-https://www.javaxiu.com/25955.html

public String getByMacAddress(@PathVariable(“macAddress”) String macAddress) {    //do something;}

参数与大括号里的名字相同的话,注解后括号里的内容可以不填。文章源自JAVA秀-https://www.javaxiu.com/25955.html

2. Jpa

@Entity:

@Table(name=”“):

表明这是一个实体类。一般用于jpa ,这两个注解一般一块使用,但是如果表名和实体类名相同的话,@Table可以省略;文章源自JAVA秀-https://www.javaxiu.com/25955.html

@MappedSuperClass:

用在确定是父类的entity上。父类的属性子类可以继承;文章源自JAVA秀-https://www.javaxiu.com/25955.html

@NoRepositoryBean:

一般用作父类的repository,有这个注解,spring不会去实例化该repository;文章源自JAVA秀-https://www.javaxiu.com/25955.html

@Column:

如果字段名与列名相同,则可以省略;文章源自JAVA秀-https://www.javaxiu.com/25955.html

@Id:

表示该属性为主键;文章源自JAVA秀-https://www.javaxiu.com/25955.html

@GeneratedValue(strategy=GenerationType.SEQUENCE,generator = “repair_seq”):

表示主键生成策略是sequence(可以为Auto、IDENTITY、native等,Auto表示可在多个数据库间切换),指定sequence的名字是repair_seq;文章源自JAVA秀-https://www.javaxiu.com/25955.html

@SequenceGenerator(name = “repair_seq”, sequenceName = “seq_repair”, allocationSize = 1):

name为sequence的名称,以便使用,sequenceName为数据库的sequence名称,两个名称可以一致;文章源自JAVA秀-https://www.javaxiu.com/25955.html

@Transient:

表示该属性并非一个到数据库表的字段的映射,ORM框架将忽略该属性.文章源自JAVA秀-https://www.javaxiu.com/25955.html

如果一个属性并非数据库表的字段映射,就务必将其标示为@Transient,否则,ORM框架默认其注解为@Basic;文章源自JAVA秀-https://www.javaxiu.com/25955.html

@Basic(fetch=FetchType.LAZY):

标记可以指定实体属性的加载方式;文章源自JAVA秀-https://www.javaxiu.com/25955.html

@JsonIgnore:

作用是json序列化时将java bean中的一些属性忽略掉,序列化和反序列化都受影响;文章源自JAVA秀-https://www.javaxiu.com/25955.html

@JoinColumn(name=”loginId”):

一对一:本表中指向另一个表的外键。文章源自JAVA秀-https://www.javaxiu.com/25955.html

一对多:另一个表指向本表的外键。文章源自JAVA秀-https://www.javaxiu.com/25955.html

@OneToOne

@OneToMany

@ManyToOne:

对应Hibernate配置文件中的一对一,一对多,多对一。文章源自JAVA秀-https://www.javaxiu.com/25955.html

3. 全局异常处理

@ControllerAdvice:

包含@Component。可以被扫描到。统一处理异常;文章源自JAVA秀-https://www.javaxiu.com/25955.html

@ExceptionHandler(Exception.class):

用在方法上面表示遇到这个异常就执行以下方法。文章源自JAVA秀-https://www.javaxiu.com/25955.html

4. SpringCloud

@EnableEurekaServer:

用在springboot启动类上,表示这是一个eureka服务注册中心;文章源自JAVA秀-https://www.javaxiu.com/25955.html

@EnableDiscoveryClient:

用在springboot启动类上,表示这是一个服务,可以被注册中心找到;文章源自JAVA秀-https://www.javaxiu.com/25955.html

@LoadBalanced:

开启负载均衡能力;文章源自JAVA秀-https://www.javaxiu.com/25955.html

@EnableCircuitBreaker:

用在启动类上,开启断路器功能;文章源自JAVA秀-https://www.javaxiu.com/25955.html

@HystrixCommand(fallbackMethod=”backMethod”):

用在方法上,fallbackMethod指定断路回调方法;文章源自JAVA秀-https://www.javaxiu.com/25955.html

@EnableConfigServer:

用在启动类上,表示这是一个配置中心,开启Config Server;文章源自JAVA秀-https://www.javaxiu.com/25955.html

@EnableZuulProxy:

开启zuul路由,用在启动类上;文章源自JAVA秀-https://www.javaxiu.com/25955.html

@SpringCloudApplication:

包含文章源自JAVA秀-https://www.javaxiu.com/25955.html

@SpringBootApplication文章源自JAVA秀-https://www.javaxiu.com/25955.html

@EnableDiscovertyClient文章源自JAVA秀-https://www.javaxiu.com/25955.html

@EnableCircuitBreaker文章源自JAVA秀-https://www.javaxiu.com/25955.html

分别是SpringBoot注解、注册服务中心Eureka注解、断路器注解。对于SpringCloud来说,这是每一微服务必须应有的三个注解,所以才推出了@SpringCloudApplication这一注解集合。## *1.SpringBoot/spring *文章源自JAVA秀-https://www.javaxiu.com/25955.html

文章源自JAVA秀-https://www.javaxiu.com/25955.html

欢迎加入我的知识星球,一起探讨架构,交流源码。加入方式,长按下方二维码噢文章源自JAVA秀-https://www.javaxiu.com/25955.html

SpringBoot中必须掌握的45个注解文章源自JAVA秀-https://www.javaxiu.com/25955.html

已在知识星球更新源码解析如下:文章源自JAVA秀-https://www.javaxiu.com/25955.html

SpringBoot中必须掌握的45个注解文章源自JAVA秀-https://www.javaxiu.com/25955.html

SpringBoot中必须掌握的45个注解文章源自JAVA秀-https://www.javaxiu.com/25955.html

SpringBoot中必须掌握的45个注解文章源自JAVA秀-https://www.javaxiu.com/25955.html

SpringBoot中必须掌握的45个注解文章源自JAVA秀-https://www.javaxiu.com/25955.html

最近更新《芋道 SpringBoot 2.X 入门》系列,已经 20 余篇,覆盖了 MyBatis、Redis、MongoDB、ES、分库分表、读写分离、SpringMVC、Webflux、权限、WebSocket、Dubbo、RabbitMQ、RocketMQ、Kafka、性能测试等等内容。文章源自JAVA秀-https://www.javaxiu.com/25955.html

提供近 3W 行代码的 SpringBoot 示例,以及超 4W 行代码的电商微服务项目。文章源自JAVA秀-https://www.javaxiu.com/25955.html

获取方式:点“在看”,关注公众号并回复 666 领取,更多内容陆续奉上。文章源自JAVA秀-https://www.javaxiu.com/25955.html

文章源自JAVA秀-https://www.javaxiu.com/25955.html

文章有帮助的话,在看,转发吧。谢谢支持哟 (*^__^*)
文章源自JAVA秀-https://www.javaxiu.com/25955.html

阅读原文文章源自JAVA秀-https://www.javaxiu.com/25955.html

继续阅读
速蛙云 - 极致体验,强烈推荐!!!购买套餐就免费送各大视频网站会员!快速稳定、独家福利社、流媒体稳定解锁!速度快,全球上网、视频、游戏加速、独立IP均支持!基础套餐性价比很高!这里不多说,我一直正在使用,推荐购买:https://www.javaxiu.com/59919.html
weinxin
资源分享QQ群
本站是JAVA秀团队的技术分享社区, 会经常分享资源和教程; 分享的时代, 请别再沉默!
沙海
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定