参考文档
先把文档给下载下来,不然每次上官网查挺慢的(你懂的)由于准备看的是分支是 4.x 所以就直接看对应版本的 doc 吧
https://spring.io/projects/spring-framework#learn 官网的布局貌似改了些
选的 4.3.18 GA, GA 即 General Available 状态,表明这个版本已经得到广泛应用了。
基本思想
Part I. Overview of Spring framework 中有这么一段话
The Spring Framework Inversion of Control (IoC) component addresses this concern by providing a formalized means of composing disparate components into a fully working application ready for use. The Spring Framework codifies formalized design patterns as first-class objects that you can integrate into your own application(s). Numerous organizations and institutions use the Spring Framework in this manner to engineer robust, maintainable applications.
this concern 指的是其他 java 框架所没能解决的一个问题,java 应用中有很多个对象,它们需要相互协作来完成程序的功能,但是如何将它们给联系起来的任务则落到了架构师和开发人员身上。虽然我们可以通过工厂、抽象工厂、装饰器等设计模式来将多种多样的类和实例对象组合成应用的结构,但是模子相对来说还是比较固定,而且你还是得自己实现整个模子。
spring framework 则以 IOC 控制反转,或更通俗的说 DI “依赖注入” 为基调,提供了一种标准的方式将各独立的组件准备好,直接提供给程序使用。
模块
spring 的模块很多,官方将它的各个模块按照功能类别分组了,如下
分为:测试、核心容器、切面、消息、数据访问/集成、web;其中上面蓝色框的是我比较感兴趣的部分。
-
Core Container 核心容器
- Beans 和 Core 是整个 spring 框架的基础,提供了 IOC 和 DI 特性。其中实现了工厂模式的 BeanFactory 解除了程序中组件的耦合。
- Context 上下文模块,则是在 Beans 和 Core 两个模块的基础上提供 spring 风格的对象访问模式;这类似 ServletContext 的概念,Servlet 上下文携带了一次 Http 请求的 request 和 response;而通过 Spring 上下文 ApplicationContext 能访问其基础模块准备好的了部件(通常叫 bean)。
- SpEL, Expression Language 提供了运行时查询和操作对象的能力。
Aop Aspect Oriented Programming 面向切面,spring aop 实现了 aop 联盟的规范接口。
-
Data Access/Integration 数据访问模块
- spring-jdbc 模块简化了原始 jdbc 编码方式,并统一了各数据库供应商的错误码
- spring-orm 集成了多种 orm 接口,包括 jpa,jdo 和 hibernate。
- spring-oxm 类似 orm, 不过是映射 xml 的;
- spring-jms (Java Message Service)包括消息的生产和消费,4.1版本之后它可以和 spring-messaging 集成
Web
和页面相关的模块,不过这两年的工作基本以服务为主,页面接触的少咯
- spring-web 模块集成了基本的 web 功能,如文件上传、Servlet 监听器。
- spring-webmvc 模块包含 mvc 模式的实现也提供 rest 服务的实现。
使用场景
-
使用第三方web框架, spring 并不限制你和其他框架一起使用;这个场景是很经典的三层架构,只是展示层替换成了别的框架
-
远程调用场景,目前流行的服务类架构则很类似这种的2层(api层 和 逻辑层)