一、Spring 框架的 7 大基本概念(必须牢牢记住)
| 概念 | 英文全称 | 核心作用与一句话解释 |
|---|---|---|
| 1. IoC(控制反转) | Inversion of Control | 谁创建对象?以前是你 new,现在交给 Spring 容器来创建 |
| 2. DI(依赖注入) | Dependency Injection | 对象需要别的对象时,不自己 new,由容器“注入”给你 |
| 3. Bean | Bean | 所有被 Spring 容器管理起来的对象统称为 Bean |
| 4. Container(容器) | IoC Container | 负责 Bean 的创建、装配、生命周期管理的“工厂” |
| 5. AOP(面向切面编程) | Aspect-Oriented Programming | 把日志、事务、安全等横切关注点从业务代码中抽离 |
| 6. ApplicationContext | ApplicationContext | 高级 IoC 容器,提供国际化、事件发布、资源加载等高级特性 |
| 7. Spring Boot | — | 基于 Spring 的“约定大于配置”快速开发框架,真正主流产品 |
二、Spring 框架 6 大核心模块 + Spring Boot 完整详解(2025 最新版)
| 模块组 | 模块名称 | 具体功能与实际使用场景(重点标注常用度) |
|---|---|---|
| Core Container(核心容器) | spring-core | 提供 IoC 和 DI 的基础功能,所有模块都依赖它 ★★★★★ |
| spring-beans | BeanFactory、Bean 定义、解析、创建 ★★★★★ | |
| spring-context | ApplicationContext(最常用的容器实现)★★★★★ | |
| spring-expression | SpEL 表达式语言(@Value(“#{…}”))★★★★ | |
| AOP 和 Instrumentation | spring-aop | 代理模式的 AOP 实现 ★★★★ |
| spring-aspects | 基于 AspectJ 的切面支持 ★★★★ | |
| spring-instrument | 支持类仪表(运行时织入,Tomcat 热部署用)★★ | |
| Data Access/Integration(数据层) | spring-jdbc | JdbcTemplate,最轻量级的数据库操作方式 ★★★★★ |
| spring-tx | 声明式事务 @Transactional(Transactional)★★★★★ | |
| spring-orm | 集成 JPA、Hibernate、MyBatis 等 ★★★★ | |
| spring-oxm | 对象与 XML 相互转换(几乎不用)★ | |
| spring-jms | JMS 消息支持(ActiveMQ、RabbitMQ 等 ★★ | |
| Web | spring-web | 基础 Web 功能(Multipart、WebUtils 等)★★★★ |
| spring-webmvc | 传统 Servlet 栈 MVC 框架(@Controller、@RestController)★★★★★ | |
| spring-websocket | WebSocket 支持 ★★★ | |
| spring-webflux | 响应式 Web 框架(基于 Reactor,Netty)★★★★(近年越来越火) | |
| Test | spring-test | 单元测试与集成测试支持(@SpringBootTest、MockMvc)★★★★★ |
| 独立生态(非 Framework 核心) | Spring Boot | 自动配置、内嵌服务器、生产就绪特性 ★★★★★(实际开发 99% 用这个) |
| Spring Data | 一站式数据访问(JPA、Mongo、Redis、R2DBC 等)★★★★★ | |
| Spring Security | 认证授权最强框架(OAuth2、JWT、RBAC)★★★★★ | |
| Spring Batch | 大批量数据处理框架 ★★★★ | |
| Spring Integration | 企业集成模式(消息驱动框架 ★★ | |
| Spring Cloud | 微服务全家桶(Eureka、Nacos、Gateway、OpenFeign、Config、Sleuth+Zipkin 等)★★★★★ |
三、最常用的 5 个模块(面试 + 实际项目必问)
- spring-boot(整个项目启动入口 + 自动配置)
- spring-webmvc / spring-webflux(写接口)
- spring-data-jpa 或 mybatis-plus(操作数据库)
- spring-tx + spring-aop(事务管理)
- spring-security(权限认证)
四、Spring 容器两种实现对比(常考)
| 对比项 | BeanFactory | ApplicationContext(最常用) |
|---|---|---|
| 功能 | 基础 IoC 容器 | 高级容器,继承了 BeanFactory |
| 懒加载 | 默认懒加载 | 默认立即加载(可以通过 @Lazy 改成懒加载) |
| 额外功能 | 只有最基本的 IoC | 国际化、事件发布、资源加载、AOP 更完善 |
| 使用场景 | 极致节省内存的场景(几乎不用) | 99.99% 项目都用 ApplicationContext |
| 获取方式 | new ClassPathXmlApplicationContext 已过时 | new AnnotationConfigApplicationContext(AppConfig.class)(主流) |
五、Bean 的生命周期(11 步,面试高频)
- 实例化(new)
2 属性赋值(依赖注入)
3 BeanNameAware.setBeanName()
4 BeanFactoryAware.setBeanFactory()
5 ApplicationContextAware.setApplicationContext()
6 BeanPostProcessor 前置处理
7 @PostConstruct
8 InitializingBean.afterPropertiesSet()
9 或者 init-method
9 BeanPostProcessor 后置处理(AOP 代理在这里生成)
10 真正可以使用
11 销毁:@PreDestroy → DisposableBean.destroy() → destroy-method
掌握这 11 步,你在面试中画生命周期图就无敌了。
结论一句话:
现代 Java 后端开发 = Spring Boot + Spring MVC/WebFlux + Spring Data + Spring Security + Spring Cloud(微服务)
其他所有模块都是围绕这几个核心在扩展。
想继续深挖哪个模块?我可以给你从配置文件到源码级别全部讲透!