springboot有关的注解……

在使用 Spring Boot 进行开发时,有许多常用注解帮助我们快速构建功能模块。下面按照不同的功能类别,列出 Spring Boot 最常用的注解,并简要说明其作用。


一、启动类相关

注解 说明
@SpringBootApplication 复合注解,包含 @Configuration@EnableAutoConfiguration@ComponentScan,是 Spring Boot 启动类的核心注解。
@EnableAutoConfiguration 开启 Spring Boot 自动配置功能,通常不单独使用。
@ComponentScan 自动扫描并注册被注解的组件(如 @Component、@Service 等)。
@Configuration 表示该类是一个配置类,等价于配置文件。

二、Spring 组件注解(自动装配)

注解 说明
@Component 标记为 Spring 的组件,纳入容器管理。
@Service 表示服务层组件,实质上是 @Component 的派生。
@Repository 表示数据访问层组件,可结合异常翻译。
@Controller 表示控制器类,用于处理前端请求。
@RestController 等价于 @Controller + @ResponseBody,用于构建 RESTful API。
@Autowired 自动注入 Spring 容器中的 Bean。
@Qualifier 配合 @Autowired,指定注入 Bean 的名称。
@Value 注入配置文件中的值,如 ${server.port}

三、Web 开发相关

注解 说明
@RequestMapping 用于映射请求路径,可作用在类或方法上。
@GetMapping @PostMapping @PutMapping @DeleteMapping 请求方法专属注解,是 @RequestMapping 的快捷方式。
@RequestParam 获取请求参数,如 ?name=xxx
@PathVariable 获取路径变量,如 /user/{id}
@RequestBody 接收 JSON 请求体。
@ResponseBody 返回 JSON 数据给前端(常用于 Ajax)。

四、数据持久层相关(Spring Data JPA / MyBatis)

注解 说明
@Entity 表示一个实体类。
@Table 指定数据库对应表名。
@Id 主键标识。
@GeneratedValue 主键生成策略。
@Column 指定字段属性映射。
@Mapper(MyBatis) 用于 Mapper 接口,告知 Spring 管理这个接口。
@Select@Insert(MyBatis 注解方式) 编写 SQL 的注解方式。

五、事务与异常处理

注解 说明
@Transactional 声明方法或类需要事务支持。
@ControllerAdvice 全局异常处理类,搭配 @ExceptionHandler 使用。
@ExceptionHandler 处理指定类型的异常。

六、测试相关

注解 说明
@SpringBootTest 用于 Spring Boot 项目的集成测试。
@RunWith(SpringRunner.class) 启动 Spring 测试环境。
@WebMvcTest 只测试 Controller 层。
@DataJpaTest 只测试 JPA Repository。

七、配置与环境

注解 说明
@ConfigurationProperties 批量绑定配置文件的属性。
@PropertySource 指定外部配置文件的路径。
@Profile 指定该类或 Bean 在某个环境(如 dev、prod)中生效。

你可能感兴趣的:(springboot,spring,boot,java)