深入解析Spring Boot的常用注解和组件(上)

复制代码
Spring Boot作为Spring生态系统的一部分,通过简化配置和开发过程,使得构建独立、生产级别的Spring应用变得更加容易。本文将详细介绍Spring Boot的常用注解和组件,帮助开发者快速上手并充分利用Spring Boot的强大功能。

## 1. Spring Boot的常用注解

### 1.1 `@SpringBootApplication`

这是Spring Boot应用的核心注解,通常放在主类上,用于标识一个Spring Boot应用。它是以下三个注解的组合:

- `@SpringBootConfiguration`:标识该类为Spring配置类。
- `@EnableAutoConfiguration`:启用Spring Boot的自动配置机制。
- `@ComponentScan`:启用组件扫描,默认扫描主类所在包及其子包。

示例:

```java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

1.2 @RestController

这是一个组合注解,等价于@Controller@ResponseBody,用于定义RESTful Web服务的控制器类。

示例:

复制代码
java复制代码import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyController {

    @GetMapping("/hello")
    public String sayHello() {
        return "Hello, World!";
    }
}

1.3 @RequestMapping

用于映射HTTP请求到控制器的方法或类上,可以用在类级别或方法级别。常用的变种注解包括@GetMapping@PostMapping@PutMapping@DeleteMapping等。

示例:

复制代码
java复制代码import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api")
public class ApiController {

    @GetMapping("/users")
    public List<User> getUsers() {
        // 逻辑
    }
}

1.4 @Service

标识一个服务类(业务逻辑层),将该类标记为Spring的组件,使其被Spring扫描并管理。

示例:

复制代码
java复制代码import org.springframework.stereotype.Service;

@Service
public class MyService {

    public String performService() {
        return "Service logic executed";
    }
}

1.5 @Repository

标识一个数据访问层的组件,通常用于持久化操作。该注解将类标记为Spring的组件,并将其识别为持久化异常的转换目标。

示例:

复制代码
java复制代码import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface UserRepository extends JpaRepository<User, Long> {
    // 自定义查询方法
}

1.6 @Configuration

标识一个配置类,用于定义Bean或导入其他配置类。等价于Spring的XML配置文件。

示例:

复制代码
java复制代码import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MyConfig {

    @Bean
    public MyService myService() {
        return new MyService();
    }
}

1.7 @Autowired

用于自动注入依赖。可以标注在构造器、字段或方法上。Spring会自动匹配并注入合适的Bean。

示例:

复制代码
java复制代码import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

@Controller
public class MyController {

    @Autowired
    private MyService myService;

    // ...
}

在本文中,我们介绍了Spring Boot的常用注解及其应用场景。下一篇文章中,我们将深入探讨Spring Boot的常用组件及其在实际开发中的应用。

相关推荐
半夜修仙8 小时前
RabbitMQ中如何保证消息的可靠性传输
java·分布式·中间件·rabbitmq·github·java-rabbitmq
Flittly8 小时前
【AgentScope Java新手村系列】(3)工具系统
java·spring boot·spring
吴声子夜歌8 小时前
Java——多线程编程技巧
java·多线程
神奇小汤圆8 小时前
什么是面向切面编程AOP?
后端
倾颜8 小时前
从手写 Runner 到 LangGraph:受控 Agent 接入 LangGraph
前端·后端·langchain
AI行业学习8 小时前
CC-Switch v3.16.1 官方下载 | 安装配置详细教程【2026.6.10】
java·开发语言·vue.js·python·mysql·eclipse·html
谁在黄金彼岸9 小时前
Lance模型解读
后端
神奇小汤圆9 小时前
深入理解MySQL事务隔离级别:MVCC机制与Next-Key Lock如何解决幻读问题?
后端
万少9 小时前
一封邮件,让我重新打开了搁置半年的鸿蒙应用
前端·javascript·后端
Java编程爱好者9 小时前
手把手看懂 Java 字节码:讲透 Integer 判等、静态方法重写与 try-finally 核心底层
后端