必知的 Spring Boot 常用注解:一文读懂原理与实战

Spring Boot 中有许多常用的注解,这些注解用于配置、管理和定义 Spring Boot 应用程序的各个方面。以下是这些注解按大类和小类的方式分类,并附有解释和示例。

一、Spring Boot 核心注解

@SpringBootApplication  

解释:这是一个组合注解,通常用于主应用程序类,标志着这是 Spring Boot 应用程序的入口点。它包含了其他注解,如@Configuration、@ComponentScan 和@EnableAutoConfiguration。  

示例:

1@SpringBootApplication 2public class MyApplication { 3 public static void main(String[] args) { 4 SpringApplication.run(MyApplication.class, args); 5 } 6}

@Configuration

  1. 解释:标志着一个类作为配置类,它通常用于定义 Bean。
  2. 示例:
1@Configuration 2public class MyConfig { 3 @Beanpublic MyBean myBean() { 4 return new MyBean(); 5 } 6}

@ComponentScan  

解释:用于指定 Spring 容器扫描组件的基本包路径。  

示例:

1 @SpringBootApplication 2 @ComponentScan(basePackages = "com.example") 3 public class MyApplication { 4 public static void main(String[] args) { 5 SpringApplication.run(MyApplication.class, args); 6 } 7}

二、Spring Boot Web 注解

@Controller

  1. 解释:标志着一个类是 Spring MVC 控制器,处理 HTTP 请求。
  2. 示例:
1@Controller 2public class MyController { 3 // Controller methods here 4}

@RestController  

解释:结合@Controller 和@ResponseBody,用于创建 RESTful 风格的控制器。  示例:

1@RestController 2public class MyRestController { 3 @GetMapping("/hello") 4 public String hello() { 5 return "Hello, World!"; 6 } 7}

@RequestMapping  

解释:用于映射 HTTP 请求到控制器方法,并指定 URL 路径。  

示例:

1@Controller 2@RequestMapping("/my") 3public class MyController { 4 @GetMapping("/hello") 5 public String hello() { 6 return "Hello, World!"; 7 } 8}

三、Spring Boot Bean 注解

@Component

解释:标志着一个类是 Spring 的组件,会被 Spring 扫描并注册为一个 Bean。

示例:

1@Component 2public class MyComponent { 3 // Component logic here 4}

@Service

解释:

用于标记一个类作为业务逻辑的服务组件。

示例:

1@Service 2public class MyService { 3 // Service logic here 4}

@Repository

解释:

用于标记一个类作为数据访问组件,通常用于持久层。

示例:

1@Repository 2public class MyRepository { 3 // Repository logic here 4}

@Controller

解释:用于标记一个类作为 Spring MVC 控制器。

示例:

1@Controller 2public class MyController { 3 // Controller logic here 4}

@RestController

解释:结合@Controller 和@ResponseBody,用于创建 RESTful 风格的控制器。

示例:

1@RestController 2public class MyRestController { 3 @GetMapping("/hello") 4 public String hello() { 5 return "Hello, World!"; 6 } 7}

@Configuration

解释:标志着一个类是 Spring 的配置类,通常用于定义 Bean。

示例:

1@Configuration 2public class MyConfig { 3 @Bean 4 public MyBean myBean() { 5 return new MyBean(); 6 } 7}

这些注解用于定义和管理 Spring Bean,是 Spring Boot 应用程序中的重要组成部分。每个注解都有不同的用途和上下文,你可以根据应用程序的需求使用适当的注解。在 Spring Boot 应用程序中,使用这些注解可以轻松创建和管理 Bean,而无需显式的 XML 配置。

四、Spring Boot 数据访问注解

@Repository  

解释:标志着一个类是 Spring Data 仓库,用于数据库访问。  

示例:

1@Repository 2 public class UserRepository { 3 // Data access methods here 4}

@Entity  

解释:用于定义 JPA 实体类,映射到数据库表。

示例:

1@Entity 2 public class User { 3 // Entity fields and methods here 4}

五、Spring Boot 依赖注入注解

@Autowired  

解释:用于自动装配 Bean,通常与构造函数、Setter 方法或字段一起使用。  

示例:

1 @Service 2 public class MyService { 3 private final MyRepository repository; 4 5 @Autowired 6 public MyService(MyRepository repository) { 7 this.repository = repository; 8 } 9} 10

@Qualifier  

解释:与@Autowired 一起使用,用于指定要注入的 Bean 的名称。  

示例:

1 @Service 2 public class MyService { 3 private final MyRepository repository; 4 5 @Autowired 6 public MyService(@Qualifier("myRepository") MyRepository repository) { 7 this.repository = repository; 8 } 9}

六、其他常用注解

@Value

  1. 解释:用于注入属性值,通常从配置文件中获取。
  2. 示例:
1@Component 2public class MyComponent { 3 @Value("${my.property}") 4 private String myProperty; 5}

@ConfigurationProperties

解释:用于将配置属性绑定到一个 POJO 类,通常用于从配置文件中读取属性值。

示例:

1 @Configuration 2 @ConfigurationProperties(prefix = "my") 3 public class MyProperties { 4 private String property1; 5 private int property2; 6 // Getters and setters 7}

这只是 Spring Boot 中一些常用注解的分类和示例,还有许多其他注解可用于更专业的用例。根据你的需求,你可以选择使用适当的注解来配置和管理你的 Spring Boot 应用程序。

使用 Apifox 测试和管理接口

Apifox 是一个比 Postman 更强大的接口测试工具,Apifox = Postman + Swagger + Mock + JMeter, 支持调试 http(s)、WebSocket、Socket、gRPCDubbo 等协议的接口,并且集成了 IDEA 插件。在开发完接口后,可以通过 Apifox 的 IDEA 插件一键自动生成接口文档,多端同步,非常方便测试和维护。

alt

alt

知识扩展:

点赞
收藏

评论区

加载中...

相关推荐

spring注解

随着越来越多地使用Springboot敏捷开发,更多地使用注解配置Spring,而不是Spring的applicationContext.xml文件。Configuration注解:Spring解析为配置类,相当于spring配置文件Bean注解:容器注册Bean组件,默认id为方法名@Configurat

Spring注解大全,汇总版

Spring使用的注解大全和解释注解解释@Controller组合注解(组合了@Component注解),应用在MVC层(控制层),DispatcherServlet会自动扫描注解了此注解的类,然后将web请求映射到注解了@RequestMapping的方法上。@Service组合注解(组合了@Component注解),应用在

1.1Spring Boot 环境配置和常用注解

SpringBoot常用注解:@Service:注解在类上,表示这是一个业务层bean@Controller:注解在类上,表示这是一个控制层bean@Repository:注解在类上,表示这是一个数据访问层bean@Component:注解在类上,表示通用bean,value不写默认就是类名首字母小写@Auto

0、Spring 注解驱动开发

0、Spring注解驱动开发0.1简介《Spring注解驱动开发》是一套帮助我们深入了解Spring原理机制的教程;现今SpringBoot、SpringCloud技术非常火热,作为Spring之上的框架,他们大量使用到了Spring的一些底层注解、原理,比如@Conditional、@Import、@

Spring Boot面试题(2020最新版)

文章目录概述什么是SpringBoot?SpringBoot有哪些优点?SpringBoot的核心注解是哪个?它主要由哪几个注解组成的?配置什么是JavaConfig?S

SpringBoot学习(五)

SpringBoot的核心1、入口类和@SpringBootApplicationSpringBoot的项目一般都会有\Application的入口类,入口类中会有main方法,这是一个标准的Java应用程序的入口方法。而@SpringBootApplication注解是SpringBoot的核心