美文网首页
SpringBoot开发必须知道的18个注解

SpringBoot开发必须知道的18个注解

作者: JerrysCode | 来源:发表于2023-08-12 09:10 被阅读0次

    使用SpringBoot时,我们使用注解来组装Bean,如下时最常用的TopN注解

    Component

    @Component用于注解一个类, 表示这个类是一个Bean
    @Component是一个最通过的标注,Spring还提供了更具体的标注,开发中优先使用这些具体标记

    • @Controller:SpringMVC中的Controller
    • @RestController: restful应用中的Controller
    • @Service:服务层的Component, 用于业务逻辑处理
    • @Repository:数据访问层Component, 用于访问数据源

    Controller, RestController的区别
    Controller把数据交给view resolver处理,而RestController直接(转为json)返回给前端

    @ComponentScan

    用@Component标记类后,还要通过@ComponentScan告诉Spring去扫描这些Bean。
    这段代码告诉Spring框架到com.jerry.foo和com.tom.otherfoo下扫描Bean

    @ComponentScan(basePackages = {"com.jerry.foo", "com.tom.otherfoo"})
    public class MyConfiguration {
        // ...
    }
    

    SpringBoot提供了默认查找Bean的机制,会在Application.java启动类所在的pakcage下查找Bean,所以有时候我们不加@ComponentScan 也能找到Bean

    @Autowired

    告诉Spring容器自动注入依赖的Bean

    @Component
    public class Car {
        @Autowired
        private Engine bar;
    }
    

    @Configuration + @Bean

    除了使用@Component + @Component标记实现Bean的定义和注入。我们还有另外的选择,那就是@Configuration
    @Configuration标记的类为配置类,在配置类里可以定义Bean
    这段代码表示MyConfiguration是配置类,在配置类里用@Bean定义了一个Car类型的Bean

    @Configuration
    public class MyConfiguration {
        @Bean
        public Car car() {
            return new Car(); 
        }
    }
    

    @RequestMapping

    一般标记在方法上,也可以标记在类上面,用于处理http请求
    这段代码表示 GET /books这个请求由findAll()方法处理

    @Controller
    public class FooController {
        @RequestMapping(value = "/books", method = RequestMethod.GET)
        public List<Book> findAll() {
            // ... return all foos in the application ... 
        }
    }
    

    @RequestMapping是一个通过的注解,可以使用更具体的标注替代它

    • @GetMapping:标记http GET请求
    • @PostMapping:标记http POST请求
    • @PutMapping:标记http PUT请求
    • @DeleteMapping:标记http Delete请求

    @PathVariable

    从请求url路径中获取参数
    假如请求url为 http://localhost:8080/car/100, 如下代码会把100赋给方法参数id

    @Controller
    public class MyController {
        @GetMapping("/car/{id}")
        public void getCarById(@PathVariable("id") String id) {
            //get car object by id
        }
    }
    

    @RequestParam

    标记请求查询参数
    例如:请求url为 http://localhost:8080/car?id=100, 如下的代码会把100赋给方法参数id

    @Controller
    public class MyController {
        @GetMapping("/car")
        public void getCarById(@RequestParam("id") String id) {
            //get car object by id
        }
    }
    

    @RequestBody

    标记从request body中获取请求参数,如下代码会把request body中的数据转为Car对象

    @Controller
    public class MyController {
        @PostMapping("/car")
        public void create(@RequestBody Car car) {
            // Create a car resource
        }
    }
    

    @RestControlAdvice + @ExceptionHandler

    结合这两个注解实现全局异常处理, 当发生错误时,业务代码只需要抛出异常。@RestControllerAdvice标记的类会捕获异常,并交给 @ExceptionHandler标记的方法处理。

    @RestControllerAdvice
    public class GlobalExceptionHandler
    {
        @ExceptionHandler(Exception.class)
        public AjaxResult handleException(Exception e)
        {
            //process Exception
        }
    
        @ExceptionHandler(MyException.class)
        public AjaxResult handleException(MyException e)
        {
            //process MyException
        }
    }
    

    熟练使用以上18个注解就可以满足日常大部分工作啦。如果对你有帮助就点个赞吧。

    相关文章

      网友评论

          本文标题:SpringBoot开发必须知道的18个注解

          本文链接:https://www.haomeiwen.com/subject/kodxmdtx.html