在使用SpringBoot开发中或者在求职面试中都会使用到很多注解或者问到注解相关的知识。本文主要对一些常用的注解进行了总结,同时也会举出具体例子,供大家学习和参考。
解释:
@SpringBootApplication
public class MyApplication {
public static void mAIn(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
解释:
@Configuration
public class MyConfig {
@Bean
public MyBean myBean() {
return new MyBean();
}
}
解释:
@SpringBootApplication
@EnableAutoConfiguration
public class MyApplication {
// ...
}
解释:
@SpringBootApplication
@ComponentScan("com.example")
public class MyApplication {
// ...
}
解释:
@RestController
public class UserController {
@GetMapping("/users")
public List<User> getAllUsers() {
// 返回所有用户列表的逻辑
}
}
解释:
@RestController
@RequestMapping("/users")
public class UserController {
@GetMapping("/{id}")
public User getUserById(@PathVariable("id") Long id) {
// 根据id获取用户的逻辑
}
}
@GetMapping、@PostMapping、@DeleteMapping、@PutMapping
解释:
@RestController
@RequestMapping("/users")
public class UserController {
@GetMapping("/{id}")
public User getUserById(@PathVariable("id") Long id) {
// 根据id获取用户的逻辑
}
}
@RestController
@RequestMapping("/users")
public class UserController {
@PostMapping
public User createUser(@RequestBody User user) {
// 创建用户的逻辑
}
}
@RestController
@RequestMapping("/users")
public class UserController {
@DeleteMapping("/{id}")
public void deleteUser(@PathVariable("id") Long id) {
// 删除用户的逻辑
}
}
@RestController
@RequestMapping("/users")
public class UserController {
@PutMapping("/{id}")
public User updateUser(@PathVariable("id") Long id, @RequestBody User user) {
// 更新用户信息的逻辑
}
}
解释:
@RestController
@RequestMapping("/users")
public class UserController {
@GetMapping
public List<User> getUsersByPage(@RequestParam("page") int page,
@RequestParam(value = "size", defaultValue = "10") int size) {
// 分页查询用户列表的逻辑
}
}
解释:
@RestController
@RequestMapping("/users")
public class UserController {
@GetMapping("/{id}")
public User getUserById(@PathVariable("id") Long id) {
// 根据id获取用户的逻辑
}
}
解释:
@RestController
@RequestMapping("/users")
public class UserController {
@PostMapping
public User createUser(@RequestBody User user) {
// 创建用户的逻辑
}
}
解释:
@RestController
@RequestMapping("/users")
public class UserController {
@PostMapping
public ResponseEntity<User> createUser(@Valid @RequestBody User user) {
// 创建用户的逻辑
return ResponseEntity.status(HttpStatus.CREATED).body(user);
}
}
Bean管理相关
解释:
@SpringBootApplication
@EnableScheduling
public class MyApp {
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args);
}
}
@Component
public class MyScheduler {
@Scheduled(fixedRate = 5000)
public void myTask() {
// 每个5秒执行一次的任务逻辑
}
}
@Component
public class MyScheduler {
@Scheduled(cron = "0 0 8 * * ?")
public void myTask() {
// 每天早上8点执行的任务逻辑
}
}
解释:
@SpringBootApplication
@EnableAsync
public class MyApp {
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args);
}
}
@Service
public class MyService {
@Async
public CompletableFuture<String> asyncTask() {
// 异步执行的任务逻辑
// 返回一个CompletableFuture以支持异步结果的处理
}
}
缓存和事务管理
解释:
@SpringBootApplication
@EnableCaching
@EnableTransactionManagement
public class MyApp {
public static void main(String[] args) {
SpringApplication.run(MyApp.class, args);
}
}
@Service
public class MyService {
@Transactional
public void performTransaction() {
// 执行数据库操作等需要事务管理的逻辑
}
}
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(Exception.class)
public ResponseEntity<String> handleException(Exception e) {
// 异常处理逻辑,可以根据实际需求进行对异常的处理和返回相应的错误信息
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Internal Server Error");
}
}
有关于SpringBoot常用注解到此就结束了。感谢大家的阅读,希望大家对此部分内容散发讨论或者还有其他注解可以在评论区进行补充,便于学到更多的知识。感激不尽!