SpringBoot常用注解整理
Spring Boot 是一个基于 Spring 框架的快速开发框架,它简化了 Spring 应用的配置和开发过程。在 Spring Boot 开发过程中,使用了许多注解来帮助开发者快速进行配置和开发。以下是一些常用的 Spring Boot 注解及其作用:
核心注解
-
@SpringBootApplication: 标记主程序类,包含 @Configuration、@EnableAutoConfiguration 和 @ComponentScan 的功能。
-
@Configuration: 标识一个类为配置类,替代 XML 配置文件。
-
@ComponentScan: 自动扫描并注册 Spring 组件。
-
@EnableAutoConfiguration: 启用 Spring Boot 的自动配置机制。
@SpringBootApplication public class StudentManagementApplication { public static void main(String[] args) { SpringApplication.run(StudentManagementApplication.class, args); } }
1. 控制层(Controller 层)
控制层负责处理客户端的请求并返回响应。在Spring Boot中,控制层通常使用以下注解:
-
@RestController: 声明为RESTful风格的控制器,会将方法的返回值直接作为响应内容返回给客户端。
-
@RequestMapping, @GetMapping, @PostMapping, @PutMapping, @DeleteMapping: 用于映射HTTP请求到特定的处理方法。
-
@RequestParam: 用于将请求中的参数绑定到方法的参数上。
-
@PathVariable: 用于将请求URL中的变量绑定到方法的参数上。
-
其他如 @RequestBody、@ResponseBody 等,用于处理请求体和响应体。
@RestController @RequestMapping("/api/students") public class StudentController { @Autowired private StudentService studentService; @GetMapping public List<Student> getAllStudents() { return studentService.getAllStudents(); } @GetMapping("/{id}") public ResponseEntity<Student> getStudentById(@PathVariable Long id) { Student student = studentService.getStudentById(id); if (student != null) { return ResponseEntity.ok(student); } else { return ResponseEntity.notFound().build(); } } @PostMapping public ResponseEntity<Student> addStudent(@RequestBody Student student) { Student createdStudent = studentService.addStudent(student); return ResponseEntity.status(HttpStatus.CREATED).body(createdStudent); } @PutMapping("/{id}") public ResponseEntity<Student> updateStudent(@PathVariable Long id, @RequestBody Student student) { Student updatedStudent = studentService.updateStudent(id, student); if (updatedStudent != null) { return ResponseEntity.ok(updatedStudent); } else { return ResponseEntity.notFound().build(); } } @DeleteMapping("/{id}") public ResponseEntity<Void> deleteStudent(@PathVariable Long id) { boolean deleted = studentService.deleteStudent(id); if (deleted) { return ResponseEntity.noContent().build(); } else { return ResponseEntity.notFound().build(); } } }
2. 服务层(Service 层)
服务层包含应用程序的业务逻辑,负责处理复杂的业务操作。在Spring Boot中,服务层通常使用以下注解:
@Service: 声明为服务层组件。
@Autowired: 自动装配其他组件,如DAO层的实现。
可以使用任意的自定义注解来标识特定的服务功能,例如事务管理的 @Transactional。
@Service
public class StudentService {
private List<Student> students = new ArrayList<>();
public List<Student> getAllStudents() {
return students;
}
public Student getStudentById(Long id) {
return students.stream()
.filter(student -> student.getId().equals(id))
.findFirst()
.orElse(null);
}
public Student addStudent(Student student) {
student.setId((long) (students.size() + 1)); // Simulate auto-increment id
students.add(student);
return student;
}
public Student updateStudent(Long id, Student updatedStudent) {
for (int i = 0; i < students.size(); i++) {
Student student = students.get(i);
if (student.getId().equals(id)) {
updatedStudent.setId(id);
students.set(i, updatedStudent);
return updatedStudent;
}
}
return null;
}
public boolean deleteStudent(Long id) {
return students.removeIf(student -> student.getId().equals(id));
}
}
3. 数据访问层(DAO 层)
数据访问层负责与数据库或其他数据存储进行交互。在Spring Boot中,数据访问层通常使用以下注解:
- @Repository: 声明为数据访问层组件。
- @Autowired: 自动装配数据源、Hibernate的SessionFactory等。
- 可以使用 @Transactional 来实现数据库事务管理。
4. 配置层
配置层包含了应用程序的配置信息,如数据库连接、第三方服务配置等。在Spring Boot中,配置层通常使用以下注解:
- @Configuration: 声明为配置类。
- @Bean: 在配置类中使用,将方法返回的对象注册为一个Bean。
- @Value: 用于注入配置文件中的属性值。
- @Profile: 根据不同的环境配置不同的Bean或配置信息。