SpringBoot常用注解整理

SpringBoot常用注解整理
1.@SpringBootApplication
我们可以把 @SpringBootApplication看作是 @Configuration、@EnableAutoConfiguration、@ComponentScan 注解的集合。

2.@EnableAutoConfiguration:启用 SpringBoot 的自动配置机制

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

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

5.@Autowired:自动导入对象到类中,被注入进的类同样要被 Spring 容器管理
例如:
`@Service
public class UserService {

}

@RestController
@RequestMapping("/users")
public class UserController {
@Autowired
private UserService userService;

}`

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

7.@RestController:
@RestController注解是@Controller和@ResponseBody的合集,表示这是个控制器 bean,并且是将函数的返回值直 接填入 HTTP 响应体中,是 REST 风格的控制器。

8.@Scope:
声明 Spring Bean 的作用域,使用方法。例如:

@Bean
@Scope(“singleton”)
public Person personSingleton() {
return new Person();
}
四种常见的 Spring Bean 的作用域:
singleton : 唯一 bean 实例,Spring 中的 bean 默认都是单例的。
prototype : 每次请求都会创建一个新的 bean 实例。
request : 每一次 HTTP 请求都会产生一个新的 bean,该 bean 仅在当前 HTTP request 内有效。
session : 每一次 HTTP 请求都会产生一个新的 bean,该 bean 仅在当前 HTTP session 内有效。

9.@Configuration:
一般用来声明配置类,可以使用 @Component注解替代,不过使用Configuration注解声明配置类更加语义化。(因为英文中Configuration有“配置”的含义)

10.请求类型相关注解:
@GetMapping(“users”) 等同于@RequestMapping(value="/users",method=RequestMethod.GET)
@PostMapping(“users”) 等同于@RequestMapping(value="/users",method=RequestMethod.POST)
@PutMapping("/users/{userId}") ) 等同于@RequestMapping(value="/users/{userId}",method=RequestMethod.PUT)
@DeleteMapping("/users/{userId}")等同于@RequestMapping(value="/users/{userId}",method=RequestMethod.DELETE)

11.@PathVariable 和 @RequestParam
@PathVariable用于获取路径参数,@RequestParam用于获取查询参数。例如:

@GetMapping("/klasses/{klassId}/teachers")
public List getKlassRelatedTeachers(
@PathVariable(“klassId”) Long klassId,
@RequestParam(value = “type”, required = false) String type ) {

}
如果我们请求的 url 是:/klasses/{123456}/teachers?type=web
那么我们服务获取到的数据就是:klassId=123456,type=web。

12.@RequestBody:
用于读取 Request 请求(可能是 POST,PUT,DELETE,GET 请求)的 body 部分并且Content-Type 为 application/json 格式的数据,接收到数据之后会自动将数据绑定到 Java 对象上去。例如:
@PostMapping("/sign-up")
public ResponseEntity signUp(@RequestBody @Valid UserRegisterRequest userRegisterRequest) {
userService.save(userRegisterRequest);
return ResponseEntity.ok().build();
}
注意:一个请求方法只可以有一个@RequestBody,但是可以有多个@RequestParam和@PathVariable。

  1. 常用的字段验证的注解:
    @NotEmpty 被注释的字符串的不能为 null 也不能为空
    @NotBlank 被注释的字符串非 null,并且必须包含一个非空白字符
    @Null 被注释的元素必须为 null
    @NotNull 被注释的元素必须不为 null
    @AssertTrue 被注释的元素必须为 true
    @AssertFalse 被注释的元素必须为 false
    @Pattern(regex=,flag=)被注释的元素必须符合指定的正则表达式
    @Email 被注释的元素必须是 Email 格式。
    @Min(value)被注释的元素必须是一个数字,其值必须大于等于指定的最小值
    @Max(value)被注释的元素必须是一个数字,其值必须小于等于指定的最大值
    @DecimalMin(value)被注释的元素必须是一个数字,其值必须大于等于指定的最小值
    @DecimalMax(value) 被注释的元素必须是一个数字,其值必须小于等于指定的最大值
    @Size(max=, min=)被注释的元素的大小必须在指定的范围内
    @Digits (integer, fraction)被注释的元素必须是一个数字,其值必须在可接受的范围内
    @Past被注释的元素必须是一个过去的日期
    @Future 被注释的元素必须是一个将来的日期

14.@value:使用 @Value("${property}") 读取比较简单的配置信息

15.@ConfigurationProperties:通过@ConfigurationProperties读取配置信息并与 bean 绑定。

16.@Validated:这个注解可以告诉 Spring 去校验方法参数。

17.异常处理相关注解:
@ControllerAdvice :注解定义全局异常处理类
@ExceptionHandler :注解声明异常处理方法
使用方式例如:
@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {
/**
* 请求参数异常处理
*/
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<?> handleMethodArgumentNotValidException(MethodArgumentNotValidException ex, HttpServletRequest request) {

}
}

18.数据表相关的注解:
@Entity声明一个类对应一个数据库实体。
@Table 设置表名。
@Column 声明字段。
@Transient :声明不需要与数据库映射的字段,在保存的时候不需要保存进数据库 。

19.@Transactional:在要开启事务的方法上使用@Transactional注解即可!

20.过滤json数据相关注解:
@JsonIgnoreProperties 作用在类上用于过滤掉特定字段不返回或者不解析。
@JsonIgnore一般用于类的属性上,作用和上面的@JsonIgnoreProperties 一样。
例如:
//生成json时将userRoles属性过滤
@JsonIgnoreProperties({“userRoles”})
public class User {
private String userName;
private String fullName;
private String password;
@JsonIgnore
private List userRoles = new ArrayList<>();
}

21.@JsonFormat一般用来格式化 json 数据。例如:
@JsonFormat(shape=JsonFormat.Shape.STRING, pattern=“yyyy-MM-dd’T’HH:mm:ss.SSS’Z’”, timezone=“GMT”)
private Date date;

22.测试相关注解:
@Test声明一个方法为测试方法
@Transactional被声明的测试方法的数据会回滚,避免污染测试数据。

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值