Spring Boot常用注解

爱被打了一巴掌 2021-09-01 05:03 449阅读 0赞

Spring Boot常用注解

  • 简介
    • 什么是注解式编程
    • 了解系统注解
    • Spring Boot的常用注解
      • 使用在类名上的注解
      • 使用在方法上的注解
    • 其他注解

简介

未来框架的大趋势是“约定大于配置”,代码封装会更加严密。开发人员会将更多的精力放在代码整体优化和业务逻辑上,所以注解式编程会被广泛地使用。

什么是注解式编程

注解(annotations)用来定义一个类、属性或一些方法,以便程序能被编译处理。它相当于一个说明文件,告诉应用程序某个被注解的类或属性是什么,要怎么处理。注解可以用于标注包、类、方法和变量等。
下方的代码中的注解@RestController,是一个用来定义Rest分割的控制器。其中,注解@GetMapping(“/hello”)定义访问路径是”/hello”

  1. @RestController
  2. public class HelloWordController {
  3. @RequestMapping("/hello")
  4. public String hello(){
  5. return "hello SpringBoot";
  6. }
  7. }

了解系统注解

在这里插入图片描述
下面重点介绍一下@SuppressWarnnings注解,它有一下几种属性。

  • unchecked:未检查的转换
  • unused:未使用的变量
  • resource:泛型,即未指定类型
  • path:在类中的路径
  • deprecation:使用了某些不赞成使用的类或方法
  • fallthrough:switch语句执行到底,不会遇到break关键字
  • serial:实现了Seriallizable,但为定义serialVersionUID
  • rawtypes:没有传递带有泛型的参数
  • all:代表全部类型警告

Spring Boot的常用注解

使用在类名上的注解

在这里插入图片描述

  1. @RestController
    它用于返回JSON(JavaScript Object Notation ,JS对象简谱)、XML(eXtensible Markup Language)等数据,但不能返回HTML(HyperText Markup Language)页面。相当于注解@ResponseBody和注解@Controller和在一起使用

    @RestController
    public class HelloWorldController {

    1. @RequestMapping("/hello")
    2. public String hello() throws Exception{
    3. return "HelloWorld ,Spring Boot!";
    4. }

    }

  2. @Controller
    它用于标注控制层,在MVC开发模式中代表C(控制器)。有时候等效于@RestController

    @Controller
    public class HelloWorldController {
    @RequestMapping(“/helloWordB”)
    @ResponseBody

    1. public String hello() throws Exception{
    2. return "HelloWorld ,Spring Boot!";
    3. }

    }

3.@Service
它用于声明一个业务处理类(实现一个非接口类),用于标注服务层,处理业务逻辑。

  1. @Service
  2. public class ArticleServiceImpl implements ArticleService {
  3. @Autowired
  4. private ArticleResonsitory articleResonsitory;
  5. /**
  6. * Description:重写service接口的实现,实现根据id查询对象功能
  7. * @param id
  8. * @return
  9. */
  10. @Override
  11. public Article findArticleById(long id){
  12. return articleResonsitory.findById(id);
  13. }
  14. }
  1. @Repository
    它用于标注数据访问层
  2. == @Component==
    它用于普通的POJO(Plain Ordinary Java Objects,简单的java对象)实例化到Spring容器中。当类不属于注解@Controller和@Service等时,就可以使用注解@Component来标注这个类。他可配合CommandLineRunner使用,以便在程序启动后执行一些基础任务
    注:Spring 会把注解@Controller , @Service, @Reponsitory, @Component标注的类纳入Spring容器中进行管理。
  3. @Configuration
    它用于标注配置类,并且可以由Spring容器自动处理。他作为Bean的载体,用来指示一个类声明,一个或多个@Bean方法,在运行时为这些Bean生成BeanDefinition和服务请求。
  4. @Resource
    @Autowired与@Resource都可以用来装载Bean,也都可以写在字段上或Setter方法上。

    public class ArticleController {

    1. @Resource
    2. private ArticleResonsitory articleResonsitory;
    3. @PostMapping("")
    4. public String saveArticle(Artice model){
    5. articleResonsitory.save(model);
    6. return "redirect:/article";
    7. }

    }

  5. @Autowired
    它表示被修饰的类需要注入对象。Spring会扫描所有被@Autowired标注的类,然后根据类型在IoC容器中找到匹配的类进行注入。被@Autowired注解后的类不需要在导入文件。

  6. @RequestMapping
    它用来处理请求地址的映射,用在类或方法上。如果用在类上,则表示类中的所有响应请求的方法都是以该地址作为父路径的。该注解有6个属性。

    • Params:指定Request中必须包含某些参数值,才让该方法处理。
    • Headers:指定Request中必须包含某些指定的header值,才能让该方法处理请求。
    • Value: 指定请求的实际地址,指定的地址可以是URI Template模式。
    • Method:指定请求的Method类型,如:GET、POST、PUT、DELETE等
    • Consumes:指定处理请求的提交内容类型Content-Type,如“application/json,text/html”。
    • Produces: 指定返回的内容类型。只有当Request请求头中的Accept类型中包含指定类型时才返回。
  7. @Transactional
    它可以用在接口、接口方法、类以及类方法上。
    但Spring不建议在接口或者接口方法上使用该注解,因为该注解只有在使用基于接口的代理时才会生效。如果异常被捕获(try{ } catch{ })了,则事物就不回滚了,如果想让事物回滚,则必须在往外抛出异常(try{ } catch{ throw Exception})

  8. @Qualifer
    它的意思是“合格者”,用于标注哪一个实现类才是需要注入的。需要注意的是,@Qualifier的参数名称为被注入的类中的注解@Service标注的名称。
    @Qualifier常和@Autowired一起使用

    @Autowired
    @Qualifier(“articleService”)

而@Resource和它不同,@Resource自带name属性

使用在方法上的注解

在这里插入图片描述

  1. @RequestBody
    它常用来处理JSON/XML格式的数据。通过@RequestBody可以将请求体中的(JSON/XML)字符串绑定到相应的Bean上,也可以将其分别绑定到对应的字符串上。
    例:用AJAX(前端)提交数据,然后在控制器(后端)接受数据。

    $.ajax({

    1. url:'/post',
    2. type:'POST',
    3. data:'{"name":"fyh"}',
    4. contentType:"application/json charset=utf-8",
    5. success:function(data){
    6. alert("request success");
    7. }

    });

在控制器中接受数据的代码如下:

  1. @requestMapping(“/post”)
  2. Public void post(@requestBody String name){
  3. //省略
  4. }
  1. @PathVariable
    用于获取路径中的参数
  2. @Bean
    它代表产生一个Bean,并交给Spring管理用于封装数据,一般有Setter、Getter方法。在MVC模型中,对应的是M(模型)
  3. @ResponseBody
    它的作用是通过转换器将控制器中方法返回的对象转换为指定格式,然后写入Response对象的body区。它常用来返回JSON/XML格式的数据。
    使用此注解后,数据直接写入输入流中,不需要进行视图渲染

    @GetMapping(“/test”)
    @ResponseBody
    public String showForm(User user) {

    1. return "test";

    }

其他注解

在这里插入图片描述


选自 《Spring Boot 实战派》

发表评论

表情:
评论列表 (有 0 条评论,449人围观)

还没有评论,来说两句吧...

相关阅读