JAVA 整理常用注解说明
整理常用注解,及说明
1. org.springframework.boot.autoconfigure
| 注解 | 说明 |
|---|---|
| @SpringBootApplication | SpringBoot 项目程序启动入口配置,是一个复合注解,包含@SpringBootConfiguration、@EnableAutoConfiguration、@ComponentScan |
2. org.springframework.cloud.client
| 注解 | 说明 |
|---|---|
| @SpringCloudApplication | SpringCloud 项目程序启动入口配置,是一个复合注解,包含@SpringBootApplication、@EnableDiscoveryClient、@EnableCircuitBreaker |
| @ConditionalOnDiscoveryEnabled |
3. org.mybatis.spring.annotation
| 注解 | 说明 |
|---|---|
| @MapperScan | 扫描指定包下 mybatis 的接口文件 |
| @MapperScannerRegistrar | |
| @MapperScans |
4. org.springframework.context.annotation
| 注解 | 说明 |
|---|---|
| @Bean | |
| @ComponentScan | 扫描和自动装配指标有@Component 的类,并注册成 bean,当然包括@Component 下的子注解@Service,@Repository,@Controller。注解标记的类并纳入到 spring 容器中进管理 |
| @ComponentScans | 同时指定多个 ComponentScan |
| @Conditional | |
| @Configuration | 用于定义配置类,可替换 xml 配置文件,内部包含有一个或多个被@Bean 注解的方法,初始化 Spring 容器。 |
| @DependsOn | |
| @Description | |
| @EnableAspectJAutoProxy | |
| @EnableLoadTimeWeaving | |
| @EnableMBeanExport | |
| @Import | |
| @ImportResource | |
| @Lazy | 懒加载,当服务启动时,不进行初始化,调用时进行初始化, 可以减少容器启动的加载时间; 当出现循环依赖时,也可以添加@Lazy |
| @Primary | |
| @Profile | |
| @PropertySource | |
| @Role | |
| @Scope |
5. org.springframework.web.bind.annotation
| 注解 | 说明 |
|---|---|
| @ControllerAdvice | 异常集中处理,更好的使业务逻辑与异常处理剥离开 |
| @CookieValue | |
| @CrossOrigin | |
| @DeleteMapping | |
| @ExceptionHandler | 统一处理某一类异常,从而能够减少代码重复率和复杂度 |
| @GetMapping | http get 请求, 指定请求方式,路径等,默认封装了@RequestMapping(method = RequestMethod.GET) |
| @InitBinder | |
| @Mapping | |
| @MatrixVariable | |
| @ModelAttribute | |
| @-info | |
| @PatchMapping | |
| @PathVariable | |
| @PostMapping | http post 请求, 指定请求方式,路径等,默认封装了@RequestMapping(method = RequestMethod.POST) |
| @PutMapping | |
| @RequestAttribute | |
| @RequestBody | 用来接收 POST 请求的数据, @RequestBody 与@RequestParam 可以同时使用,最多一个 RequestBody,可以有多个 RequestParam |
| @RequestHeader | |
| @RequestMapping | |
| @RequestParam | 用来获取 url 中 key=value 的数据 |
| @RequestPart | |
| @ResponseBody | |
| @ResponseStatus | 映射为 HTTP 状态码 |
| @RestController | 应用于控制器, 包含@Controller @ResponseBody |
| @RestControllerAdvice | |
| @SessionAttribute | |
| @SessionAttributes | |
| @ValueConstants |
6. springfox.documentation.swagger2.annotations
| 注解 | 说明 |
|---|---|
| @EnableSwagger2 | 开启 Swagger2 |
7. io.swagger.annotations
| 注解 | 说明 |
|---|---|
| @Api | |
| @ApiImplicitParam | |
| @ApiImplicitParams | |
| @ApiKeyAuthDefinition | |
| @ApiModel | |
| @ApiModelProperty | |
| @ApiOperation | |
| @ApiParam | |
| @ApiResponse | |
| @ApiResponses | |
| @Authorization | |
| @AuthorizationScope | |
| @BasicAuthDefinition | |
| @Contact | |
| @Example | |
| @ExampleProperty | |
| @Extension | |
| @ExtensionProperty | |
| @ExternalDocs | |
| @Info | |
| @License | |
| @OAuth2Definition | |
| @ResponseHeader | |
| @Scope | |
| @SecurityDefinition | |
| @SwaggerDefinition | |
| @Tag |
8. org.springframework.beans.factory.annotation
| 注解 | 说明 |
|---|---|
| @Autowired | 对类成员变量进行标注,完成自动装配 |
| @Configurable | |
| @Lookup | |
| @Qualifier | |
| @Value | 从配置文件读取值 |
9. lombok
| 注解 | 说明 |
|---|---|
| @AllArgsConstructor | |
| @Builder | |
| @Cleanup | |
| @CustomLog | |
| @Data | 主要省去大量 Getter 和 Setter 繁琐的写法 |
| @EqualsAndHashCode | |
| @Generated | |
| @Getter | |
| @NoArgsConstructor | |
| @NonNull | 不为空 |
| @RequiredArgsConstructor | |
| @Setter | |
| @Singular | |
| @SneakyThrows | |
| @Synchronized | |
| @ToString | |
| @val | |
| @Value | |
| @var | |
| @With |
10. org.springframework.stereotype
| 注解 | 说明 |
|---|---|
| @Controller | 控制器层(注入服务) |
| @Service | 服务层(注入 dao) |
| @Repository | 持久层(实现 dao 访问) |
| @Component | 标注一个类为 Spring 容器的 Bean,(把普通 pojo 实例化到 spring 容器中,相当于配置文件中的) 泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注,标识为一个 Bean。 |

浙公网安备 33010602011771号