Spring注解这么写,团队协作效率至少提升1倍

463 阅读1分钟

@SpringBootApplication

包含@Configuration、@EnableAutoConfiguration、@ComponentScan,通常放在主类上。

@Controller

标志是一个控制器类,需要与@RequestMapping配合使用。通常做前后端分离的项目用这个注解比较少,因为后端只返回json数据结构,而不是页面。

@RestController

该注解是@Controller和@ResponseBody的组合注解

普通风格Rest风格
@RequestMapping(value=“”,method = RequestMethod.GET)@GetMapping(value =“”)
@RequestMapping(value=“”,method = RequestMethod.POST)@PostMapping(value =“”)
@RequestMapping(value=“”,method = RequestMethod.PUT)@PutMapping(value =“”)
@RequestMapping(value=“”,method = RequestMethod.DELETE)@DeleteMapping(value =“”)

@Service

标注该类是一个接口服务

@Autowired

当我需要从bean工程获取一个ben时,spring就自动为我们装配该标识为@Autowired的元素

@Resource

与Autowired获取相同的效果,不过该注解并不是spring提供的,而是j2ee提供的。两者的差别是Autowired通过类型匹配,而Resource通过名称匹配

@Configuration

配置文件注解

@Value

读取yaml文件的注解,并可以设置默认值

@Repository

jpa的dao注解,通过标注了该注解的类可以操作数据库

@RequestMap

类注解,标明访问该接口的请求地址的开头应该是什么路径

@GetMapping

get请求的缩写,上表格有说明

@PostMapping

post请求的缩写,上表格有说明

@PathVariable

路径分隔符变量,用于前端通过地址通配符给后端传递参数

@RequestBody、@RequestParam

参数注解,通过在参数上标注该注解,前端通过?后面传递参数

@EnableConfigurationProperties

启用配置文件的注解,读取yaml文件的内容到自定义的bean中

@Component

组件注解

@ConfigurationProperties

与EnableConfigurationProperties搭配使用,标注自定义对象中的字段是从yaml文件中的哪个接口开始匹配

更多原创阅读:javawu.com