Spring 框架文档6.0.11 (Spring Frameword Documentation 6.0.11)-概述(第2章)

123 阅读10分钟

Spring 框架概述

Spring Framework Overview
  

Spring使得创建Java企业应用变得简单。它提供了一切你需要在企业环境中使用Java语言的工具,支持在JVM上使用Groovy和Kotlin作为替代语言,并具有根据应用需求创建多种架构的灵活性。从Spring Framework 6.0开始,Spring要求使用Java 17+。 Spring支持广泛的应用场景。在大型企业中,应用程序通常存在很长时间,并且必须在开发人员无法控制其更新周期的JDK和应用服务器上运行。其他应用程序可能作为单个JAR文件与嵌入式服务器一起运行,可能在云环境中。还有一些可能是独立的应用程序(如批处理或集成工作负载),不需要服务器。 Spring是开源的。它拥有庞大而活跃的社区,根据各种真实世界的使用案例提供持续的反馈。这使得Spring能够在很长时间内成功演进。

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 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 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 是什么意思?

  What We Mean by "Spring"

“Spring”这个术语在不同的上下文中有不同的含义。它可以用来指代Spring Framework项目本身,这是它的起源。随着时间的推移,其他的Spring项目也建立在Spring Framework之上。大多数情况下,当人们说“Spring”时,他们指的是整个项目家族。本参考文档重点介绍了基础部分:Spring Framework本身。

Spring Framework被分为不同的模块。应用程序可以选择需要的模块。核心容器模块包括配置模型和依赖注入机制。除此之外,Spring Framework还为不同的应用架构提供了基础支持,包括消息传递、事务性数据和持久化以及Web。它还包括基于Servlet的Spring MVC Web框架,以及并行开发的Spring WebFlux响应式Web框架。

关于模块的说明:Spring的框架JAR文件可以部署到JDK 9的模块路径("Jigsaw")。对于在Jigsaw启用的应用程序中使用,Spring Framework 5的JAR文件附带了"Automatic-Module-Name"清单条目,定义了稳定的语言级模块名称("spring.core","spring.context"等),与JAR文件的构件名称无关(JAR文件遵循相同的命名模式,但使用"-"代替".",例如"spring-core"和"spring-context")。当然,Spring的框架JAR文件在JDK 8和9+上的类路径上仍然可以正常工作。

 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.

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.

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 和Spring 框架的发展历史

      History of Spring and the Spring Framework
      

Spring诞生于2003年,是对早期J2EE规范复杂性的回应。尽管有人认为Java EE及其现代继任者Jakarta EE与Spring存在竞争关系,但实际上它们是互补的。Spring编程模型并不包含Jakarta EE平台规范,而是与传统EE范围内精选的个别规范进行集成: 1)Servlet API (JSR 340) 2)WebSocket API (JSR 356) 3)Concurrency Utilities (JSR 236) 4)JSON Binding API (JSR 367) 5)Bean Validation (JSR 303) 6)JPA (JSR 338) 7)JMS (JSR 914) 8)以及必要时用于事务协调的JTA/JCA设置。

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

从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完全兼容,并且与Hibernate ORM 6.1也兼容。

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

Spring继续创新和演进。除了Spring Framework之外,还有其他项目,如Spring Boot、Spring Security、Spring Data、Spring Cloud、Spring Batch等。每个项目都有自己的源代码存储库、问题跟踪器和发布节奏。请访问spring.io/projects获取完整的Spring项目列表。

 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:
    
Servlet API ([JSR 340](https://jcp.org/en/jsr/detail?id=340))
WebSocket API ([JSR 356](https://www.jcp.org/en/jsr/detail?id=356))
Concurrency Utilities (JSR 236)
JSON Binding API (JSR 367)
Bean Validation (JSR 303)
JPA (JSR 338)
JMS (JSR 914)
as well as JTA/JCA setups for transaction coordination, if necessary.

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.

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.

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 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 Framework的指导原则:

在每个层面提供选择。Spring允许你尽可能推迟设计决策。例如,你可以通过配置切换持久化提供者而无需更改代码。对于许多其他基础设施问题和与第三方API的集成也是如此。

容纳不同的观点。Spring支持灵活性,对于如何完成事情并没有固定的观点。它支持不同角度的各种应用需求。

保持强大的向后兼容性。Spring的演进经过精心管理,几乎没有在版本之间引入重大变化。Spring支持一系列精心选择的JDK版本和第三方库,以便于维护依赖于Spring的应用程序和库。

关注API设计。Spring团队花费了很多时间和精力来创建直观且经得起多个版本和多年考验的API。

设定高标准的代码质量。Spring Framework非常重视有意义、准确和最新的javadoc。它是极少数可以声称代码结构清晰、包之间没有循环依赖的项目之一。

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。点击此处获取建议在Stack Overflow上使用的标签列表。如果您相当确定Spring Framework中存在问题或想建议一个功能,请使用GitHub Issues。

如果您有解决方案或建议的修复方法,您可以在Github上提交一个pull request。但请注意,除了最简单的问题外,我们希望在问题跟踪器中提出一个工单,讨论并留下记录以供将来参考。

有关更多详细信息,请参阅CONTRIBUTING,顶级项目页面上的指南。

 For how-to questions or diagnosing or debugging issues, we suggest using Stack Overflow. [Click](https://stackoverflow.com/questions/tagged/spring+or+spring-mvc+or+spring-aop) 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.

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.

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

开始指南

  Getting Started
  

如果您刚开始使用Spring,您可以通过创建一个基于Spring Boot的应用程序来开始使用Spring Framework。Spring Boot提供了一种快速(并且有主观意见的)方式来创建一个可用于生产的基于Spring的应用程序。它基于Spring Framework,偏向于约定优于配置,并旨在让您尽快上手。

您可以使用start.spring.io生成一个基本项目,或者按照其中一个“入门指南”进行操作,例如“入门构建RESTful Web服务”。除了更易于理解外,这些指南非常专注于任务,大部分都基于Spring Boot。它们还涵盖了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.

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.