Spring Boot 注解的使用

 Spring Boot 优于Spring mvc ,SSM,SSH 的一个亮点就是他使用了好多的注解。

1. @Autowired 

这个注解的作用是将其他的类,接口引入,类似于之前的类的初始化等,用这个注解,类中或接口的方法就可以直接调用了。

这个注解和@Inject,@Resource 作用类似,都能注入类, 接口,但是区别我就不知道了。

2. @RestController

 这个注解的作用是告诉Servlet 这个类是一个控制器,当前台调用后台的时候,根据名称就能找到这个控制类,然后去执行里面的方法。他类似于Spring mvc 中的@Controller,他继承自@Controller。

3. @RequestMapping 和他的衍生品 @GetMapping,@PostMapping,@PutMapping,@DeleteMapping,@PutMapping

这个注解的作用是当前台界面调用Controller处理数据时候告诉控制器怎么操作。get 对应查询,put 对应更新,post 对应增加, delete 对应删除。

4. @RequestParam,@PathParm,@PathVariable和@@RequestBody

这四个注解都是用来传参数的,第一个是用来传递http://localhost:8080/page1?id=1 这种用的。第二个和第三个用来处理http://localhost:8080/page1/1这种传参数的,后面这个是用来传对象用的。

  • @GetMapping = @RequestMapping(method = RequestMethod.GET)
  • @PostMapping = @RequestMapping(method = RequestMethod.POST)
  • @PutMapping = @RequestMapping(method = RequestMethod.PUT)
  • @DeleteMapping = @RequestMapping(method = RequestMethod.DELETE)

猜你喜欢

转载自blog.csdn.net/qq_35514348/article/details/80655141