Part I. 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 to use 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 theHibernate 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 to integrate AOP transparently into your software.
Spring Framework是一种轻量级的解决方案,是构建你的企业级应用程序的潜在一站式解决方案。 尽管如此,Spring是模块化的,允许你只使用你需要的那些部分,而不必引入其他的。 你可以使用IoC容器,任何Web框架在顶部(只是底层用Spring框架,比如ssh,中间那层用了Spring),但你也可以只使用Hibernate集成代码或JDBC抽象层。 Spring框架支持声明式事务管理,通过RMI或Web服务远程访问你的逻辑,以及用于持久存储数据的各种选项。 它提供了一个全功能的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.
Spring设计为非侵入式的,这意味着你所写的逻辑代码通常没有对框架本身的依赖。 在你的集中处理层(例如数据访问层)中,将存在对数据访问技术和Spring库的一些依赖。 但是,应该很容易将这些依赖关系与其余代码库隔离开。
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 Framework特性的参考指南。 如果你对本文档有任何要求,意见或问题,请将其张贴在用户邮件列表中。 框架本身的问题应该在StackOverflow(请参阅https://spring.io/questions)。
1. Spring入门
This reference guide provides detailed information about the Spring Framework. It provides comprehensive documentation for all features, as well as some background about the underlying concepts (such as "Dependency Injection") that Spring has embraced.
本参考指南提供了有关Spring框架的详细信息。 它提供了所有功能的全面文档,以及Spring所拥有的基本概念(例如“依赖注入”)的一些背景知识。
If you are just getting started with Spring, you may want to begin using the Spring Framework by creating a Spring Boot based application. Spring Boot provides a quick (and opinionated) way to create a production-ready Spring based application. It is based on the Spring Framework, favors convention over configuration, and is designed to get you up and running as quickly as possible.
如果你刚刚开始使用Spring
,你可能想要通过创建一个基于Spring Boot
的应用程序来开始使用Spring Framework
。 Spring Boot
提供了一种快速(和建议性)的方式来创建基于生产环境的Spring
应用程序。 它是基于Spring框架
,喜欢约定的配置,并且旨在让你快速启动和运行。
You can use start.spring.io to generate a basic project or follow one of the "Getting Started" guides like the Getting Started Building a RESTful Web Service one. As well as being easier to digest, these guides are very task focused, and most of them are based on Spring Boot. They also cover other projects from the Spring portfolio that you might want to consider when solving a particular problem.
你可以使用start.spring.io
来生成基本项目,或者按照“入门”指南之一,就像开始构建RESTful Web服务
一样。 除了更容易理解消化,这些指南是非常专注于任务,其中大部分都是基于Spring Boot
。 它们还涵盖了Spring
解决特定问题时可能需要考虑的Spring
组合中的其他项目。
2.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 Framework
是一个Java平台框架
,它为开发Java
应用程序的全面的基础架构支持。 Spring
处理基础架构,以便你可以专注于应用程序。
Spring enables you to build applications from "plain old Java objects" (POJOs) and to apply enterprise services non-invasively to POJOs. This capability applies to the Java SE programming model and to full and partial Java EE.
Spring
使你能够从“普通旧Java
对象”(POJO
)构建应用程序,并将企业服务非侵入性地应用于POJO
。 此功能适用于Java SE编程模型和完整和部分Java EE
。
Examples of how you, as an application developer, can benefit from the Spring platform:
- Make a Java method execute in a database transaction without having to deal with transaction APIs.
- Make a local Java method a remote procedure without having to deal with remote APIs.
- Make a local Java method a management operation without having to deal with JMX APIs.
- Make a local Java method a message handler without having to deal with JMS APIs.
作为应用程序开发人员,你可以如何从Spring平台中受益的示例:
- 在数据库事务中执行Java方法,而不必处理事务API。
- 使本地Java方法成为远程过程,而不必处理远程API。
(意思就是无须管相关的远程调用api,Spring已经封装好了,你自己只需要专注自己的逻辑即可,下同,不再解释)
- 使本地Java方法成为管理操作,而不必处理JMX API。
- 使本地Java方法成为消息处理程序,而不必处理JMS API。
2.1 依赖注入和控制反转
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应用程序 - 一个宽松的术语,其运行范围从受限的嵌入式应用程序到n层,服务器端企业应用程序 - 通常由协作形成应用程序的对象组成。 因此,在一个应用程序中的对象具有关于彼此的依赖性。
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.
Spring框架控制反转(IoC)组件通过提供一种形式化的手段将不同的组件组成一个完全可用的应用程序来解决这个问题。 Spring框架将形式化的设计模式编译为可以集成到自己的应用程序中的第一类对象。 许多组织和机构以这种方式使用Spring框架来设计强大的,可维护的应用程序。(就是将各种设计模式融合到框架中,而开发人员只需要关注自己的逻辑,在不知不觉中就已经用到了最佳的设计模式,自己的代码也得以健壮)
背景
"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在2004年在他的网站上提出了关于反转控制(IoC)的问题.Fowler建议重命名这个原则,使其更一目了然,并提出依赖注入。
2.2 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.
Spring框架由大约20个功能模块组成。 这些模块分为核心容器,数据访问/集成,Web,AOP(面向方面的编程),仪器,消息传递和测试,如下图所示。
图2.1 Spring框架的概述
The following sections list the available modules for each feature along with their artifact names and the topics they cover. Artifact names correlate to artifact IDs used in Dependency Management tools.
以下部分列出了每个功能的可用模块及其构件名称及其涵盖的主题。 构件名称与构件关联id用于管理工具的依赖。
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. TheBeanFactory
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.
核心容器由spring-core
,spring-beans
,spring-context
,spring-context-support
和spring-expression(Spring Expression Language)模块
组成。
spring-core
和spring-beans
模块提供了框架的基本部分,包括IoC
和依赖注入功能。 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, for example, 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, in particular for caching (EhCache, JCache) and scheduling (CommonJ, Quartz).
Context(spring-context)
模块建立在Core和Beans模块
提供的实体基础之上:它是一种以类似于JNDI注册表
的框架样式方式访问对象的手段。 Context模块
从Beans模块
继承其特性,并增加了对国际化(例如使用资源束),事件传播,资源加载以及通过例如Servlet容器
的透明创建上下文的支持。 Context模块
还支持Java EE功能
,如EJB
,JMX
和基本远程处理。 ApplicationContext
接口是Context模块
的焦点。 spring-context-support
提供支持将常见的第三方库集成到Spring
应用程序上下文中,特别是用于缓存(EhCache
,JCache
)和调度(CommonJ
,Quartz
)。
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.1 specification. 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.
spring-expression
模块提供了一个强大的表达式语言,用于在运行时查询和操作对象图。 它是JSP 2.1规范中规定的统一表达式语言(统一EL
)的扩展。 该语言支持设置和获取属性值,属性赋值,方法调用,访问数组的内容,集合和索引器,逻辑和算术运算符,命名变量,以及通过Spring IoC
容器中的名称检索对象。 它还支持列表投影和选择以及公共列表聚合。
2.2.2 AOP and Instrumentation
The spring-aop
module provides an AOP Alliance-compliant aspect-oriented programming implementation allowing you to define, for example, 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.
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.
spring-aop
模块提供了一个符合AOP Alliance-compliant
的面向方面的编程实现,允许你定义例如方法拦截器和切入点来干净地解耦实现应该分离的功能的代码。 使用源代码级元数据功能,你还可以以类似于.NET
属性的方式将行为信息合并到代码中。
单独的spring-aspects
模块提供与AspectJ
的集成。
spring-instrument-tomcat
模块提供类仪器支持和类加载器实现以在某些应用服务器中使用。 spring-instrument-tomcat
模块包含Spring
的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.
Spring Framework 4
包括一个 Spring消息传递模块
,从 Spring
集成项目中抽象出来,比如 Message, MessageChannel, MessageHandler 及其他用来提供基于消息的基础服务。 用作基于消息传递的应用程序的基础
。 该模块还包括一组用于将消息映射到方法的注解,类似于基于 Spring MVC
注解的编程模型。
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.
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 and Hibernate. Using the spring-orm
module you can use 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-oxm
module provides an abstraction layer that supports Object/XML mapping implementations such as JAXB, Castor, JiBX and XStream.
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.
数据访问/集成层由JDBC,ORM,OXM,JMS和Transaction 模块组成。
spring-jdbc模块提供了一个JDBC抽象层,消除了对繁琐的JDBC编码和解析数据库供应商特定的错误代码的需要。
spring-tx模块支持实现特殊接口的类以及所有POJO(普通Java对象)的编程和声明事务管理。
spring-orm模块为流行的对象关系映射API提供集成层,包括JPA和Hibernate。使用spring-orm模块,你可以使用这些O / R映射框架结合Spring提供的所有其他功能,例如前面提到的简单声明式事务管理功能。
spring-oxm模块提供了一个支持对象/ XML映射实现的抽象层,例如JAXB,Castor,JiBX和XStream。
spring-jms模块(Java消息服务)包含用于生成和使用消息的功能。从Spring Framework 4.1开始,它提供了与spring-messaging模块的集成。
2.2.5 Web
The Web layer consists of the spring-web
, spring-webmvc
and spring-websocket
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 client and 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.
Web
层由spring-web
,spring-webmvc
和spring-websocket
模块组成。(注:这里和4的文档比少了spring-webmvc-portlet模块
)
spring-web
模块提供基本的面向Web
的集成功能,例如多部分文件上传功能和使用Servlet
侦听器和面向Web
的应用程序上下文来初始化IoC
容器。 它还包含一个HTTP
客户端和Web
的相关部分的Spring
的远程支持。
spring-webmvc
模块(也称为Web-Servlet
模块)包含用于Web
应用程序的Spring
的模型视图控制器(MVC)和REST Web服务实现。 Spring
的MVC
框架提供了domain model
(领域模型)代码和Web
表单之间的清晰分离,并且集成了 Spring Framework
所有的其他功能。
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 SpringApplicationContext
s and caching of those contexts. It also provides mock objects that you can use to test your code in isolation.
spring-test
模块支持使用JUnit
或TestNG
对Spring
组件进行单元测试和集成测试。 它提供了SpringApplicationContexts
的一致加载和这些上下文的缓存。 它还提供了 mock objects(模拟对象),你可以使用它来单独测试你的代码。
觉得看着别扭的可以查看本人Gitbook :https://muyinchen.gitbooks.io/spring-framework-5-0-0-m3/content/