Spring Boot注解总结大全【案例详解,一眼秒懂】

  1. @SpringBootApplication
  • 功能 :这是Spring Boot应用的核心注解,它是一个组合注解,实际上相当于同时使用了 @Configuration@EnableAutoConfiguration@ComponentScan 。它标记在主应用类上,用于开启Spring Boot的自动配置功能,扫描组件并加载应用程序上下文。

  • 示例

import org.springframework.boot.SpringBootApplication;

@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringBootApplication.run(MyApplication.class, args);
}
}

  • 在这个示例中, MyApplication 是主应用类, @SpringBootApplication 注解告诉Spring Boot这是一个应用的入口点,并且要开启自动配置、扫描组件等操作。
  1. @RestController
  • 功能 :是 @Controller@ResponseBody 的组合注解。 @Controller 表示这个类是一个Spring MVC控制器,用于处理Web请求; @ResponseBody 表示方法的返回值直接作为HTTP响应体返回,而不是解析为视图名称,通常用于构建RESTful风格的Web服务。

  • 示例

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
@GetMapping(“/hello”)
public String sayHello() {
return “Hello, World!”;
}
}

  • 这里的 HelloController 类被 @RestController 注解标记, sayHello 方法处理 /hello 路径的 GET 请求,并直接返回一个字符串作为响应内容。
  1. @GetMapping、@PostMapping、@PutMapping、@DeleteMapping等
  • 功能 :这些注解是Spring MVC提供的用于简化HTTP请求方法映射的注解。 @GetMapping 用于处理 GET 请求, @PostMapping 用于处理 POST 请求, @PutMapping 用于处理 PUT 请求, @DeleteMapping 用于处理 DELETE 请求。它们都可以指定请求路径作为参数。

  • 示例

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {
@GetMapping(“/users”)
public String getUsers() {
// 返回用户列表的逻辑
return “List of users”;
}

@PostMapping("/users")
public String addUser() {
    // 添加用户的逻辑
    return "User added";
}

@PutMapping("/users/{id}")
public String updateUser() {
    // 更新用户的逻辑
    return "User updated";
}

@DeleteMapping("/users/{id}")
public String deleteUser() {
    // 删除用户的逻辑
    return "User deleted";
}

}

  • 这个 UserController 类展示了如何使用这些注解来处理不同类型的HTTP请求,用于对用户资源进行CRUD(创建、读取、更新、删除)操作。
  1. @RequestMapping
  • 功能 :这是一个比较通用的请求映射注解,可以用于处理多种HTTP请求方法。它可以指定请求路径、请求方法、请求头、请求参数等条件来匹配请求。 @GetMapping 等注解实际上是 @RequestMapping 的简化形式。

  • 示例

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(“/books”)
public class BookController {
@RequestMapping(value = “/{id}”, method = RequestMethod.GET)
public String getBookById() {
// 根据ID获取图书的逻辑
return “Book details”;
}
}

  • 这里 BookController 类使用 @RequestMapping 注解指定了基础路径为 /booksgetBookById 方法使用 @RequestMapping 再次指定了请求路径为 /{id} 并且请求方法为 GET ,用于获取指定ID的图书信息。
  1. @Autowired
  • 功能 :用于自动装配Spring容器中的Bean。当Spring容器中有一个类型匹配的Bean时,它会自动将这个Bean注入到被 @Autowired 注解标记的字段、方法参数或构造函数参数中。

  • 示例

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {
private final UserRepository userRepository;

@Autowired
public UserService(UserRepository userRepository) {
    this.userRepository = userRepository;
}
// 其他业务逻辑

}

  • 在这个 UserService 类中,通过构造函数注入了 UserRepository@Autowired 注解告诉Spring容器自动找到 UserRepository 类型的Bean并注入到构造函数中。
  1. @Service、@Component、@Repository、@Controller(前面已提及部分功能)
  • 功能 :这些注解都是用于标记Spring中的组件,使得Spring容器能够扫描并管理这些组件。

 -  `@Service` :用于标记业务逻辑层的组件,表明这个类是一个服务类,主要用于处理业务逻辑。
 -  `@Component` :是一个通用的组件注解,用于标记任何Spring管理的组件。如果一个类不符合 `@Service` 、 `@Repository` 或 `@Controller` 的语义,但又需要被Spring容器管理,就可以使用 `@Component` 。
 -  `@Repository` :用于标记数据访问层(如数据库访问)的组件,它还具有一些额外的功能,比如在数据访问出现异常时会自动转换为Spring的 `DataAccessException` 体系的异常。
  • 示例

import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
// 业务逻辑实现
}

import org.springframework.stereotype.Component;

@Component
public class MyComponent {
// 组件功能实现
}

import org.springframework.stereotype.Repository;

@Repository
public class UserRepositoryImpl implements UserRepository {
// 数据访问逻辑实现
}

  1. @Configuration
  • 功能 :用于标记一个类作为配置类,在这个类中可以定义Bean、导入其他配置类、设置属性源等。它相当于一个XML配置文件的Java版,用于配置Spring容器中的Bean和其他相关设置。

  • 示例

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AppConfig {
@Bean
public MyBean myBean() {
return new MyBean();
}
}

  • 在这个 AppConfig 配置类中, @Bean 注解用于定义一个Bean,方法名( myBean )默认作为Bean的名称,方法的返回值就是Bean的实例。
  1. @Value
  • 功能 :用于将外部配置文件(如 application.propertiesapplication.yml )中的属性值注入到Spring管理的组件中。可以用于注入简单类型的值,如字符串、数字等。

  • 示例

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class MyComponent {
@Value(“${my.property}”)
private String myProperty;
// 其他逻辑
}

  • 假设在 application.properties 文件中有 my.property=Hello ,那么 myProperty 字段就会被注入值 "Hello"
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值