鸟瞰Spring framework

概述

The Spring Framework is a Java platform that provides comprehensive infrastructure support for developing Java applications. Spring handles the infrastructure so you can focus on your application.

Spring Framework为java应用程序提供综合的基础设施,让开发者聚焦于自己的业务逻辑,开发者无需关注各个模块的依赖,全部交给Spring framework完成。Spring Framework将多种设计模式融合,并对外暴露简单的api,方便开发者集成自己的应用。Spring Framework需要解决的核心问题是:IoC,落地实现方式是Dependency Injection。

注:其实对于所有framework需要解决的核心问题都是IoC,将控制流从应用程序那里收上来,以便于扩展和模块化。

核心概念

Inversion of Control(控制反转)

In software engineering,inversion of control(IoC) is a design principle in which custom-written portions of a computer program receive the flow of control from a generic framework. A software architecture with this design inverts control as compared to traditional procedural programming: in traditional programming, the custom code that expresses the purpose of the program calls into reusable libraries to take care of generic tasks, but with inversion of control, it is the framework that calls into the custom, or task-specific, code.

控制反转:控制(flow of control)不再是由应用程序(custom-written portition)来持有,而是交给通用的框架(generic framework)。

电子邮件的相关交互流程(浏览器页面操作、网络传输、服务器端数据状态更改、结果返回、页面渲染等)可以适用到文本编辑器、电子商务网站等等,而不必每个应用场景的应用程序都自己控制上述流程,可以将上述流程控制交给对应的框架完成。

控制反转的目的是为了解耦,提高程序的模块化及其扩展性:

To decouple the execution of a task from implementation.

To focus a module on the task it is designed for.

To free modules from assumptions about how other systems do what they do and instead rely on contracts.

To prevent side effects when replacing a module.

控制反转理论的实现形式有很多:Software frameworks,callbacks,schedulers,event loops,service locator,dependency injection。最近在做的订单支付系统,将支付流水和订单流水状态流转通过job调度异步化处理就是该理论的实践。

在OOP编程中,有诸多实现形式:

Using a factory pattern

Using a service locator pattern

Using a dependency injection

Using template method design pattern

Using strategy design pattern

控制反转 VS 依赖注入

控制反转和依赖注入不是等价的,依赖注入只是控制反转理论的一种实践形式而已,依赖注入使用到了上下文查找。百度百科的信息持有不同观点,它认为控制反转和依赖注入是一回事儿,看官自行理解。

linked to:Inversion_of_control in WIKI

Dependency Injection(依赖注入)

In software engineering,Dependency Injection is a design principle in which code creating a new object supplies the other objects that the new object depends upon for operation. This is a special case of inversion of control. Often a dependency injection framework (or "container") is used to manage and automate the construction and lifetimes of interdependent objects.

依赖注入:是一种IoC的设计理论实践,他将被依赖对象的创建和使用分离开来,并能管理对象依赖及其生命周期。

优点:解耦了被依赖对象的依赖和对象的行为,更利于开发、测试、维护、重构。使用者无需知道服务提供者的创建细节,可以灵活地改变服务的实现,通过配置文件更加利于扩展和管理。

缺点:对象创建和使用的分离,为代码跟踪调试带来了复杂度,会使得OOP的对象和类型膨胀,同时,应用程序会更加依赖于‘DI’框架(如Spring)。

依赖注入的类型

没有依赖注入的情况


Without dependency injection

构造函数注入

可以确保客户端的所有依赖在使用前都是合法的,但是缺乏灵活性,所有的依赖需要在创建客户端对象时完成注入。

Constructor injection

Setter方法注入

灵活性高,不要求在客户端对象创建时完成依赖的注入,而是在使用到依赖时再行检查依赖和完成依赖注入,反过来对依赖合法性的校验是一个挑战,在使用依赖前,需要时刻注意依赖是否已经完成注入。

Setter injection

接口注入

接口注入的好处不是很理解,但是他的缺点很明显:如果存在多个依赖,则客户端需要实现多个接口。(TODO)

Interface injection

Assembler(装配器)

不管是哪种依赖注入方式,都需要有一个‘entry point’完成依赖注入的装配,可以是简单的main方法入口,也可以是builders,factories, 或者其他construction patterns,或者Spring framework这类框架。

Spring framerwork支持‘构造方法注入’和‘Setter方法注入’两种最常用的方式。

Spring framerwork支持xml配置文件形式的依赖配置,也支持注解式的依赖配置。

linked to:Dependency_injection in WIKI

Design Patterns in use

TODO

模块(modules)

Spring framerwork包含大概20个模块,可分组为:Core Container, Data Access/Integration, Web, AOP (Aspect Oriented Programming), Instrumentation, Messaging, Test。

Overview of the Spring Framework


Core Container

The

spring-core

and spring-beans modules 

provide the fundamental parts of the framework

, including the IoC and Dependency Injection features. TheBeanFactoryis a sophisticated implementation of the factory pattern. It removes the need for programmatic singletons and allows you to decouple the configuration and specification of dependencies from your actual program logic.


spring-context 提供依赖查找,同时提供i18n,事件传递(event propagation),资源加载,特定context创建(如为servlet container创建WebApplicationContext)

Data Access/Integration

The spring-jdbc module provides a JDBC-abstraction layer that removes the need to do tedious JDBC coding and parsing of database-vendor specific error codes.

The spring-tx module supports programmatic and declarative transaction management for classes that implement special interfaces and forall your POJOs (Plain Old Java Objects).

The spring-orm module provides integration layers for popula robject-relational mapping APIs, including JPA,JDO, and Hibernate. Using the spring-orm module you can use all of these O/R-mapping frameworks in combination with all of the other features Spring offers, such as the simple declarative transaction management feature mentioned previously.

使用较多的是jdbc和tx模块。

使用场景

全面的Web应用场景

用户交互层的动态绑定、文件上传、表单控制等都交给Spring Framework完成。

中间层应用

没有用户交互层或者用户股交互层由其他framework(如struts)完成。通常的restful service应用就是此类。同时,在如今流行前后端分离、restful风格、微服务架构的潮流下,此类型的应用越来越多。

Spring middle-tier using a third-party web framework


远程调用

需要通过webservice、RMI等调用远程服务时,可以考虑此类应用场景。可以使用Spring的Hessian-,Burlap-,Rmi- 或者JaxRpcProxyFactory classes。

Spring可以支持如下远程调用:RMI、Spring’s HTTP invoker、Hessian、Burlap、JMS、AMQP,每种技术各有利弊,需要根据实际应用场景权衡,具体可参考:remoting-considerations

Spring还支持从client端访问RESTful风格的service,spring通过RestTemplate.class为client端提供访问RESTful风格服务的能力。同时Spring还提供了异步调用RESTful服务的能力,使用AsyncRestTemplate。

RestTemplate’s behavior is customized by providingcallback methodsand configuring the `HttpMessageConverter‘ used to marshal objects into the HTTP request body and to unmarshal any response back into an object.

实际实践中,严格遵循RESTful风格的service不多,更多的是使用http协议完成组件间的通讯,也可以使用RestTemplate完成这类场景的服务调用,不过需要对注入ErrorHandler等组件进行自定义开发。


Remoting access

Artifact(器件)

标黄部分为平时用的较多的Artifact,也是Spring framework的核心Artifact。

Spring Framework Artifacts

参考资料

spring framework overview

Inversion_of_control in WIKI

Dependency_injection in WIKI

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,390评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,821评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,632评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,170评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,033评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,098评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,511评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,204评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,479评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,572评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,341评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,213评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,576评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,893评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,171评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,486评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,676评论 2 335

推荐阅读更多精彩内容