以下是关于Spring Boot的详细教程大纲,结构清晰,内容由浅入深,适合快速学习和深入理解:
一、Spring Boot 快速入门及核心配置
快速启动
使用Spring Initializr创建项目(IDEA/官网)
项目结构解析:src/main/java、resources/application.properties
第一个REST接口示例:@RestController + @GetMapping
核心配置
配置文件格式
.properties vs .yml 语法对比
多环境配置:application-dev.yml, application-prod.yml
激活环境:spring.profiles.active=dev
配置注入方式
@Value(“${server.port}”) 注解注入单个值
@ConfigurationProperties(prefix=“datasource”) 绑定对象
常用配置项
server:
port: 8080
spring:
datasource:
url: jdbc:mysql://localhost:3306/test
username: root
password: 123456
logging:
level:
org.springframework: DEBUG
二、部署方式与热部署
部署方式
JAR包部署
打包命令:mvn clean package
运行:java -jar your-app.jar --spring.profiles.active=prod
WAR包部署(传统Tomcat)
修改pom.xml为war打包方式
继承SpringBootServletInitializer并重写configure
热部署(DevTools)
添加依赖:
org.springframework.boot
spring-boot-devtools
true
自动重启:修改代码后触发(IDEA需开启Build project automatically)
排除静态资源:spring.devtools.restart.exclude=static/**
三、模板引擎:Thymeleaf vs Freemarker
Thymeleaf
集成依赖:spring-boot-starter-thymeleaf
模板位置:resources/templates/
示例模板:
默认消息
Freemarker
集成依赖:spring-boot-starter-freemarker
模板位置:resources/templates/
示例模板:
${message}
对比选择:
Thymeleaf:HTML原生,适合前后端分离不严格场景。
Freemarker:语法简洁,适合复杂模板逻辑。
四、集成主流框架
Mybatis集成
依赖:
mybatis-spring-boot-starter
配置数据源:
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/test
扫描Mapper接口:@MapperScan(“com.example.mapper”)
Redis集成
依赖:
spring-boot-starter-data-redis
配置连接:
spring:
redis:
host: localhost
port: 6379
使用
RedisTemplate操作数据:
@Autowired
private RedisTemplate<String, String> redisTemplate;
redisTemplate.opsForValue().set(“key”, “value”);
RabbitMQ集成
依赖:
spring-boot-starter-amqp
配置:
spring:
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
生产者示例:
@Autowired
private AmqpTemplate rabbitTemplate;
rabbitTemplate.convertAndSend(“exchange”, “routingKey”, “Hello!”);
消费者示例:
@RabbitListener(queues = “queueName”)
public void receive(String message) {
System.out.println("Received: " + message);
}
五、源码深度解析 重点 最好结合b站教程看 或阅读代码
启动过程分析
入口类:SpringApplication.run()
关键步骤:
加载SpringApplicationRunListener
准备环境Environment
创建ApplicationContext(默认AnnotationConfigServletWebServerApplicationContext)
刷新上下文:refreshContext()(核心!包含Bean加载、自动装配等)
自动装配原理
@SpringBootApplication组合注解解析:
@EnableAutoConfiguration:启用自动配置
@ComponentScan:扫描当前包及子包
条件注解驱动:如
@ConditionalOnClass({DataSource.class})
自定义Starter:
创建META-INF/spring.factories
定义自动配置类:@Configuration + 条件注解
打包为Starter供其他项目引用
六、微服务监控:Actuator & Admin
Spring Boot Actuator
添加依赖:
spring-boot-starter-actuator
暴露端点:
management:
endpoints:
web:
exposure:
include: “*” # 开放所有端点(生产环境需谨慎)
endpoint:
health:
show-details: always
常用端点:
/actuator/health:服务健康状态
/actuator/metrics:JVM指标
/actuator/env:环境变量
Spring Boot Admin
搭建Admin Server:
@EnableAdminServer
@SpringBootApplication
public class AdminApplication { … }
客户端配置:
spring:
boot:
admin:
client:
url: http://localhost:8080
监控面板:实时查看服务状态、日志级别动态调整、线程Dump分析
总结:通过本教程,您将掌握从Spring Boot基础到高级源码解析的全栈技能,能够快速构建企业级应用并深入理解其底层机制。建议结合实战项目练习,如搭建一个包含用户管理(Mybatis)、缓存(Redis)、消息队列(RabbitMQ)和监控(Actuator)的完整系统,以巩固知识。