美文网首页我爱编程
鸟瞰Spring framework

鸟瞰Spring framework

作者: daoqidelv | 来源:发表于2017-01-02 12:01 被阅读48次

    概述

    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

    相关文章

      网友评论

        本文标题:鸟瞰Spring framework

        本文链接:https://www.haomeiwen.com/subject/ffeivttx.html