以下是Spring Boot中常用的注解及其详细解释以及相应的代码示例:
@SpringBootApplication: 这个注解用于标识一个Spring Boot应用的主类。它整合了 @Configuration,@EnableAutoConfiguration 和 @ComponentScan。
- @SpringBootApplication
- public class DemoApplication {
- public static void main(String[] args) {
- SpringApplication.run(DemoApplication.class, args);
- }
- }
@RestController: 这个注解用于定义一个RESTful控制器,在Spring MVC中它表示所有的处理方法都返回一个Restful风格的数据。
- @RestController
- public class HelloController {
- @GetMapping("/hello")
- public String hello() {
- return "Hello, World!";
- }
- }
@Service: 这个注解用于标识一个类是业务逻辑层的组件。
- @Service
- public class UserService {
- // Service logic here
- }
@Repository: 这个注解用于标识一个类是数据访问层的组件。
- @Repository
- public class UserRepository {
- // Data access logic here
- }
@Component: 这个注解用于标识一个类是Spring的组件。
- @Component
- public class MyComponent {
- // Component logic here
- }
@Autowired: 这个注解用于自动装配Spring Bean。
- @Service
- public class UserService {
- @Autowired
- private UserRepository userRepository;
-
- // Service logic here
- }
@Qualifier: 当多个实现类满足一个接口时,可以与 @Autowired 配合使用以指定具体要注入的Bean。
- @Service
- public class UserService {
- @Autowired
- @Qualifier("userDatabaseRepository")
- private UserRepository userRepository;
-
- // Service logic here
- }
@RequestMapping: 这个注解用于将HTTP请求映射到处理方法上。
- @RestController
- @RequestMapping("/api")
- public class MyController {
- @GetMapping("/hello")
- public String hello() {
- return "Hello, World!";
- }
- }
@GetMapping, @PostMapping, @PutMapping, @DeleteMapping: 这些注解用于将HTTP GET、POST、PUT、DELETE 请求映射到处理方法上。
- @RestController
- @RequestMapping("/api")
- public class MyController {
- @GetMapping("/get")
- public String get() {
- return "GET Request";
- }
-
- @PostMapping("/post")
- public String post() {
- return "POST Request";
- }
-
- @PutMapping("/put")
- public String put() {
- return "PUT Request";
- }
-
- @DeleteMapping("/delete")
- public String delete() {
- return "DELETE Request";
- }
- }
@RequestParam: 这个注解用于从请求中获取参数的值。
- @GetMapping("/user")
- public String getUserById(@RequestParam Long id) {
- // logic to fetch user by id
- }
@PathVariable: 这个注解用于从请求的URL中获取参数的值。
- @GetMapping("/user/{id}")
- public String getUserById(@PathVariable Long id) {
- // logic to fetch user by id
- }
@ResponseBody: 这个注解用于将方法返回的对象转换为HTTP响应的主体部分。
- @GetMapping("/user")
- @ResponseBody
- public User getUser() {
- // logic to fetch user
- return user;
- }
@RequestBody: 这个注解用于将HTTP请求的主体部分转换为方法参数。
- @PostMapping("/user")
- public String addUser(@RequestBody User user) {
- // logic to add user
- }
@ResponseStatus: 这个注解用于指定方法返回的HTTP状态码。
- @ResponseStatus(HttpStatus.NOT_FOUND)
- public class ResourceNotFoundException extends RuntimeException {
- // Exception handling logic here
- }
@ExceptionHandler: 这个注解用于定义全局异常处理方法。
- @ControllerAdvice
- public class GlobalExceptionHandler {
- @ExceptionHandler(Exception.class)
- public String handleException(Exception ex) {
- // Exception handling logic here
- return "error";
- }
- }
@Configuration: 这个注解用于定义配置类,通常与 @Bean 注解一起使用。
- @Configuration
- public class AppConfig {
- @Bean
- public UserService userService() {
- return new UserService();
- }
- }
@Value: 这个注解用于从配置文件中获取值。
- @Component
- public class MyComponent {
- @Value("${my.property}")
- private String myProperty;
-
- // Component logic here
- }
以上是一些常见的Spring Boot注解及其用法示例。在实际开发中,可能还会使用到其他的注解,具体根据项目需求和设计选择。
原文地址:Spring Boot 常用注解大全