java框架常用注解总结

目录

一、Spring

@Component / @Controller / @Service / @Repository

@Configuration

 @ComponentScan

@Scope

 @PostConstruct

@PreDestroy

 @Autowired

 @Qualifier

@Value

 @PropertySource

 @Bean​编辑

@Import

 @RunWith

@ContextConfiguration​编辑 @EnableAspectJAutoProxy

 @Aspect​编辑

 @Pointcut​编辑

 @Before​编辑

 @After​编辑

 @AfterReturning​编辑

 @AfterThrowing​编辑

 @Around​编辑

 @EnableTransactionManagement

 @Transactional​编辑

 二、SpringMVC

@Controller​编辑

 @RequestMapping

 @ResponseBody​编辑

 @ComponentScan​编辑

 @RequestParam​编辑

@EnableWebMvc​编辑 @RequestBody

 @DateTimeFormat​编辑

 @ResponseBody​编辑

 @PathVariable​编辑

 @RestController​编辑

 @GetMapping @PostMapping @PutMapping @DeleteMapping​编辑

 @RestControllerAdvice​编辑

 @ExceptionHandler​编辑

三、MyBatis

 四、MyBatisPlus

Lombok常见注解:

@TableField​编辑 @TableName​编辑

 @TableId​编辑

 @TableLogic​编辑

五、SpringBoot

SpringBoot常用注解

@SpringBootApplication

@EnableAutoConfiguration

 @ComponentScan

 @Configuration

@Autowired

@RestController

@PathVariable 和 @RequestParam

@ConfigurationProperties

 @PropertySource

@Scheduled

@EnableScheduling

@Aspect

@Pointcut

@Before

@Around

@After

@AfterReturning

@AfterThrowing

5 种常见的 HTTP 请求类型

六、Spring Security

@Secured

@PreAuthorize

@PostAuthorize

七、Spring Data JPA

@LastModifiedBy

@LastModifiedDate

@CreatedBy

@CreatedDate

@Modifying

@Param

@Query

@Transient

@Column

@GeneratedValue 

@Index

@Id

@Table

@Entity



*SSM框架是spring、spring MVC 、和mybatis框架的整合,是标准的MVC模式。

*标准的SSM框架有四层,分别是dao层(mapper),service层,controller层和View层

*使用spring实现业务对象管理,使用spring MVC负责请求的转发和视图管理,mybatis作为数据对象的持久化引擎

MybatisPlus:

(简称MP)是基于MyBatis框架基础上开发的增强型工具,旨在简化开发、提供效率。

一、Spring

@Component / @Controller / @Service / @Repository

  • 把类标识成可用于 @Autowired 注解自动装配的 bean 的类
  • @Component:通用的注解,可标注任意类为 Spring 组件。如果一个 Bean 不知道属于哪个层,可以使用@Component 注解标注。
  • @Repository : 对应持久层即 Dao 层,主要用于数据库相关操作。
  • @Service : 对应服务层,主要涉及一些复杂的逻辑,需要用到 Dao 层。
  • @Controller : 对应 Spring MVC 控制层,主要用于接受用户请求并调用 Service 层返回数据给前端页面。

@Configuration

 @ComponentScan

@Scope

四种常见的 Spring Bean 的作用域:

  • singleton : 唯一 bean 实例,Spring 中的 bean 默认都是单例的。
  • prototype : 每次请求都会创建一个新的 bean 实例。
  • request : 每一次 HTTP 请求都会产生一个新的 bean,该 bean 仅在当前 HTTP request 内有效。
  • session : 每一个 HTTP Session 会产生一个新的 bean,该 bean 仅在当前 HTTP session 内有效。

 @PostConstruct

@PreDestroy

 @Autowired

 @Qualifier

@Value

  • 常用@Value("${property}") 读取比较简单的配置信息 

 @PropertySource

 @Bean

@Import



 @RunWith

@ContextConfiguration @EnableAspectJAutoProxy

 @Aspect

 @Pointcut

 @Before

 @After

 @AfterReturning

 @AfterThrowing

 @Around

 @EnableTransactionManagement

 @Transactional

 二、SpringMVC

@Controller

 @RequestMapping

 @ResponseBody

 @ComponentScan

 @RequestParam

@EnableWebMvc @RequestBody

  • 形参注解,用于修饰参数
  • 用于接收JSON格式的参数,常应用于AJAX请求、前后端分享的场景下
  • 此注解一个处理器方法只能使用一次

 @DateTimeFormat

 @ResponseBody

 @PathVariable

 @RestController

 @GetMapping @PostMapping @PutMapping @DeleteMapping

 @RestControllerAdvice

 @ExceptionHandler

三、MyBatis

 

 四、MyBatisPlus

Lombok常见注解:

  • @Setter:为模型类的属性提供setter方法
  • @Getter:为模型类的属性提供getter方法
  • @ToString:为模型类的属性提供toString方法
  • @EqualsAndHashCode:为模型类的属性提供equals和hashcode方法
  • @Data:用在类上,是个组合注解,包含上面的注解的功能
  • @NoArgsConstructor:提供一个无参构造函数
  • @AllArgsConstructor:提供一个包含所有参数的构造函数
  • @Log4j 和 @Slf4j:用在类上,为类提供一个属性名为log 的 log4j日志对象
  • @NoArgsConstructor:用在类上,为类提供一个无参的构造方法
  • @AllArgsConstructor:用在类上,为类提供一个全参的构造方法
  • @RequiredArgsConstructor: 用在类上,生成一个包含 "特定参数" 的构造器,特定参数指的是那些有加上 final 修饰词的变量们
  • @Builder: 用在类上,自动生成流式 set 值写法,不用再写一堆 setter 了

  • @Cleanup: 可以用在 IO 流上,用于关闭并释放资源

  • @SneakyThrows :将当前方法抛出的异常,包装成RuntimeException,骗过编译器,使得调用点可以不用显示处理异常信息,使用注解后不需要担心Exception的处理。

@TableField @TableName

 @TableId

 @TableLogic

五、SpringBoot

SpringBoot常用注解

描述

@SpringBootApplication

  • 创建 SpringBoot 项目之后会默认在主类加上
  • 是SpringBoot项目的启动类
  • @SpringBootApplication=@EnableAutoConfiguration + @ComponentScan + @Configuration

@EnableAutoConfiguration

启用 SpringBoot 的自动配置机制

 @ComponentScan

扫描被@Component (@Repository,@Service,@Controller)注解的 bean,注解默认会扫描该类所在的包下所有的类

 @Configuration

允许在 Spring 上下文中注册额外的 bean 或导入其他配置类

@Autowired

自动导入对象到类中,被注入进的类同样要被 Spring 容器管理比如:Service 类注入到 Controller 类中。

@RestController

  •  @RestController注解是@Controller@ResponseBody的合集
  • 表示这是个控制器 bean,并且是将函数的返回值直接填入 HTTP 响应体中,是 REST 风格的控制器。
  • @Controller 不加 @ResponseBody:返回一个视图
  • @Controller +@ResponseBody:返回 JSON 或 XML 形式数据

@PathVariable 和 @RequestParam

  • 都是修饰参数的
  • @PathVariable用于获取路径URL参数,@RequestParam用于获取查询参数。
  • 一个请求方法只可以有一个@RequestBody,但是可以有多个@RequestParam@PathVariable

@ConfigurationProperties

读取配置信息并与 bean 绑定

 @PropertySource

读取指定 properties 文件

@Scheduled

自定义定时任务的配置

参数:

  • cron:cron表达式,指定任务在特定的时间执行
  • fixedDelay:任务执行的间隔时间(毫秒),long型
  • fixedDelayString:String型
  • fixedRate:任务执行的频率,long型
  • fixedRateString:String型
  • initialDelay:首次任务的延迟执行时间,long型
  • initialDelayString:String型
  • zone:默认为当前时区

@EnableScheduling

开启定时任务

AOP常用注解描述

@Aspect

标识该类为一个AOP的切面供容器读取,之后使用@Component将切面注入IOC容器

@Pointcut

  • 表示需要切入的位置
    如:@Pointcut("execution(public * com.study.demo.controller.*.*(..))")
    
    • execution:用来匹配连接点的执行方法
    • public:匹配访问权限为public的方法
    • 第一个*:返回值为任意类型
    • 第二个*:前面包路径下的任意类
    • 第三个*:任意方法
    • (..):任意参数

@Before

切入点的方法体执行之前执行

@Around

环绕通知,把切入点包裹起来执行

@After

在切入点正常运行结束后执行

@AfterReturning

在切入点正常运行结束后执行,异常则不执行

@AfterThrowing

在切入点运行异常时执行

执行顺序:around>before>aftereturning / afterthrowing>after>around 

5 种常见的 HTTP 请求类型

  • GET:请求从服务器获取特定资源。举个例子:GET /users(获取所有学生)
    • @GetMapping("users") 等价于@RequestMapping(value="/users",method=RequestMethod.GET)
  • POST:在服务器上创建一个新的资源。举个例子:POST /users(创建学生)
    • @PostMapping("users") 等价于@RequestMapping(value="/users",method=RequestMethod.POST)
  • PUT:更新服务器上的资源(客户端提供更新后的整个资源)。举个例子:PUT /users/12(更新编号为 12 的学生)
    • @PutMapping("/users/{userId}") 等价于@RequestMapping(value="/users/{userId}",method=RequestMethod.PUT)
  • DELETE:从服务器删除特定的资源。举个例子:DELETE /users/12(删除编号为 12 的学生)
    • @DeleteMapping("/users/{userId}")等价于@RequestMapping(value="/users/{userId}",method=RequestMethod.DELETE)
  • PATCH:更新服务器上的资源(客户端提供更改的属性,可以看做作是部分更新),使用的比较少
    •  PUT 不够用了之后才用 PATCH 请求去更新数据

六、Spring Security​​​​​​​

Spring Security常用注解描述

@Secured

拥有特定角色的用户可以访问 

@PreAuthorize

 拥有特定角色或权限的用户可以访问

@PostAuthorize

拥有特定角色或权限的用户可以收到返回值(执行完成后再判断权限) 

七、Spring Data JPA

目前java的持久层框架有两派:

  1. 以Hibernate为代表的JPA路线(更关注实体,封装对数据库的操作)
  2. 以Mybatis为代表的JDBC路线 (更关注表)
Spring Data JPA常用注解描述

@LastModifiedBy

最后更新的人,用于审计(自动填充)

@LastModifiedDate

最后更新的时间,用于审计(自动填充)

@CreatedBy

创建人,用于审计(自动填充)

@CreatedDate

创建时间,用于审计(自动填充)

@Modifying

与@Query配合使用,用于更新、删除

@Param

与@Query配合使用,用于参数绑定

@Query

自定义JPQL或原生SQL

@Transient

指定实体类中不与表关联的属性

@Column

定义数据库表字段 

@GeneratedValue 

  • 定义id生成策略
    • @GeneratedValue(strategy = GenerationType.IDENTITY)

    • @GeneratedValue(strategy = GenerationType.AUTO)

    • @GeneratedValue(strategy = GenerationType.SEQUENCE)

    • @GeneratedValue(strategy = GenerationType.TABLE)

@Index

定义数据库表的索引

@Id

定义数据库表的id

@Table

  • 定义数据库表
  • 可以通过name属性指定与实体类对应的表,如果实体类和表名相同,那么可以忽略

@Entity

用在类上,定义该类为实体类

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

戏拈秃笔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值