什么是 Spring?

我们一般说 Spring 框架指的都是 **Spring Framework**,是一个开源的轻量级的 Java 开发框架,**它是很多模块的集合**,我们可以自由选择自己需要的模块,使用这些模块可以很方便地协助我们进行开发。 <img src="https://cs-wiki.oss-cn-shanghai.aliyuncs.com/img/image-20220708145108156.png" style="zoom: 67%;" /> Spring 的核心模块如下(对应的是 Spring4.x 版本。目前最新的 5.x 版本中 Web 模块的 Portlet 组件已经被废弃掉,同时增加了用于异步响应式处理的 WebFlux 组件): <img src="https://cs-wiki.oss-cn-shanghai.aliyuncs.com/img/image-20220708145145390.png" style="zoom:67%;" /> - **Core Container 核心容器**:核心模块, Spring 其他所有的功能基本都需要依赖于该类库,主要提供对 IoC (依赖注入) 的支持 > 包含以下 4 个 package: > > - The [org.springframework.core](http://www.javadoc.io/doc/org.springframework/spring-core/4.1.2.RELEASE) package. In which, base functionalities that cut across the whole framework is implemented, e.g. exceptions, version detection etc. > - The [org.springframework.beans](http://www.javadoc.io/doc/org.springframework/spring-beans/4.1.2.RELEASE) package. In which we have interfaces and classes for creating and managing Java beans. The `BeanFactory` interface is found here. > - The [org.springframework.context](http://www.javadoc.io/doc/org.springframework/spring-context/4.1.2.RELEASE) Package. This builds on the beans package. An example of an interface out of this package that you would have used is the `ApplicationContext` (it implements the BeanFactory interface)。简单理解,context 建立在 core 和 beans 之上,扩展并添加了额外的功能(这就是为什么只导入 Spring-Context 一个依赖关系就可以使用 Spring IoC 容器功能) > - The [org.springframework.expression](http://www.javadoc.io/doc/org.springframework/spring-expression/4.1.2.RELEASE) package. This provides the expression language that can be used to traverse and manipulate the object graph within the container at runtime. - **AOP**:提供了面向切面的编程实现。 - **Data Access/Integration 数据访问/集成**:该层提供与数据库交互的支持,由 5 个模块组成: - spring-jdbc : 提供了对数据库访问的抽象 JDBC。不同的数据库都有自己独立的 API 用于操作数据库,而 Java 程序只需要和 JDBC API 交互,这样就屏蔽了数据库的影响。 - spring-tx : 提供对事务的支持。 - spring-orm : 提供对 Hibernate 等 ORM 框架的支持。 - spring-oxm : 提供对 Castor 等 OXM 框架的支持。 - spring-jms : Java 消息服务。 - **Web**:提供创建 Web 应用程序的支持,由 4 个模块组成 - spring-web :对 Web 功能的实现提供一些最基础的支持。 - spring-webmvc : 提供对 Spring MVC 的实现。 - spring-websocket : 提供了对 WebSocket 的支持,WebSocket 可以让客户端和服务端进行双向通信。 - spring-webflux :提供对 WebFlux 的支持。WebFlux 是 Spring Framework 5.0 中引入的新的响应式框架。与 Spring MVC 不同,它不需要 Servlet API,是完全异步. - Test:提供对 JUnit(单元测试框架)、TestNG(类似 JUnit)、Mockito(主要用来 Mock 对象)、PowerMock(解决 Mockito 的问题比如无法模拟 final, static, private 方法)等常用的测试框架的支持 - Instrumentation:该层为类检测和类加载器实现提供支持 - 几个杂项模块: - Messaging – 该模块为 STOMP 提供支持。它还支持注解编程模型,该模型用于从 WebSocket 客户端路由和处理 STOMP 消息。 - Aspects – 该模块为与 AspectJ 的集成提供支持。