目录
2.2 起步依赖(Starter Dependencies)
一、什么是SpringBoot
- 定义:Spring Boot 是Spring生态中的一个子项目,目的是简化 Spring 应用的初始搭建和开发过程,通过 “约定大于配置” 的原则,提供快速、开箱即用的开发体验
- 目标:解决传统Spring开发中复杂的配置和依赖管理问题,让开发者专注于业务逻辑。
二、核心特性
2.1 自动配置(Auto-Configurantion)
- 原理:根据项目中的依赖(如 spring-boot-starter-web 、 spring-boot-starter-data-jpa),自动装配所需的Bean和组件。
- 示例:
- 如果项目中包含 spring-boot-starter-web,Spring Boot 会自动装配内嵌Tomcat 和 Spring MVC。
- 如果包含 spring-boot-starter-data-jpa,则自动配置数据源和JPA相关Bean。
- 自定义:通过 application.properties 或 @configuration 类覆盖默认配置。
2.2 起步依赖(Starter Dependencies)
- 作用:预定义一组依赖的集合,简化Maven/Gradle的依赖管理。
- 常用 Starter:
- spring-boot-starter-web:Web开发(RESTful API)。
- spring-boot-starter-data-jpa:数据库访问(JPA + Hibernate).
- spring-boot-starter-security:安全控制
- spring-boot-starter-test:单元测试
2.3 内嵌服务器(Embedded Server)
- 支持服务器:Tomcat(默认)、Jetty、Undertow。
- 优势:无需部署WAR包,直接通过 java -jar 运行JAR文件。
2.4 生产级特性
- 健康检查: 集成Spring Boot Actuator,提供 /actuator/health 端点。
- 监控指标:通过Micrometer 支持 Prometheus、Graphite等。
- 外部化配置:支持多环境配置(如 application-dev.properties)。
三、Spring Boot 架构
3.1 核心组件
- 主类:通过 @SpringBootApplication 注解标记的启动类,包含 main 方法。
- 自动配置模块: spring-boot-autoconfigure 包,基于条件注解(如 @conditionalOnClass--满足条件才加载)动态加载配置。
- Starter模块:提供预集成的依赖管理。
@SpringBootApplication 注解
@SpringBootConfiguration
- 作用:标识当前类是一个 Spring Boot 的配置类
- 源码:本质上是 @Configuration 的别名
- 功能:等价于 @Configuration ,表示该类中可能定义
@Bean
方法
@EnableAutoConfiguration
启用 Spring Boot 的自动装配机制。
核心机制:
@Import(AutoConfigurationImportSelector.class)
:通过AutoConfigurationImportSelector
加载所有符合条件的自动配置类(如DataSourceAutoConfiguration
)。
@AutoConfigurationPackage
:将当前主类所在的包路径作为自动扫描的基准包。
@C
omponentScan
自动扫描并注册当前包及其子包下的组件(如
@Component
,@Service
,@Controller
)。若未指定
basePackages
,则扫描主类所在的包及其子包。
3.2 启动流程
- 加载 SpringApplication 并初始化。
- 读取 application.properties 或 application.yml 。
- 扫描 @Component、 @Service 等注解,创建Bean。
- 根据依赖自动配置(如数据源、Web MVC)。
- 启动内服务器并监听端口
四、项目结构约定
- 默认目录结构(Maven/Gradle):
src/ main/ java/ com.example.demo/ # 主包 DemoApplication.java # 启动类 resources/ static/ # 静态资源(CSS/JS) templates/ # 模板文件(Thymeleaf) application.properties # 配置文件 test/ # 单元测试
五、常用模块与整合
5.1 Web开发
- RESTful API:
@RestController public class UserController { @GetMapping("/user/{id}") public User getUser(@PathVariable Long id) { // 业务逻辑 } }
- 参数校验:使用 @Valid
5.2 数据访问
- MyBatis整合:
mybatis: mapper-locations: classpath:mapper/*.xml
5.3 安全控制(Spring Security)
- 配置登录和权限:
@Configuration public class SecurityConfig { @Bean public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/admin/**").hasRole("ADMIN") .anyRequest().authenticated() .and().formLogin(); return http.build(); } }
5.4 缓存
- 使用Spring Cache抽象:
@Cacheable("users") public User getUser(Long id) { // 查询数据库 }
六、配置文件与多环境
6.1 配置文件格式
- application.properties :
server.port=8080 spring.datasource.url=jdbc:mysql://localhost:3306/demo
- application.yml(更简洁):
server: port: 8080 spring: datasource: url: jdbc:mysql://localhost:3306/demo
6.2 多环境配置
- 通过文件名区分环境
- application-dev.properties(开发环境)
- application-st.properties(测试环境)
- application-prod.properties(生产环境)
- 激活指定环境 -- bash
java -jar app.jar --spring.profiles.active=prod
七、微服务与Spring Cloud
- Spring Cloud 整合
- 服务注册与发现:Eureka、Consul、Nacos
- 配置中心:Spring Cloud Config
- API 网关:Spring Cloud Gateway
- 负载均衡:Ribbon 或 Spring Cloud LoadBalancer