华为技术专家整理Spring Boot 注解大全,一键收藏了

1、注解 (annotations) 列表

@SpringBootApplicationhtml

包含了 @ComponentScan、@Configuration 和 @EnableAutoConfiguration 注解。vue

其中 @ComponentScan 让 spring Boot 扫描到 Configuration 类并把它加入到程序上下文。git

@ConfigurationU等同于 spring 的 XML 配置文件;使用 Java 代码能够检查类型安全。web

**@EnableAutoConfiguration ** 自动配置。spring

**@ComponentScan ** 组件扫描,可自动发现和装配一些 Bean。数据库

@Component 可配合 CommandLineRunner 使用,在程序启动后执行一些基础任务。json

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

@Autowired 自动导入。api

@PathVariable 获取参数。安全

@JsonBackReference 解决嵌套外链问题。

@RepositoryRestResourcepublic 配合 spring-boot-starter-data-rest 使用。

2、注解 (annotations) 详解

@SpringBootApplication:申明让 spring boot 自动给程序进行必要的配置,这个配置等同于:@Configuration ,@EnableAutoConfiguration 和 @ComponentScan 三个配置。

package com.example.myproject;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScan
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

@ResponseBody:表示该方法的返回结果直接写入 HTTP response body 中,通常在异步获取数据时使用,用于构建 RESTful 的 api。

在使用 @RequestMapping 后,返回值一般解析为跳转路径,加上 @responsebody 后返回结果不会被解析为跳转路径,而是直接写入 HTTP response body 中。

好比异步获取 json 数据,加上 @responsebody 后,会直接返回 json 数据。

该注解通常会配合 @RequestMapping 一块儿使用。示例代码:

@RequestMapping(“/test”)
@ResponseBody
public String test(){
    return”ok”;
}

@Controller:用于定义控制器类,在 spring 项目中由控制器负责将用户发来的 URL 请求转发到对应的服务接口(service 层)

通常这个注解在类中,一般方法须要配合注解 @RequestMapping。

示例代码:

@Controller
@RequestMapping(“/demoInfo”)
publicclass DemoController {
    @Autowired
    private DemoInfoService demoInfoService;

    @RequestMapping("/hello")
    public String hello(Map map){
        System.out.println("DemoController.hello()");
        map.put("hello","from TemplateController.helloHtml");
        //会使用hello.html或者hello.ftl模板进行渲染显示.
        return"/hello";
    }
}

@RestController:用于标注控制层组件 (如 struts 中的 action),@ResponseBody 和 @Controller 的合集。

示例代码:

package com.kfit.demo.web;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(“/demoInfo2”)
publicclass DemoController2 {

    @RequestMapping("/test")
    public String test(){
        return"ok";
    }
}

@RequestMapping:提供路由信息,负责 URL 到 Controller 中的具体函数的映射。

@EnableAutoConfiguration:Spring Boot 自动配置(auto-configuration):尝试根据你添加的 jar 依赖自动配置你的 Spring 应用。

例如,若是你的 classpath 下存在 HSQLDB,而且你没有手动配置任何数据库链接 beans,那么咱们将自动配置一个内存型(in-memory)数据库”。

你能够将 @EnableAutoConfiguration 或者 @SpringBootApplication 注解添加到一个 @Configuration 类上来选择自动配置。

搜索公众号:MarkerHub,关注回复[ vue ]获取先后端入门教程!

若是发现应用了你不想要的特定自动配置类,你可使用 @EnableAutoConfiguration 注解的排除属性来禁用它们。

@ComponentScan:表示将该类自动发现扫描组件。

我的理解至关于,若是扫描到有 @Component、@Controller、@Service 等这些注解的类,并注册为 Bean,能够自动收集全部的 Spring 组件,包括 @Configuration 类。

咱们常用 @ComponentScan 注解搜索 beans,并结合 @Autowired 注解导入。能够自动收集全部的 Spring 组件,包括 @Configuration 类。

若是没有配置的话,Spring Boot 会扫描启动类所在包下以及子包下的使用了 @Service,@Repository 等注解的类。

@Configuration:至关于传统的 xml 配置文件,若是有些第三方库须要用到 xml 文件,建议仍然经过 @Configuration 类做为项目的配置主类——可使用 @ImportResource 注解加载 xml 配置文件。

@Import:用来导入其余配置类。

@ImportResource:用来加载 xml 配置文件。

@Autowired:自动导入依赖的 bean

@Service:通常用于修饰 service 层的组件

@Repository:使用 @Repository 注解能够确保 DAO 或者 repositories 提供异常转译,这个注解修饰的 DAO 或者 repositories 类会被 ComponetScan 发现并配置,同时也不须要为它们提供 XML 配置项。

@Bean:用 @Bean 标注方法等价于 XML 中配置的 bean。

@Value:注入 Spring boot application.properties 配置的属性的值。示例代码:

@Value(value = “#{message}”)
private String message;

@Inject:等价于默认的 @Autowired,只是没有 required 属性;

@Component:泛指组件,当组件很差归类的时候,咱们可使用这个注解进行标注。

@Bean:至关于 XML 中的, 放在方法的上面,而不是类,意思是产生一个 bean, 并交给 spring 管理。

@AutoWired:自动导入依赖的 bean。byType 方式。把配置好的 Bean 拿来用,完成属性、方法的组装,它能够对类成员变量、方法及构造函数进行标注,完成自动装配的工做。当加上(required=false)时,就算找不到 bean 也不报错。

@Qualifier:当有多个同一类型的 Bean 时,能够用 @Qualifier(“name”) 来指定。与 @Autowired 配合使用。@Qualifier 限定描述符除了能根据名字进行注入,但能进行更细粒度的控制如何选择候选者,具体使用方式以下:

@Autowired
@Qualifier(value = “demoInfoService”)
private DemoInfoService demoInfoService;

@Resource(name=”name”,type=”type”):没有括号内内容的话,默认 byName。与 @Autowired 干相似的事。

3、JPA 注解

@Entity:@Table(name=”“):代表这是一个实体类。通常用于 jpa 这两个注解通常一块使用,可是若是表名和实体类名相同的话,@Table 能够省略

@MappedSuperClass: 用在肯定是父类的 entity 上。父类的属性子类能够继承。

@NoRepositoryBean: 通常用做父类的 repository,有这个注解,spring 不会去实例化该 repository。

@Column:若是字段名与别名相同,则能够省略。

@Id:表示该属性为主键。

@GeneratedValue(strategy=GenerationType.SEQUENCE,generator= “repair_seq”):表示主键生成策略是 sequence(能够为 Auto、IDENTITY、native 等,Auto 表示可在多个数据库间切换),指定 sequence 的名字是 repair_seq。

@SequenceGeneretor(name = “repair_seq”, sequenceName = “seq_repair”, allocationSize = 1):name 为 sequence 的名称,以便使用,sequenceName 为数据库的 sequence 名称,两个名称能够一致。

@Transient:表示该属性并不是一个到数据库表的字段的映射, ORM 框架将忽略该属性。

若是一个属性并不是数据库表的字段映射, 就务必将其标示为 @Transient, 不然, ORM 框架默认其注解为 @Basic。@Basic(fetch=FetchType.LAZY):标记能够指定实体属性的加载方式

@JsonIgnore:做用是 json 序列化时将 Java bean 中的一些属性忽略掉, 序列化和反序列化都受影响。

@JoinColumn(name=”loginId”): 一对一:本表中指向另外一个表的外键。一对多:另外一个表指向本表的外键。

@OneToOne、@OneToMany、@ManyToOne:对应 hibernate 配置文件中的一对一,一对多,多对一。

4、springMVC 相关注解

@RequestMapping:@RequestMapping(“/path”)表示该控制器处理全部 “/path” 的 UR L 请求。

RequestMapping 是一个用来处理请求地址映射的注解,可用于类或方法上。

用于类上,表示类中的全部响应请求的方法都是以该地址做为父路径。该注解有六个属性:

params: 指定 request 中必须包含某些参数值是,才让该方法处理。

headers: 指定 request 中必须包含某些指定的 header 值,才能让该方法处理请求。

value: 指定请求的实际地址,指定的地址能够是 URI Template 模式

method: 指定请求的 method 类型, GET、POST、PUT、DELETE 等

consumes: 指定处理请求的提交内容类型(Content-Type),如 application/json,text/html;

produces: 指定返回的内容类型,仅当 request 请求图中的 (Accept) 类型中包含该指定类型才返回

@RequestParam:用在方法的参数前面。
@RequestParam
String a =request.getParameter(“a”)。

@PathVariable: 路径变量。如

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

参数与大括号里的名字同样要相同。

5、全局异常处理

@ControllerAdvice:包含 @Component。能够被扫描到。统一处理异常。

@ExceptionHandler(Exception.class):用在方法上面表示遇到这个异常就执行如下方法。


怎么样,刺激吗?你会或者知道多少呢?

而这是我在平常开发的过程当中,常常用到的或者见到的一些经常使用的注解方式,但愿对你们可以有所帮助

下面是和你们分享的一份SpringBoot的文档

Spring Data JPA

Spring Boot配置

部署Spring Boot应用

Testing单元测试

相应的文章已经整理造成文档,git扫码获取资料看这里

https://gitee.com/biwangsheng/personal.git

相关文章
相关标签/搜索