本节翻译人员

博客/GitHub

负责范围

虚生花

掘金

Spring Framework Overview

知秋

掘金

审校

Spring makes it easy to create Java enterprise applications. It provides everything you need to embrace the Java language in an enterprise environment, with support for Groovy and Kotlin as alternative languages on the JVM, and with the flexibility to create many kinds of architectures depending on an application’s needs. As of Spring Framework 5.1, Spring requires JDK 8+ (Java SE 8+) and provides out-of-the-box support for JDK 11 LTS. Java SE 8 update 60 is suggested as the minimum patch release for Java 8, but it is generally recommended to use a recent patch release.

Spring让创建Java企业级应用变得简单。它为你提供了在企业环境中使用Java语言所需的一切,并支持Groovy和Kotlin作为JVM上的替代语言,并且可以根据应用程序的需求灵活地创建多种体系结构。从Spring Framework 5.1版本开始,Spring所需的JDK版本为JDK 8以上(Java SE 8+),并且它为JDK 11 LTS提供了开箱即用的支持。建议将Java SE 8 update 60作为Java 8的最低修订版本进行使用。但一般来说,我们推荐你使用最新的修订版本。

Spring supports a wide range of application scenarios. In a large enterprise, applications often exist for a long time and have to run on a JDK and application server whose upgrade cycle is beyond developer control. Others may run as a single jar with the server embedded, possibly in a cloud environment. Yet others may be standalone applications (such as batch or integration workloads) that do not need a server.

Spring支持了广泛的应用场景。在大型企业中,应用程序通常会存在很长一段时间,并且必须跑在JDK上运行,应用程序服务器的更新周期也不受开发人员控制。另一些可能会以单一的内嵌服务器的jar包运行,也可能运行在云环境上。还有一些可能是不需要服务器的单体应用程序(例如批处理或集成工作负载)。

Spring is open source. It has a large and active community that provides continuous feedback based on a diverse range of real-world use cases. This has helped Spring to successfully evolve over a very long time.

Spring是开源的。它拥有一个庞大而活跃的社区,可以根据各种实际用例提供持续的反馈。这已经帮助Spring成功发展很长一段时间了。

Spring的含义(What We Mean by "Spring")

The term "Spring" means different things in different contexts. It can be used to refer to the Spring Framework project itself, which is where it all started. Over time, other Spring projects have been built on top of the Spring Framework. Most often, when people say "Spring", they mean the entire family of projects. This reference documentation focuses on the foundation: the Spring Framework itself.

Spring在不同的场景中表示不同的含义。它可以被用来指代Spring Framework项目本身,这也是它一开始的含义。随着时间的推移,在Spring Framework之上建立了其他Spring项目。大部分情况下,当人们聊到Spring时,他们所说的Spring是Spring整个家族。本参考文档注重的则是基础,即Spring Framework自身。

The Spring Framework is divided into modules. Applications can choose which modules they need. At the heart are the modules of the core container, including a configuration model and a dependency injection mechanism. Beyond that, the Spring Framework provides foundational support for different application architectures, including messaging, transactional data and persistence, and web. It also includes the Servlet-based Spring MVC web framework and, in parallel, the Spring WebFlux reactive web framework.

Spring Framework被分为多个模块。应用程序可以选择它们所需要的模块。其中最核心的模块就是core容器,它包括了一个配置模型以及依赖注入机制。除此之外,Spring Framework提供了对不同应用架构的支持,其中包括消息传递,事务数据和持久化以及web。它还包括基于Servlet的Spring MVC web框架,以及并行方面的Spring WebFlux响应式web框架。

A note about modules: Spring’s framework jars allow for deployment to JDK 9’s module path ("Jigsaw"). For use in Jigsaw-enabled applications, the Spring Framework 5 jars come with "Automatic-Module-Name" manifest entries which define stable language-level module names ("spring.core", "spring.context" etc) independent from jar artifact names (the jars follow the same naming pattern with "-" instead of ".", e.g. "spring-core" and "spring-context"). Of course, Spring’s framework jars keep working fine on the classpath on both JDK 8 and 9+.

关于模块化的注意事项:Spring Framework的jar包可以使用JDK 9的模块化路径("Jigsaw")进行部署。为了在Jigsaw的应用中进行使用,Spring框架5的jar包中有"Automatic-Module-Name" manifest入口,里面定义了基于JDK9+模块化标准的模块名称(例如,"spring.core", "spring.context"等等)。它们独立于jar包的artifact名(jar包所遵循的相同命名模式,它使用"-"代替"."。例如,"spring-core" 和 "spring-context")当然,Spring Framework的jar包也能在JDK8和9+的类路径上正常工作。

Spring以及Spring框架的历史(History of Spring and the Spring Framework)

Spring came into being in 2003 as a response to the complexity of the early J2EE specifications. While some consider Java EE and Spring to be in competition, Spring is, in fact, complementary to Java EE. The Spring programming model does not embrace the Java EE platform specification; rather, it integrates with carefully selected individual specifications from the EE umbrella:

作为早期 J2EE 这个复杂规范的回应,Spring在2003年应运而生。 尽管有些人认为Java EE和Spring之间是一种竞争关系。但事实上,Spring是对Java EE的一种补充。在Spring的编程模型中并不包含Java EE的平台规范。 相反,它精心整合了一些EE中的个别规范。

The Spring Framework also supports the Dependency Injection (JSR 330) and Common Annotations (JSR 250) specifications, which application developers may choose to use instead of the Spring-specific mechanisms provided by the Spring Framework.

Spring Framework还支持依赖注入( JSR 330)和通用注解规范( JSR 250)。 应用程序开发者可以选择使用这些规范而无须使用Spring Framework所提供的Spring所特定的机制。

As of Spring Framework 5.0, Spring requires the Java EE 7 level (e.g. Servlet 3.1+, JPA 2.1+) as a minimum - while at the same time providing out-of-the-box integration with newer APIs at the Java EE 8 level (e.g. Servlet 4.0, JSON Binding API) when encountered at runtime. This keeps Spring fully compatible with e.g. Tomcat 8 and 9, WebSphere 9, and JBoss EAP 7.

从Spring Framework 5.0开始,Spring将Java EE 7(e.g. Servlet 3.1+, JPA 2.1+)作为开发环境的最低要求。同时提供了与Java EE 8级别的较新API的开箱即用的相关集成,以防在运行时碰到。

Over time, the role of Java EE in application development has evolved. In the early days of Java EE and Spring, applications were created to be deployed to an application server. Today, with the help of Spring Boot, applications are created in a devops- and cloud-friendly way, with the Servlet container embedded and trivial to change. As of Spring Framework 5, a WebFlux application does not even use the Servlet API directly and can run on servers (such as Netty) that are not Servlet containers.

随着时间的推移,Java EE在应用程序开发中所扮演的角色也已经改变。在Java EE和Spring的早期阶段,通常我们创建完应用程序并将它们部署到应用程序服务器上。 如今,在Spring Boot的帮助下,可以很友好地为devops和cloud创建应用程序,它内部嵌入了Servlet容器并对其做了微不足道的更改。 从Spring Framework 5开始,基于WebFlux开发的应用程序甚至都不直接使用Servlet API就能够运行在非Servlet容器的服务器上(例如Netty)。

Spring continues to innovate and to evolve. Beyond the Spring Framework, there are other projects, such as Spring Boot, Spring Security, Spring Data, Spring Cloud, Spring Batch, among others. It’s important to remember that each project has its own source code repository, issue tracker, and release cadence. See spring.io/projects for the complete list of Spring projects.

Spring将持续创新和发展。除了Spring Framework以外,还有其他一些项目。例如Spring Boot,Spring Security,Spring Data,Spring Cloud,Spring Batch等等。 要记住每个项目都有它自己源码仓库,问题跟踪以及发布节奏,这一点很重要。有关Spring项目的完整列表,具体请参阅 spring.io/projects

设计理念(Design Philosophy)

When you learn about a framework, it’s important to know not only what it does but what principles it follows. Here are the guiding principles of the Spring Framework:

当你在学习一个框架的时候,重要的是,你不仅要知道它做什么,而且还要知道它遵循什么原则。以下是Spring框架的指导原则:

  • Provide choice at every level. Spring lets you defer design decisions as late as possible. For example, you can switch persistence providers through configuration without changing your code. The same is true for many other infrastructure concerns and integration with third-party APIs.

  • Accommodate diverse perspectives. Spring embraces flexibility and is not opinionated about how things should be done. It supports a wide range of application needs with different perspectives.

  • Maintain strong backward compatibility. Spring’s evolution has been carefully managed to force few breaking changes between versions. Spring supports a carefully chosen range of JDK versions and third-party libraries to facilitate maintenance of applications and libraries that depend on Spring.

  • Care about API design. The Spring team puts a lot of thought and time into making APIs that are intuitive and that hold up across many versions and many years.

  • Set high standards for code quality. The Spring Framework puts a strong emphasis on meaningful, current, and accurate javadoc. It is one of very few projects that can claim clean code structure with no circular dependencies between packages.

  • 为每个级别提供选择。Spring让你尽可能的推迟你的设计决策。例如,在无需修改你代码的情况下,你可以通过配置切换Persistence Provider。对于许多其他基础架构问题以及与第三方API的集成也是如此。

  • 容纳不同的观点。Spring拥有很高的灵活性,它并不会强制你该按照怎样的方式去办事。它从不同的角度支持广泛的应用程序需求。

  • 保持强大的向后兼容性。Spring团队对于Spring的演变进行了仔细的管理。使得各个版本之间没有什么特别大的差异。Spring仔细选择了所支持的JDK版本以及第三方库的版本范围,有助于维护依赖于Spring的应用程序和库。

  • 注重于API设计。Spring团队为了打造直观的API投入了大量时间和精力。这使得这些API能够在许多版本和很多年后依然适用。

  • 严格要求代码质量。Spring框架非常注重javadoc,它必须有意义,保持最新并且准确无误。

反馈与贡献(Feedback and Contributions)

For how-to questions or diagnosing or debugging issues, we suggest using StackOverflow, and we have a questions page that lists the suggested tags to use. If you’re fairly certain that there is a problem in the Spring Framework or would like to suggest a feature, please use the GitHub Issues.

对于如何提问或诊断和调试问题,我们推荐使用StackOverflow,并且我们有一个 问题页面,上面列出了一系列推荐使用的标签。 如果你对Spring框架有任何疑问或想提出功能,请使用 GitHub Issues

If you have a solution in mind or a suggested fix, you can submit a pull request on Github. However, please keep in mind that, for all but the most trivial issues, we expect a ticket to be filed in the issue tracker, where discussions take place and leave a record for future reference.

如果你有解决方案,你可以在 Github上提交pull请求。但是,请注意,除了最微不足道的问题之外的所有问题,我们希望对问题跟踪讨论并保留记录,以备将来参考。

For more details see the guidelines at the CONTRIBUTING, top-level project page.

更多详细信息,请参考 CONTRIBUTING文档。

入门指南(Getting Started)

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 Framework,并倾心于约定大于配置,旨在让你可以快速启动并运行程序。

You can use start.spring.io to generate a basic project or follow one of the "Getting Started" guides, such as Getting Started Building a RESTful Web Service. 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风格的微服务。这些指南不仅易于理解,且注重任务需求,并且大部分都是基于Spring Boot。它们还涵盖了Spring家族中的其他项目。当你在解决某些特定问题时,你可能会用到它们。