⭐⭐⭐ Spring Boot 项目实战 ⭐⭐⭐ Spring Cloud 项目实战
《Dubbo 实现原理与源码解析 —— 精品合集》 《Netty 实现原理与源码解析 —— 精品合集》
《Spring 实现原理与源码解析 —— 精品合集》 《MyBatis 实现原理与源码解析 —— 精品合集》
《Spring MVC 实现原理与源码解析 —— 精品合集》 《数据库实体设计合集》
《Spring Boot 实现原理与源码解析 —— 精品合集》 《Java 面试题 + Java 学习指南》

摘要: 原创出处 juejin.cn/post/7239279470250934327 「Cosolar」欢迎转载,保留摘要,谢谢!


🙂🙂🙂关注微信公众号:【芋道源码】有福利:

  1. RocketMQ / MyCAT / Sharding-JDBC 所有源码分析文章列表
  2. RocketMQ / MyCAT / Sharding-JDBC 中文注释源码 GitHub 地址
  3. 您对于源码的疑问每条留言都将得到认真回复。甚至不知道如何读源码也可以请教噢。
  4. 新的源码解析文章实时收到通知。每周更新一篇左右。
  5. 认真的源码交流微信群。

Spring Boot 是一款非常流行的 Java 框架,其注解用法复杂而丰富。 在介绍 Spring Boot 的注解之前,我们需要先了解 Spring 框架中的 AOP(面向切面编程)概念。

Spring 的 AOP 可以帮助开发者实现一些非业务功能的代码,如日志记录、性能监控等。这些功能可以通过定义一个 Aspect(切面) 类来实现。

在 Spring Boot 中,除了常规的 AOP 注解外,还有以下几类注解,这也是我看到一个脑图后的一点点收获,想要自己也能够总结总结所用到的注解,常记定能有所感悟。

  1. 核心注解
  2. 原型注解
  3. SpringBoot注解
  4. SpringCloud注解
  5. 缓存注解
  6. 测试注解
  7. 数据库访问注解
  8. JPA注解
  9. SpringMVC跟REST注解
  10. 全局异常处理注解

接下来我们将分别介绍这几类注解及其使用方法。

一、核心注解

@Configuration

@Configuration 注解可以被用来标记一个类,表示这个类是一个 Bean 配置类。在配置类中,我们可以使用其他 Bean 的定义和依赖,甚至可以使用 @Autowired 和 @Value 注解将其他 Bean 注入到当前的 Bean 中。下面是一个简单的例子:

@Configuration
public class AppConfig {
@Bean
public MyService myService() {
return new MyServiceImpl();
}
}

@Autowired

@Autowired 注解可以将一个 Bean 自动注入到当前类中。默认情况下,@Autowired 的匹配规则是根据类型进行匹配。如果有多个类型相同的 Bean,可以使用 @Qualifier 进行限定。

@Component
public class MyController {
private final MyService myService;

@Autowired
public MyController(MyService myService) {
this.myService = myService;
}
}

@Value

@Value 注解用于从配置文件或环境变量中获取值,可以注入 String、int、long、double、boolean 等类型。使用 ${} 可以引用配置文件中的属性,使用 $() 可以引用系统环境变量。

例如:

@Component
public class MyComponent {
@Value("${my.config.property}")
private String configProperty;

@Value("$MY_ENV_VAR")
private String envVar;
}

@Bean

@Bean 注解用于定义一个 Bean,将其加入到 Spring 容器中。通常在 @Configuration 类中使用。可以指定该 Bean 的名称、作用域、相关依赖等。

@Configuration
public class AppConfig {
@Bean(name = "myService", scope = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public MyService myService() {
return new MyServiceImpl();
}
}

@ComponentScan

@ComponentScan 注解用于扫描包中的组件(包括 Bean、Controller 等),将这些组件加入到 Spring 容器中。

@Configuration
@ComponentScan(basePackages = { "com.example" })
public class AppConfig {
}

@EnableAutoConfiguration

@EnableAutoConfiguration 注解可以自动配置 Spring Boot 应用程序。在启用该注解时,Spring Boot 将根据类路径和配置文件中的信息来尝试猜测并配置应用程序。

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

二、原型注解

@Scope

@Scope 注解用于设置 Bean 的作用域,包括 Singleton、Prototype、Request、Session 等。默认情况下,Bean 是 Singleton 的。

@Configuration
public class AppConfig {
@Bean(name = "myService")
@Scope( ConfigurableBeanFactory.SCOPE_PROTOTYPE )
public MyService myService() {
return new MyServiceImpl();
}
}

@Lazy

@Lazy 注解用于标记一个 Bean 延迟初始化。当容器从 XML 文件、Java 配置类或其他方式加载时,不会创建这个 Bean。

@Configuration
public class AppConfig {
@Bean(name = "myService")
@Lazy
public MyService myService() {
return new MyServiceImpl();
}
}

@DependsOn

@DependsOn 注解用于标识 Bean 之间的依赖关系。在容器启动时,先创建 @DependsOn 注解中指定的 Bean,再创建当前 Bean。

@Configuration
public class AppConfig {
@Bean(name = "myService")
public MyService myService() {
return new MyServiceImpl();
}

@Bean(name = "myController")
@DependsOn("myService")
public MyController myController() {
return new MyController(myService());
}
}

三、SpringBoot注解

@SpringBootApplication

@SpringBootApplication 注解是一个组合注解,等价于同时使用 @Configuration、@EnableAutoConfiguration 和 @ComponentScan 注解。它用于标记主类,并告诉 Spring Boot 启动该应用程序。

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

@RestController

@RestController 注解表示控制器中所有方法都返回 JSON 格式数据。它是 Spring MVC 中 @Controller 注解的扩展。下面是一个简单的例子:

@RestController
public class MyController {
@GetMapping("/greeting")
public String greeting() {
return "Hello, world!";
}
}

@RequestMapping

@RequestMapping 注解用于映射 HTTP 请求。可以使用 value 属性指定 URL 映射,使用 method 属性指定请求方法。

@RestController
@RequestMapping("/api")
public class MyController {
@GetMapping("/greeting")
public String greeting() {
return "Hello, world!";
}
}

@GetMapping

@GetMapping 注解等同于 @RequestMapping(method = RequestMethod.GET),用于映射 HTTP GET 请求。

@RestController
@RequestMapping("/api")
public class MyController {
@GetMapping("/greeting")
public String greeting() {
return "Hello, world!";
}
}

@PostMapping

@PostMapping 注解等同于 @RequestMapping(method = RequestMethod.POST),用于映射 HTTP POST 请求。

@RestController
@RequestMapping("/api")
public class MyController {
@PostMapping("/greeting")
public String greeting() {
return "Hello, world!";
}
}

@PutMapping

@PutMapping 注解等同于 @RequestMapping(method = RequestMethod.PUT),用于映射 HTTP PUT 请求。

@RestController
@RequestMapping("/api")
public class MyController {
@PutMapping("/greeting")
public String greeting() {
return "Hello, world!";
}
}

@DeleteMapping

@DeleteMapping 注解等同于 @RequestMapping(method = RequestMethod.DELETE),用于映射 HTTP DELETE 请求。

@RestController
@RequestMapping("/api")
public class MyController {
@DeleteMapping("/greeting")
public String greeting() {
return "Hello, world!";
}
}

@RequestParam

@RequestParam 注解用于将请求参数映射到方法的参数中。可以使用 value 属性指定参数名,使用 required 属性指定是否必填,使用 defaultValue 属性指定默认值。

@RestController
@RequestMapping("/api")
public class MyController {
@GetMapping("/greeting")
public String greeting(@RequestParam("name") String name) {
return "Hello, " + name + "!";
}
}

@PathVariable

@PathVariable 注解用于将 URL 中的占位符映射到方法的参数中。可以使用 value 属性指定占位符名称。

@RestController
@RequestMapping("/api")
public class MyController {
@GetMapping("/{name}")
public String greeting(@PathVariable("name") String name) {
return "Hello, " + name + "!";
}
}

@RequestBody

@RequestBody 注解用于从请求体中获取数据。通常用于处理 POST、PUT 请求。

@RestController
@RequestMapping("/api")
public class MyController {
@PostMapping("/greeting")
public String greeting(@RequestBody GreetingRequest request) {
return "Hello, " + request.getName() + "!";
}
}

public class GreetingRequest {
private String name;

// getters and setters
}

@ResponseBody

@ResponseBody 注解表示该方法返回的结果直接输出到响应体中。

@RestController
@RequestMapping("/api")
public class MyController {
@GetMapping("/greeting")
@ResponseBody
public String greeting() {
return "Hello, world!";
}
}

@ResponseStatus

@ResponseStatus 注解用于指定请求处理完成后的状态码。

@RestController
@RequestMapping("/api")
public class MyController {
@GetMapping("/greeting")
@ResponseStatus(HttpStatus.OK)
public String greeting() {
return "Hello, world!";
}
}

@ExceptionHandler

@ExceptionHandler 注解用于处理请求抛出的异常。

@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(IllegalArgumentException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ErrorResult handleIllegalArgumentException(IllegalArgumentException ex) {
return new ErrorResult(ex.getMessage());
}
}

public class ErrorResult {
private String message;

public ErrorResult(String message) {
this.message = message;
}

// getters and setters
}

四、SpringCloud注解

@EnableDiscoveryClient

@EnableDiscoveryClient 注解用于启用服务发现功能。使用该注解后,应用程序能够将自己注册到 Eureka 服务器,并从中获取其他实例的信息。

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

@LoadBalanced

@LoadBalanced 注解用于标记 RestTemplate Bean,以启用负载均衡功能。在使用 REST 请求时,RestTemplate 将根据服务名自动选择一个可用的实例。

@Configuration
public class AppConfig {
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
}

@FeignClient

@FeignClient 注解用于定义一个 Feign 客户端。Feign 是一款用于简化 HTTP API 客户端的工具,它支持声明式 REST 客户端。使用 @FeignClient 注解后,我们可以通过接口来调用具有相同功能的微服务。

@FeignClient(name = "my-service")
public interface MyServiceClient {
@GetMapping("/greeting")
String greeting();
}

五、缓存注解

@Cacheable

@Cacheable 注解表示方法的结果应该被缓存起来,下次调用该方法时,如果参数和之前相同,则返回缓存结果。

@Service
public class MyService {
@Cacheable("greetingCache")
public String greeting(String name) {
return "Hello, " + name + "!";
}
}

@CachePut

@CachePut 注解表示方法的结果应该被缓存起来,下次调用该方法时,不会返回缓存结果,而是重新计算结果并缓存起来。

@Service
public class MyService {
@CachePut("greetingCache")
public String greeting(String name) {
return "Hello, " + name + "!";
}
}

@CacheEvict

@CacheEvict 注解表示方法执行后从缓存中删除指定项。

@Service
public class MyService {
@CacheEvict("greetingCache")
public void clearGreetingCache() {
}
}

六、测试注解

@SpringBootTest

@SpringBootTest 注解用于测试 Spring Boot 应用程序。它会创建一个完整的 Spring 应用程序上下文,并在测试期间使用它。

@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class MyControllerTest {
@Autowired
private TestRestTemplate restTemplate;

@Test
public void testGreeting() {
ResponseEntity<String> responseEntity = restTemplate.getForEntity("/api/greeting", String.class);
assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(responseEntity.getBody()).isEqualTo("Hello, world!");
}
}

@MockBean

@MockBean 注解用于模拟一个 Bean 的实现。在测试过程中,可以使用 Mockito.when() 等方法来指定一些行为。

@SpringBootTest
public class MyServiceTest {
@Autowired
private MyService myService;

@MockBean
private MyRepository myRepository;

@Test
public void testFindById() {
Mockito.when(myRepository.findById(1L)).thenReturn(Optional.of(new MyEntity()));
MyEntity entity = myService.findById(1L);
assertThat(entity).isNotNull();
Mockito.verify(myRepository).findById(1L);
}
}

七、数据库访问注解

@Transactional

@Transactional 注解用于指定一个方法需要在事务中执行。默认情况下,只有 RuntimeException 会触发事务回滚。

@Service
public class MyService {
@Autowired
private MyRepository myRepository;

@Transactional
public void save(MyEntity entity) {
myRepository.save(entity);
}
}

@Repository

@Repository 注解用于标记数据访问层,表示这个类是一个数据仓库。

@Repository
public interface MyRepository extends JpaRepository<MyEntity, Long> {
}

@Entity

@Entity 注解用于标记实体类,表示这个类是一个 JPA 实体。它与数据库中的表相对应。

@Entity
@Table(name = "my_entity")
public class MyEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

// getters and setters
}

@Id

@Id 注解用于标记主键字段。

@Entity
@Table(name = "my_entity")
public class MyEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

// getters and setters
}

@GeneratedValue

@GeneratedValue 注解用于指定主键的生成策略。

@Entity
@Table(name = "my_entity")
public class MyEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

// getters and setters
}

@Column

@Column 注解用于指定实体属性与数据库表列之间的映射关系。

@Entity
@Table(name = "my_entity")
public class MyEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(name = "name")
private String name;

// getters and setters
}

八、JPA注解

@EnableJpaRepositories

@EnableJpaRepositories 注解用于启用 Spring Data JPA 功能。它会自动配置 Spring Data JPA 相关的 Bean。

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

@Query

@Query 注解用于自定义查询语句。

@Repository
public interface MyRepository extends JpaRepository<MyEntity, Long> {
@Query("select e from MyEntity e where e.name = :name")
List<MyEntity> findByName(@Param("name") String name);
}

九、全局异常处理注解

@ControllerAdvice

@ControllerAdvice 注解用于定义一个全局异常处理类,用于捕获所有控制器中抛出的异常,进行统一处理。

@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(Exception.class)
public ModelAndView handleException(HttpServletRequest request, Exception ex) {
ModelAndView mav = new ModelAndView();
mav.addObject("exception", ex);
mav.addObject("url", request.getRequestURI());
mav.setViewName("error");
return mav;
}
}

以上是 Spring Boot 中常见的注解和使用方法,终于干完了,虽然相关注解还有很多,以后一点一点充实这篇文章吧。

文章目录
  1. 1. 一、核心注解
    1. 1.1. @Configuration
    2. 1.2. @Autowired
    3. 1.3. @Value
    4. 1.4. @Bean
    5. 1.5. @ComponentScan
    6. 1.6. @EnableAutoConfiguration
  2. 2. 二、原型注解
    1. 2.1. @Scope
    2. 2.2. @Lazy
    3. 2.3. @DependsOn
  3. 3. 三、SpringBoot注解
    1. 3.1. @SpringBootApplication
    2. 3.2. @RestController
    3. 3.3. @RequestMapping
    4. 3.4. @GetMapping
    5. 3.5. @PostMapping
    6. 3.6. @PutMapping
    7. 3.7. @DeleteMapping
    8. 3.8. @RequestParam
    9. 3.9. @PathVariable
    10. 3.10. @RequestBody
    11. 3.11. @ResponseBody
    12. 3.12. @ResponseStatus
    13. 3.13. @ExceptionHandler
  4. 4. 四、SpringCloud注解
    1. 4.1. @EnableDiscoveryClient
    2. 4.2. @LoadBalanced
    3. 4.3. @FeignClient
  5. 5. 五、缓存注解
    1. 5.1. @Cacheable
    2. 5.2. @CachePut
    3. 5.3. @CacheEvict
  6. 6. 六、测试注解
    1. 6.1. @SpringBootTest
    2. 6.2. @MockBean
  7. 7. 七、数据库访问注解
    1. 7.1. @Transactional
    2. 7.2. @Repository
    3. 7.3. @Entity
    4. 7.4. @Id
    5. 7.5. @GeneratedValue
    6. 7.6. @Column
  8. 8. 八、JPA注解
    1. 8.1. @EnableJpaRepositories
    2. 8.2. @Query
  9. 9. 九、全局异常处理注解
    1. 9.1. @ControllerAdvice