整理的springboot的常用注解

时间:2019-01-23
本文章向大家介绍整理的springboot的常用注解,主要包括整理的springboot的常用注解使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

注解(annotations)

  • @SpringBootApplication
    这是springboot项目的基本注解,包含了@ComponentScan、@Configuration和@EnableAutoConfiguration注解。@ComponentScan让spring Boot扫描到Configuration类并把它加入到springApplication上下文。
  • @Configuration
    声明这是一个配置类,相当于spring的XML配置
  • @EnableAutoConfiguration
    开启自动配置
  • @ComponentScan
    组件扫描,自动扫描装载bean
  • @Component
    1、泛指组件,当组件不好归类,使用这个声明一个组件 2、也可类实现CommandLineRunner,完成springboot启动加载某些数据
  • @RestController
    这是@Controller和@ResponseBody的合集,表示这是个控制器,并将方法返回的数据转为json
  • @Autowired
    自动导入
  • @PathVariable
    获取restful风格连接中的参数user/get/{id} (@PathVariable int id)
  • @RequestBody
    接收请求参数,请求参数为json格式
  • @Value
    注入Spring boot application.properties配置的属性的值(@Value(value = “#{userid}”) )
  • @Bean
    相当于XML中的,放在方法的上面,而不是类,意思是产生一个bean,并交给spring管理。
  • @RequestMapping
    springmvc注解,处理请求映射的地址
  • @ControllerAdvice
    增强controller,可以用来处理全局异常
  • @ExceptionHandler(Exception.class)
    用在方法上面表示遇到这个异常就执行以下方法