Spring Boot

发布于:2025-06-08 ⋅ 阅读:(21) ⋅ 点赞:(0)

一、Spring Boot 简介

Spring Boot 是基于 Spring 框架之上的快速开发框架,旨在简化 Spring 应用的构建过程,让开发者更专注于业务逻辑,而不是繁琐的配置。

特点:

  • 自动配置(Auto Configuration)

  • 起步依赖(Starter Dependencies)

  • 内嵌式服务器(如 Tomcat)

  • 无需大量 XML 配置

  • 配置灵活(支持 application.properties / application.yml


二、环境准备

1. 开发工具

  • IDE:IntelliJ IDEA(推荐)

  • JDK:Java 8 或更高版本

  • 构建工具:Maven 或 Gradle

  • 数据库:MySQL(或 H2 作为内嵌数据库)

2. 创建项目

推荐使用 Spring 官方提供的 Spring Initializr 来快速生成项目结构。


三、项目结构说明

src
 └── main
     ├── java
     │   └── com.example.demo
     │       ├── controller
     │       ├── service
     │       ├── repository
     │       └── DemoApplication.java
     └── resources
         ├── application.yml
         ├── static/
         └── templates/

四、核心注解介绍

注解 说明
@SpringBootApplication 核心注解,包含 @Configuration@EnableAutoConfiguration@ComponentScan
@RestController 组合注解,包含 @Controller + @ResponseBody
@RequestMapping 用于请求路径映射
@Autowired 自动注入依赖
@Service / @Repository 标识服务层 / 数据访问层 Bean

五、配置文件说明(application.yml)

server:
  port: 8080

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/demo
    username: root
    password: root
  thymeleaf:
    cache: false
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true

六、核心功能实战

1. Web 接口开发

Controller 示例:

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

2. 服务层(Service)

@Service
public class UserService {
    public String getUserInfo() {
        return "用户信息";
    }
}

3. 数据访问层(Repository)

使用 Spring Data JPA:

public interface UserRepository extends JpaRepository<User, Long> {}

七、常用模块

1. Spring Boot + MyBatis

  • 添加依赖:

<dependency>
  <groupId>org.mybatis.spring.boot</groupId>
  <artifactId>mybatis-spring-boot-starter</artifactId>
  <version>2.3.1</version>
</dependency>
  • Mapper 接口 + XML 配置

2. Spring Boot + Thymeleaf

用于构建前端模板页面:

@Controller
public class PageController {
    @GetMapping("/home")
    public String home(Model model) {
        model.addAttribute("msg", "欢迎!");
        return "home";
    }
}

八、定时任务实现

@Component
public class ScheduledTasks {
    @Scheduled(cron = "0/10 * * * * ?")
    public void reportCurrentTime() {
        System.out.println("现在时间:" + new Date());
    }
}
  • 启动类中添加:@EnableScheduling


九、异常处理机制

@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(Exception.class)
    public ResponseEntity<String> handle(Exception e) {
        return ResponseEntity.status(500).body("服务器异常:" + e.getMessage());
    }
}

十、打包部署

使用 Maven 打包:

mvn clean package

运行 Jar:

java -jar target/demo-0.0.1-SNAPSHOT.jar

十一、常见问题与调试技巧

  • 端口被占用? 修改 application.yml 中的 server.port

  • 404 页面? 检查路径是否映射正确

  • 自动配置失效? 查看是否存在 @SpringBootApplication@ComponentScan 被覆盖的问题

  • 调试 SQL? 开启 spring.jpa.show-sql=true


十二、进阶学习建议

  • 学习 Spring Boot 与 SSM 的整合

  • 理解 Spring Boot 自动配置原理(查看源码:spring-boot-autoconfigure

  • 深入使用 AOP、缓存、消息队列(RabbitMQ/Kafka)

  • 掌握 Spring Cloud 微服务架构