第1章 核心模块与应用场景
Spring Framework使创建Java企业级应用变得容易,它提供了一种简单易用的开发方式。Spring框架提供的生态使其适用于各种应用场景,它是现今非常流行的Java开源框架,对于一个Java开发工程师来说,熟练掌握Spring 是必不可少的。
提示
博客基于Spring Framework 5.3.x
版本编写,要求Java环境JDK 8+
。
一、Spring模块
Spring框架提供了很多功能模块,主要模块如下:
1. 核心容器模块
名称 | 功能 |
---|---|
spring-core | 提供IoC和DI等功能 |
spring-beans | 提供Bean容器等功能 |
spring-context | 基于core和beans模块构建,扩展了功能 |
spring-expression | 提供SpEL表达式支持 |
2. AOP模块
名称 | 功能 |
---|---|
spring-aop | 提供AOP(面向切面编程)支持 |
spring-aspects | 提供AspectJ的集成 |
spring-instrument | 提供类植入和类加载器的实现 |
3. 数据访问模块
名称 | 功能 |
---|---|
spring-jdbc | 提供JDBC抽象层来代替原生JDBC功能 |
spring-orm | 提供主流的ORM框架API支持 |
spring-oxm | 提供支持对象和XML映射抽象层 |
spring-tx | 提供编程式和声明式事务管理 |
4. Web模块
名称 | 功能 |
---|---|
spring-web | 提供Web功能支持 |
spring-webmvc | 提供Web应用程序的MVC和REST Web Services支持 |
spring-webflux | 提供基于响应式编程的Web框架 |
spring-websocket | 提供Web Socket支持 |
5. 测试模块
名称 | 功能 |
---|---|
spring-test | 提供JUnit或TestNG等主流单元测试框架的集成 |
6. 集成模块
名称 | 功能 |
---|---|
spring-context-support | 支持整合第三方工具 |
spring-messaging | 提供消息功能支持 |
spring-jms | 提供支持JMS消息功能支持 |
spring-jcl | 提供日志框架适配器 |
二、应用场景
Spring对一些常用的功能组件都会基于IoC/DI模式来进行封装,简化其使用方式,以便开发人员开箱即用,不仅可以提高开发效率,还降低了耦合,提高了测试性和可移植性。
Spring框架由多个模块构成,它不是一个全有或者全无的解决方案,完全可以按照需求使用全部或者部分特性,尤其对于常见的分层结构的应用程序,可以按需使用。
1. 典型完整的Spring Web应用程序
Spring完整使用场景:
- 应用程序:基于Tomcat Servlet容器
- Web层:使用Spring MVC,包括Controller、Multipart Resolver、JSP等
- 业务层:使用Spring,包括正常业务、远程调用、集成其他组件等
- 数据持久层:使用Spring Data,包括ORM、Repositories、事务等
2. 使用第三方Web框架的应用程序
整合第三方Web框架:
- 应用程序:基于Tomcat Servlet容器
- Web层:使用Struts、Tapestry等第三方框架
- 业务层:使用Spring,包括正常业务、远程调用、集成其他组件等
- 数据持久层:使用Spring Data,包括ORM、Repositories、事务等
3. 基于RPC的应用程序
RPC应用程序:
- 应用程序:基于Tomcat Servlet容器
- 业务层:Web服务使用Spring的
Hessian-
、Burlap-
、Rmi-
等功能访问现有程序
4. 基于EJB的应用程序
EJB应用程序:Spring提供访问层和抽象层来管理EJB,可以复用现有的POJOs,将它们包装在无状态会话Bean中,以便在安全可扩展的Web应用程序中使用。
5. 其他组合的应用程序
我们通常所说的SSM框架和SSH框架就是Spring框架和第三方框架的组合形式:
Web层 | 业务层 | 数据持久层 | |
---|---|---|---|
SSM | SpringMVC | Spring | MyBatis |
SSH | Struts | Spring | Hibernate |
无论以什么方式使用Spring,都能很好的集成到现有的应用程序中,Spring这种灵活的特性给了很大的操作空间,我们不用拘泥于哪种方式,针对业务场景以及历史项目改造成本来确定使用哪种解决方案。