内容概述
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
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
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
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框架的设计理念有了自己一定的理解?如果有,我很开森。