Spring Framework Overview

Spring 使得创建 Java 企业应用程序变得容易。它提供了您在企业环境中使用 Java 语言所需的一切,包括对 Groovy 和 Kotlin 作为 JVM 上备用语言的支持,以及根据应用程序的需要创建多种架构的灵活性。从 Spring Framework 6.0 开始,Spring 需要 Java 17 及更高版本。

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 6.0, Spring requires Java 17+.

Spring 支持广泛的应用程序场景。在大型企业中,应用程序往往存在很长时间,并且必须在 JDK 和应用程序服务器上运行,而它们的升级周期超出了开发者的控制范围。其他应用程序可能作为带有嵌入服务器的单个 jar 文件运行,可能在云环境中运行。还有一些可能是独立应用程序(如批处理或集成工作负载),不需要服务器。

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 the developer’s 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 是一个开源项目。它拥有一个庞大而活跃的社区,可以根据多种实际用例提供持续的反馈。这一功能帮助 Spring 在很长的时间里成功地不断发展。

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.

What We Mean by "Spring"

在不同的上下文中,“Spring” 一词含义不同。它可以用来指代 Spring Framework 项目本身,它就是从那里开始的。随着时间的推移,其他 Spring 项目已构建在 Spring Framework 之上。在大多数情况下,当人们说“Spring”时,他们指的是整个项目系列。此参考文档重点介绍了基础:Spring Framework 本身。

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 Framework 分为多个模块。应用程序可以选择所需的模块。核心是核心容器的模块,包括配置模型和依赖关系注入机制。除此之外,Spring Framework 为不同的应用程序架构提供基础支持,包括消息传递、事务数据和持久性,以及 Web。它还包括基于 Servlet 的 Spring MVC Web 框架,以及并行的 Spring WebFlux 响应式 Web 框架。

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 的框架 jar 允许部署到 JDK 9 的模块路径(“Jigsaw”)。为了在启用 Jigsaw 的应用程序中使用,Spring Framework 5 jar 附带“Automatic-Module-Name”清单项,这些项定义与 jar 工件名称无关(jar 遵循相同的命名模式,用 “-” 替换 “.”,例如 “spring-core” 和 “spring-context”)的稳定的语言级模块名称(“spring.core”、“spring.context” 等)。当然,Spring 的框架 jar 在 JDK 8 和 9+ 的类路径上也能正常工作。

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+.

History of Spring and the Spring Framework

Spring 诞生于 2003 年,是对早期 J2EE 规范的复杂性的回应。虽然有人认为 Java EE 及其现代继任者 Jakarta EE 与 Spring 相竞争,但事实上它们是互补的。Spring 编程模型并未采用 Jakarta EE 平台规范;相反,它与从传统的 EE 框架中精心挑选的各个规范相集成:

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

Spring Framework 还支持依赖项注入 (JSR 330) 和通用注释 (JSR 250) 规范,应用程序开发者可以选择使用它们,而不是 Spring Framework 提供的特定于 Spring 的机制。最初,它们基于常见的 javax 包。

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. Originally, those were based on common javax packages.

从 Spring Framework 6.0 开始,Spring 已升级到 Jakarta EE 9 级别(例如 Servlet 5.0+、JPA 3.0+),基于 jakarta 命名空间,而不是传统的 javax 包。由于 EE 9 是最低版本,而且 EE 10 已获得支持,Spring 准备对 Jakarta EE API 的进一步演进提供开箱即用的支持。Spring Framework 6.0 与 Tomcat 10.1、Jetty 11 和 Undertow 2.3 完全兼容,用作 Web 服务器,还与 Hibernate ORM 6.1 兼容。

As of Spring Framework 6.0, Spring has been upgraded to the Jakarta EE 9 level (e.g. Servlet 5.0+, JPA 3.0+), based on the jakarta namespace instead of the traditional javax packages. With EE 9 as the minimum and EE 10 supported already, Spring is prepared to provide out-of-the-box support for the further evolution of the Jakarta EE APIs. Spring Framework 6.0 is fully compatible with Tomcat 10.1, Jetty 11 and Undertow 2.3 as web servers, and also with Hibernate ORM 6.1.

随着时间的推移,Java/Jakarta EE 在应用程序开发中的角色发生了演变。在 J2EE 和 Spring 早期,应用程序都是为了部署到应用程序服务器而创建的。如今,在 Spring Boot 的帮助下,可以在 devops 和云友好的方式中创建应用程序,其中 Servlet 容器已嵌入并且易于更改。在 Spring Framework 5 中,WebFlux 应用程序甚至不再直接使用 Servlet API,并且可以在不是 Servlet 容器的服务器(例如 Netty)上运行。

Over time, the role of Java/Jakarta EE in application development has evolved. In the early days of J2EE 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.

Spring 不断创新和发展。除了 Spring Framework 以外,还有其他的项目,例如 Spring Boot、Spring Security、Spring Data、Spring Cloud、Spring Batch 等。请记住,每个项目都有自己的源代码存储库、问题追踪器和发布节奏。请参见 spring.io/projects 了解 Spring 项目的完整列表。

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.

Design Philosophy

当您了解框架时,了解它不仅可以做什么,而且遵循什么原则非常重要。以下是 Spring Framework 的指导原则:

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:

  • 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.

Feedback and Contributions

对于操作方法问题或诊断或调试问题,我们建议您使用 Stack Overflow。单击此处 here,获取要在 Stack Overflow 上使用的建议标签列表。如果您相当确定 Spring Framework 中有个问题或者想要建议个功能,请使用 GitHub Issues

For how-to questions or diagnosing or debugging issues, we suggest using Stack Overflow. Click here for a list of the suggested tags to use on Stack Overflow. 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.

如果您心中有了解决方案或建议的修复,您可以在 Github 上提交一个请求请求。但是,请记住对于除最简单的之外的所有问题,我们期望在问题追踪器中填写一张工单,问题追踪器是讨论问题和将记录保留给未来参考的地方。

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.

有关更多详情,请参见 CONTRIBUTING 的指南,这是顶级项目页面。

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

Getting Started

如果您刚刚开始使用 Spring,您可能想要使用 Spring Boot-basedapplication 创建一个 Spring Framework。Spring Boot 提供了一种快速(且固执己见)的方式来创建一个基于 Spring 的生产就绪应用。它基于 Spring Framework,优选惯例高于配置,旨在尽快让您开始运行。

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.

您可以使用 start.spring.io 生成一个基本的项目,或者按照其中一个 "入门" 指南,例如 RESTful Web 服务入门指南。这些指南不仅容易消化,而且非常注重任务,并且大多数都基于 Spring Boot。它们还涵盖 Spring 产品组合中的其他项目,当您解决特定问题时,您可能需要考虑这些项目。

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.