[笔记] Spring Framework设计理念

内容概述

1. 模块化、非侵入式设计

2. 依赖注入/控制反转,实现设计模式

3. Core、Beans模块提供框架的基础部分BeanFactory消除了对编程单例的需要,并将依赖关系的配置和规范与你的实际程序逻辑分离)

4. Context模块建立在基础模块之上,是以框架方式访问对象的一种方法ApplicationContext是模块的焦点)

5. context-support模块提供将常用的第三方包集成到应用上下文的支持

6. Expression模块提供强大的表达式语言,用于在运行时查询和操作对象图

7. AOP模块提供符合AOP联盟的面向切面的编程实现(定义方法拦截器和切入点来干净地分离应该分离地实现功能的代码)

8. jdbc模块提供一个JDBC抽象层(不需要进行繁琐的JDBC编码和数据库供应商的特定错误代码的解析)

9. webmvc模块包括Web应用的模型-视图-控制器(MVC)和REST Web服务实现MVC框架提供领域模型代码和Web表单的清晰分离)


Part I. Overview of Spring Framework - Spring框架概述

The Spring Framework is a lightweight solution and a potential one-stop-shop for building your enterprise-ready applications. However, Spring is modular, allowing you touse only those parts that you need, without having to bring in the rest. You can use the IoC container, with any web framework on top, but you can also use only the Hibernate integration code or the JDBC abstraction layer. The Spring Framework supports declarative transaction management, remote access to your logic through RMI or web services, and various options for persisting your data. It offers a full-featured MVC framework, and enables you tointegrate AOP transparently into your software.

Spring框架是一个用于构建企业级应用轻量级的解决方案和潜在的一站式服务模块化设计,允许您仅使用需要的模块,而无需使用其余模块。它提供一个全功能的MVC框架,使您能够将AOP透明地集成到软件中。

Spring is designed to be non-intrusive, meaning that your domain logic code generally has no dependencies on the framework itself. In your integration layer (such as the data access layer), some dependencies on the data access technology and the Spring libraries will exist. However, it should be easy to isolate these dependencies from the rest of your code base.

非侵入式设计,意味着您的领域逻辑代码通常不依赖于框架本身。它应该很容易地这些依赖关系与您的代码库的其余部分隔离开来

This document is a reference guide to Spring Framework features. If you have any requests, comments, or questions on this document, please post them on the user mailing list. Questions on the Framework itself should be asked on StackOverflow(see https://spring.io/questions).

本文档是Spring框架所有功能的参考指南。

2. Introduction to the Spring Framework - Spring框架简介

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框架提供开发Java应用的综合基础设施Spring处理基础设施,以便您可以专注于您的应用。(职责分明)

2.1 Dependency Injection and Inversion of Control - 依赖注入和控制反转

A Java application — a loose term that runs the gamut from constrained, embedded applications to n-tier, server-side enterprise applications — typically consists of objects that collaborate to form the application proper. Thus the objects in an application have dependencies on each other.

Java应用通常由一系列协作的对象组成,应用中的对象彼此有依赖关系

Although the Java platform provides a wealth of application development functionality, it lacks the means to organize the basic building blocks into a coherent whole, leaving that task to architects and developers. Although you can use design patterns, such as Factory, Abstract Factory, Builder, Decorator, and Service Locator, to compose the various classes and object instances that make up an application, these patterns are simply that: best practices given a name, with a description of what the pattern does, where to apply it, the problems it addresses, and so forth. Patterns are formalized best practices that you must implement yourself in your application.

Java平台缺乏将基本构建块组织成一个整体的手段。你可以使用设计模式编写组成应用的各种类和对象实例。这些模式简单地说:最佳实践给了一个名字,描述了模式做什么,哪里应用它,它解决的问题等等。模式是正式的最佳实践,但必须在应用中自己实现

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.

控制反转(IoC)组件通过提供将不同组件组成完全工作应用的形式化手段来解决这一问题(必须在应用中自己实现模式)。Spring框架编写了正式的设计模式作为一流的对象,构建健壮的、可维护的应用

Background

"The question is, what aspect of control are [they] inverting?" Martin Fowler posed this question about Inversion of Control (IoC) on his site in 2004. Fowler suggested renaming the principle to make it more self-explanatory and came up with Dependency Injection.

"问题是:什么方面的控制是需要反转的?" Martin Fowler建议,依赖注入使控制反转原则更容易理解。

2.2 Framework Modules - 模块化

The Spring Framework consists of features organized into about 20 modules. These modules are grouped into Core Container, Data Access/Integration, Web, AOP (Aspect Oriented Programming), Instrumentation, Messaging, and Test, as shown in the following diagram.

Figure 2.1. Overview of the Spring Framework

Figure 2.1. Overview of the Spring Framework

2.2.1 Core Container - 核心容器

The Core Container consists of the spring-core, spring-beans, spring-context, spring-context-support, and spring-expression(Spring Expression Language) modules.

The spring-core and spring-beans modules provide the fundamental parts of the framework, including the IoC and Dependency Injection features. The BeanFactory is 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.

Core、Beans模块提供框架的基础部分BeanFactory消除了对编程单例的需要,并将依赖关系的配置和规范你的实际程序逻辑分离

The Context (spring-context) module builds on the solid base provided by the Core and Beans modules: it is a means to access objects in a framework-style manner that is similar to a JNDI registry. The Context module inherits its features from the Beans module and adds support for internationalization (using, for example, resource bundles), event propagation, resource loading, and the transparent creation of contexts by a Servlet container. The Context module also supports Java EE features such as EJB, JMX, and basic remoting.The ApplicationContext interface is the focal point of the Context module. spring-context-support provides support for integrating common third-party libraries into a Spring application context for caching (EhCache, Guava, JCache), mailing (JavaMail), scheduling (CommonJ, Quartz) and template engines (FreeMarker, JasperReports,Velocity).

Context模块建立在基础模块之上,是以框架方式访问对象的一种方法。Context模块继承Beans模块的功能,并增加对国际化、事件传播、资源加载和上下文的透明创建的支持。ApplicationContext是模块的焦点。context-support模块提供将常用的第三方包集成到应用上下文的支持。

The spring-expression module provides a powerful Expression Language for querying and manipulating an object graph at runtime. It is an extension of the unified expression language (unified EL) as specified in the JSP 2.1specification. The language supports setting and getting property values, property assignment, method invocation, accessing the content of arrays, collections and indexers, logical and arithmetic operators, named variables, and retrieval of objects by name from Spring’s IoC container. It also supports list projection and selection as well as common list aggregations.

Expression模块提供强大的表达式语言,用于在运行时查询和操作对象图

2.2.2 AOP and Instrumentation

The spring-aop module provides an AOP Alliance-compliant aspect-oriented programming implementation allowing you to define method interceptors and pointcuts to cleanly decouple code that implements functionality that should be separated. Using source-level metadata functionality, you can also incorporate behavioral information into your code, in a manner similar to that of .NET attributes.

AOP模块提供符合AOP联盟的面向切面的编程实现,允许您定义方法拦截器和切入点干净地分离应该分离地实现功能的代码

The separate spring-aspects module provides integration with AspectJ.

The spring-instrument module provides class instrumentation support and classloader implementations to be used in certain application servers. The spring-instrument-tomcat module contains Spring’s instrumentation agent for Tomcat.

2.2.3 Messaging

Spring Framework 4 includes a spring-messaging module with key abstractions from the Spring Integration project such as Message, MessageChannel, MessageHandler, and others to serve as a foundation for messaging-based applications. The module also includes a set of annotations for mapping messages to methods, similar to the Spring MVC annotation based programming model.

2.2.4 Data Access/Integration

The Data Access/Integration layer consists of the JDBC, ORM, OXM, JMS, and Transaction modules.

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.

jdbc模块提供一个JDBC抽象层,不需要进行繁琐的JDBC编码和数据库供应商的特定错误代码的解析

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

The spring-orm module provides integration layers for popular object-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.

The spring-jms module (Java Messaging Service) contains features for producing and consuming messages. Since Spring Framework 4.1, it provides integration with the spring-messaging module.

2.2.5 Web

The Web layer consists of the spring-web, spring-webmvc, spring-websocket, and spring-webmvc-portlet modules.

The spring-web module provides basic web-oriented integration features such as multipart file upload functionality and the initialization of the IoC container using Servlet listeners and a web-oriented application context. It also contains an HTTP clientand the web-related parts of Spring’s remoting support.

The spring-webmvc module (also known as the Web-Servlet module) contains Spring’s model-view-controller (MVC) and REST Web Services implementation for web applications. Spring’s MVC framework provides a clean separation between domain model code and web forms and integrates with all of the other features of the Spring Framework.

webmvc模块包括Web应用的模型-视图-控制器(MVC)和REST Web服务实现。MVC框架提供领域模型代码Web表单的清晰分离

2.2.6 Test

The spring-test module supports the unit testing and integration testing of Spring components with JUnit or TestNG. It provides consistent loading of Spring ApplicationContexts and caching of those contexts. It also provides mock objects that you can use to test your code in isolation.

2.3 Usage scenarios - 使用场景

The building blocks described previously make Spring a logical choice in many scenarios, from embedded applications that run on resource-constrained devices to full-fledged enterprise applications that use Spring’s transaction management functionality and web framework integration.

Figure 2.2. Typical full-fledged Spring web application

Figure 2.2. Typical full-fledged Spring web application

Spring’s declarative transaction management features make the web application fully transactional, just as it would be if you used EJB container-managed transactions. All your custom business logic can be implemented with simple POJOs and managed by Spring’s IoC container. Additional services include support for sending email and validation that is independent of the web layer, which lets you choose where to execute validation rules. Spring’s ORM support is integrated with JPA, Hibernate and JDO; for example, when using Hibernate, you can continue to use your existing mapping files and standard Hibernate SessionFactory configuration. Form controllers seamlessly integrate the web-layer with the domain model, removing the need for ActionForms or other classes that transform HTTP parameters to values for your domain model.

Spring的声明式事务管理功能使Web应用具有完全的事务性。所有您自定义的业务逻辑都可以使用简单的POJOs实现,并由Spring的IoC容器管理表单控制器和领域模型可以无缝地集成到Web层。

Figure 2.4. Remoting usage scenario

Figure 2.4. Remoting usage scenario

When you need to access existing code through web services, you can use Spring’s Hessian-, Burlap-, Rmi- or JaxRpcProxyFactory classes. Enabling remote access to existing applications is not difficult.

请思考一下,本文是否让您对Spring框架的设计理念有了自己一定的理解?如果有,我很开森。😁

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

推荐阅读更多精彩内容

  • PLEASE READ THE FOLLOWING APPLE DEVELOPER PROGRAM LICENSE...
    念念不忘的阅读 13,459评论 5 6
  • 1 关于自行车,我有很多的记忆。 小时候家里孩子多事情更多,父母基本顾不上我们,除了在外面疯玩还是疯玩。大多未知都...
    周雨聊管理阅读 343评论 0 7
  • 在今天的世界,世人只求朝夕,我却渴望万年。在世俗的眼里,爱只求轰轰烈烈,我却梦想长长久久。在现代的社会,别人都在独...
    花生zfh阅读 169评论 0 0
  • Omega2是onion公司搞出的一款超迷你的IOT设备,体积非常迷你,12月参与的众筹,春节后收到的东西(顺丰发...
    神楽坂寻樱阅读 1,058评论 0 3
  • 终于,老岳开口说话了:“你一个人住,不孤单吗?”小蛮点点头,可嘴巴里吐出来的却是:“我很好啊,一个人…挺自在的。”...
    我是白小姐阅读 338评论 0 0