本文共 2238 字,大约阅读时间需要 7 分钟。
@SpringBootApplication注解是Spring Boot应用开发的标准注解,主要目的是快速初始化Spring Boot应用。其核心三个注解包括:
@EnableAutoConfiguration
@SpringBootApplication
@ComponentScan
注:默认情况下,@SpringBootApplication注解会自动包含@EnableAutoConfiguration,因此通常不需要手动添加@EnableAutoConfiguration注解。
实体类注解主要用于定义数据库表对应的类,下面是几个常见注解及其作用:
@Entity(name="bean")
@Table(name="table_name")
@Id
@GeneratedValue
控制层注解用于定义Spring Boot应用的后端控制逻辑,以处理HTTP请求。常用注解包括:
@RestController
@RequestMapping(method = RequestMethod.GET/POST/...)
@GetMapping
@PostMapping
@RequestMapping
在Spring Boot中,还有一些常用的注解,主要用于处理HTTP请求参数和数据格式。
@PathVariable("id") Integer userId
。@GetMapping("/user/{id}")public String getUser(@PathVariable("id") Integer userId) { // 方法逻辑...}```
@PostMapping("/user")public User saveUser(@RequestBody User user) { // 方法逻辑...}```
application/x-www-form-urlencoded
格式。@PostMapping("/users")public ListgetUsers(@RequestParam(name = "name") String name) { // 方法逻辑...}```
@RestController与@Controller
@Target与@Enable
@EnableWebMvc
。有时候需要自定义注解,可以通过@Target
和@Inherited
等注解实现:
@Target(ElementType.METHOD)
@Target(ElementType.METHOD)@Inherited(ElementType.METHOD)public @interface MyAnnotation { String value();}
@Inherited
转载地址:http://yuwqz.baihongyu.com/