☕ Spring Boot 基础入门

概念 从零了解 Spring Boot,理解核心注解和配置,跑通第一个 Hello World

🤔 什么是 Spring Boot?

Spring Boot 是基于 Spring 框架的快速开发工具。简单来说,它帮你省去了大量繁琐的配置,让你能专注于写业务代码。

用一个生活中的比喻:

🏠 装修比喻

传统 Spring 就像毛坯房 —— 你需要自己装水电、铺地板、刷墙,每一步都要手动配置。
Spring Boot 就像精装修拎包入住 —— 水电、地板、墙面都帮你搞好了,你只需要把家具搬进去(写业务代码)。

Spring Boot 的核心优势

💡 为什么不用传统 Java Web?

在没有 Spring Boot 之前,搭建一个 Java Web 项目需要:

  1. 安装并配置 Tomcat 服务器
  2. 编写 web.xml 部署描述文件
  3. 手动配置 Spring 的 XML 文件(一大堆 bean 定义)
  4. 手动管理依赖版本冲突

Spring Boot 把这些全部自动化了。来看看对比:

对比项传统 Java WebSpring Boot
配置方式XML 配置文件(几十上百行)注解 + application.yml(几行搞定)
服务器单独安装 Tomcat内嵌,直接运行 main 方法
依赖管理手动引入,容易版本冲突Starter 统一管理版本
启动一个接口~50 行代码 + 配置~10 行代码,零配置
开发效率慢,配置多快,约定大于配置

🧩 核心注解与配置

Spring Boot 中你会频繁遇到以下几个东西,先混个脸熟:

3.1 @SpringBootApplication —— 启动入口

这个注解放在主类上,表示"这是一个 Spring Boot 应用"。它其实是三个注解的组合:

@SpringBootApplication  // = @Configuration + @EnableAutoConfiguration + @ComponentScan
public class KuaiServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(KuaiServiceApplication.class, args);
    }
}

@ComponentScan 会让 Spring 自动扫描当前包及子包下所有带注解的类(比如 @RestController、@Service),把它们注册到容器里。

3.2 @RestController —— 接口控制器

标记一个类为"REST 接口",浏览器或前端发 HTTP 请求时,就会调用这个类里的方法。

@RestController
public class HelloController {

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

访问 http://localhost:8080/hello,浏览器就会显示 Hello, Spring Boot!

3.3 @Service —— 业务逻辑层

标记一个类为"服务",专门放业务逻辑。Controller 负责接收请求,Service 负责处理具体事情。

@Service
public class UserService {
    public String getUserName(Long id) {
        // 这里写查询数据库等业务逻辑
        return "用户" + id;
    }
}

3.4 @Autowired / @Resource —— 自动注入

Spring 的核心功能:依赖注入(DI)。你不用自己 new 对象,Spring 会自动帮你创建并注入进来。

@RestController
public class UserController {

    @Autowired  // Spring 自动把 UserService 实例注入进来
    private UserService userService;

    @GetMapping("/user/{id}")
    public String getUser(@PathVariable Long id) {
        return userService.getUserName(id);
    }
}

@Autowired vs @Resource 区别

@Autowired:Spring 提供的,按类型匹配
@Resource:Java 标准注解,按名称匹配
在实际项目中两者经常混用,效果类似。

3.5 application.yml —— 配置文件

所有可配置项都写在这里,比如端口号、数据库连接等:

server:
  port: 8080

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/kuai
    username: root
    password: 123456

对比传统 XML 配置动辄几百行,YAML 格式简洁直观。

🚀 动手:Hello World 示例

一个最小的 Spring Boot 项目只需要这些东西:

第一步:pom.xml 引入依赖

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.2.0</version>
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>  <!-- Web 开发起步依赖 -->
    </dependency>
</dependencies>

第二步:写主类

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

第三步:写 Controller

@RestController
public class HelloController {
    @GetMapping("/hello")
    public String hello() {
        return "Hello, Spring Boot!";
    }
}

第四步:运行

运行 main 方法,控制台会出现 Spring Boot 的 banner,表示启动成功。打开浏览器访问 http://localhost:8080/hello 即可。

就这么简单!不需要安装 Tomcat,不需要写 XML,不需要配置 web.xml。

试试问 Agent:"Spring Boot 的自动配置原理是什么?" 或者 "@Controller 和 @RestController 有什么区别?"

📝 Quiz — 检验你的理解

Q1: @SpringBootApplication 注解包含了以下哪些注解?
Q2: @Autowired 和 @Resource 的主要区别是什么?
Q3: Spring Boot 项目启动后,默认监听哪个端口?